diff --git a/demo/Herradura.go b/demo/Herradura.go index 57fa663..de53624 100644 --- a/demo/Herradura.go +++ b/demo/Herradura.go @@ -20,6 +20,7 @@ package main import ( "flag" "fmt" + hkex "blitter.com/herradurakex" ) diff --git a/demo/client b/demo/client index 3ff95e2..7e019c3 100755 Binary files a/demo/client and b/demo/client differ diff --git a/demo/client.go b/demo/client.go index 301f709..623bb6c 100644 --- a/demo/client.go +++ b/demo/client.go @@ -4,15 +4,21 @@ import ( "bufio" "fmt" "net" + + hkex "blitter.com/herradurakex" ) func main() { - conn, err := net.Dial("tcp", "localhost:2000") + bareconn, err := net.Dial("tcp", "localhost:2000") if err != nil { // handle error } + + conn := hkex.NewHKExConn(&bareconn) + fmt.Printf("conn: %v\n", conn) + fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") -// status, err := bufio.NewReader(conn).ReadString('\n') + // status, err := bufio.NewReader(conn).ReadString('\n') _, err = bufio.NewReader(conn).ReadString('\n') // ... } diff --git a/demo/server.go b/demo/server.go index 91c59fb..d06dfbd 100644 --- a/demo/server.go +++ b/demo/server.go @@ -5,6 +5,7 @@ import ( "io" "log" "net" + // "blitter.com/herradurakex" ) func main() { @@ -26,14 +27,13 @@ func main() { fmt.Println("Accepted client") - // Handle the connection in a new goroutine. // The loop then returns to accepting, so that // multiple connections may be served concurrently. go func(c net.Conn) { // Echo all incoming data. io.Copy(c, c) - fmt.Println("Client sent:%v\n",c) + fmt.Println("Client sent:%v\n", c) // Shut down the connection. c.Close() }(conn) diff --git a/herradurakex.go b/herradurakex.go index e5be8f8..5d5ac16 100644 --- a/herradurakex.go +++ b/herradurakex.go @@ -21,6 +21,7 @@ import ( "fmt" "math/big" "math/rand" + "net" "time" ) @@ -130,3 +131,29 @@ func (h *HerraduraKEx) String() string { h.PeerD.Text(16), h.fa.Text(16)) } + +type HKExConn struct { + c net.Conn +} + +// Return c coerced into a HKExConn (which implements interface net.Conn) +func NewHKExConn(c *net.Conn) (hc *HKExConn) { + fmt.Println("** NewHKExConn wrapping net.Conn **") + return &HKExConn{*c} +} + +func (hc HKExConn) Read(b []byte) (n int, err error) { + n, err = hc.c.Read(b) + if n > 0 { + fmt.Println("** hc.Read() wraps c.Read() **") + } + return +} + +func (hc HKExConn) Write(b []byte) (n int, err error) { + n, err = hc.c.Write(b) + if n > 0 { + fmt.Printf("** hc.Write('%s') wraps c.Write() **\n", b) + } + return +}