mirror of
https://gogs.blitter.com/RLabs/xs
synced 2024-08-14 10:26:42 +00:00
HKExConn captures net.Conn
This commit is contained in:
parent
663f2f6d1f
commit
c8b4fa3596
5 changed files with 38 additions and 4 deletions
|
@ -20,6 +20,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
hkex "blitter.com/herradurakex"
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
BIN
demo/client
BIN
demo/client
Binary file not shown.
|
@ -4,15 +4,21 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conn, err := net.Dial("tcp", "localhost:2000")
|
bareconn, err := net.Dial("tcp", "localhost:2000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
// handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn := hkex.NewHKExConn(&bareconn)
|
||||||
|
fmt.Printf("conn: %v\n", conn)
|
||||||
|
|
||||||
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
|
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')
|
_, err = bufio.NewReader(conn).ReadString('\n')
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
// "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -26,14 +27,13 @@ func main() {
|
||||||
|
|
||||||
fmt.Println("Accepted client")
|
fmt.Println("Accepted client")
|
||||||
|
|
||||||
|
|
||||||
// Handle the connection in a new goroutine.
|
// Handle the connection in a new goroutine.
|
||||||
// The loop then returns to accepting, so that
|
// The loop then returns to accepting, so that
|
||||||
// multiple connections may be served concurrently.
|
// multiple connections may be served concurrently.
|
||||||
go func(c net.Conn) {
|
go func(c net.Conn) {
|
||||||
// Echo all incoming data.
|
// Echo all incoming data.
|
||||||
io.Copy(c, c)
|
io.Copy(c, c)
|
||||||
fmt.Println("Client sent:%v\n",c)
|
fmt.Println("Client sent:%v\n", c)
|
||||||
// Shut down the connection.
|
// Shut down the connection.
|
||||||
c.Close()
|
c.Close()
|
||||||
}(conn)
|
}(conn)
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -130,3 +131,29 @@ func (h *HerraduraKEx) String() string {
|
||||||
h.PeerD.Text(16),
|
h.PeerD.Text(16),
|
||||||
h.fa.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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue