HKExConn captures net.Conn

This commit is contained in:
Russ Magee 2018-01-06 20:26:08 +00:00
parent 663f2f6d1f
commit c8b4fa3596
5 changed files with 38 additions and 4 deletions

View file

@ -20,6 +20,7 @@ package main
import (
"flag"
"fmt"
hkex "blitter.com/herradurakex"
)

Binary file not shown.

View file

@ -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')
// ...
}

View file

@ -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)

View file

@ -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
}