I am attempting to pass an already accepted connection to http.Serve().  I 
need to preview the connection allowing inspection of type of traffic 
appearing on a single port.
The desire is to handle a number of types of connections to a single port. 
 I'll preview the traffic, determine what type it is, and then pass it off 
to the appropriate handler.

I understand that I need a net.Listener interface.

Testing code:

package main

import (
"fmt"
"net"
"net/http"
)

func main() {
loginfo.Println("startup")

listener, err := net.Listen("tcp", ":8080")
if err != nil {
loginfo.Println("unable to bind ")
return
}

conn, err := listener.Accept()
if err != nil {
loginfo.Println("unable to bind", err)
return
}

wedgeListener := &WedgeListener{conn: conn}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
loginfo.Println("/")
})

err = http.Serve(wedgeListener, nil)
if err != nil {
loginfo.Println("Serve: ", err)
}

}

func connState(conn net.Conn, state http.ConnState) {
loginfo.Println("connState")
fmt.Println(conn, conn.LocalAddr(), conn.RemoteAddr())
fmt.Println(state)
}

//WedgeListener -- used to hand off connections to other protocols via 
Listen
type WedgeListener struct {
conn net.Conn
once sync.Once
}

//Accept --
func (s *WedgeListener) Accept() (net.Conn, error) {
var c net.Conn

loginfo.Println("Accept")

s.once.Do(func() {
loginfo.Println("Do Once")
c = s.conn
})

if c != nil {
loginfo.Println("accepted")
return c, nil
}
return nil, io.EOF
}

//Close --
func (s *WedgeListener) Close() error {
s.once.Do(func() {
loginfo.Println("close called")
s.conn.Close()
})
return nil
}

//Addr --
func (s *WedgeListener) Addr() net.Addr {
loginfo.Println("Add Called", s.conn.LocalAddr())
return s.conn.LocalAddr()
}

When I hit the listener I see the following:

INFO: main: 2017/02/23 19:44:46.339133 main.go:10: startup

INFO: main: 2017/02/23 19:45:06.887653 wedge_listener.go:56: Add Called 
192.168.1.158:8080

INFO: main: 2017/02/23 19:45:06.887668 wedge_listener.go:20: Accept

INFO: main: 2017/02/23 19:45:06.887671 wedge_listener.go:34: Do Once

INFO: main: 2017/02/23 19:45:06.887673 wedge_listener.go:39: accepted

INFO: main: 2017/02/23 19:45:06.887685 wedge_listener.go:20: Accept

INFO: main: 2017/02/23 19:45:06.887692 main.go:32: Serve:  EOF


It appears to me that http.Serve() does not like the connection I've 
provided via WedgeListener.  It dumps the 1st connection and tries to get 
another.


The WedgeListener works just fine Accepted:


wConn, err := wedgeListener.Accept()

if err != nil {

loginfo.Println("Bad accept ", err)

return

}


cnt, err := wConn.Read(buffer[0:])

if err != nil {

loginfo.Println(err)

return

}

loginfo.Println("byte read", cnt)

loginfo.Println(hex.Dump(buffer[0:cnt]))


I get data back, works great.




-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to