diff --git a/examples/defaultServer/callback.go b/examples/defaultServer/callback.go new file mode 100644 index 0000000..7e147d5 --- /dev/null +++ b/examples/defaultServer/callback.go @@ -0,0 +1,24 @@ +package main + +import ( + "log" + + "github.com/wuyingsong/tcp" +) + +type callback struct{} + +func (c *callback) OnMessage(conn *tcp.TCPConn, p tcp.Packet) { + log.Println("server receive:", string(p.Bytes()[1:])) +} + +func (c *callback) OnConnected(conn *tcp.TCPConn) { + log.Println("new conn:", conn.GetRemoteAddr().String()) +} + +func (c *callback) OnDisconnected(conn *tcp.TCPConn) { + for err := range conn.Errors() { + log.Println(err) + } + log.Printf("%s disconnected\n", conn.GetRemoteIPAddress()) +} diff --git a/examples/defaultServer/client.go b/examples/defaultServer/client.go new file mode 100644 index 0000000..2c64dd2 --- /dev/null +++ b/examples/defaultServer/client.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "log" + "net" + "time" + + "github.com/wuyingsong/tcp" +) + +func startClient(callback tcp.CallBack, protocol tcp.Protocol) { + tcpAddr, err := net.ResolveTCPAddr("tcp", "localhost:9001") + if err != nil { + panic(err) + } + conn, err := net.DialTCP("tcp", nil, tcpAddr) + if err != nil { + panic(err) + } + tc := tcp.NewTCPConn(conn, callback, protocol) + log.Println(tc.Serve()) + i := 0 + for { + if tc.IsClosed() { + break + } + msg := fmt.Sprintf("hello %d", i) + log.Println("client send: ", msg) + tc.AsyncWritePacket(&tcp.DefaultPacket{Type: 1, Body: []byte(msg)}) + i++ + time.Sleep(time.Second) + } +} diff --git a/examples/defaultServer/main.go b/examples/defaultServer/main.go new file mode 100644 index 0000000..c334035 --- /dev/null +++ b/examples/defaultServer/main.go @@ -0,0 +1,11 @@ +package main + +import "github.com/wuyingsong/tcp" + +func main() { + protocol := &tcp.DefaultProtocol{} + protocol.SetMaxPacketSize(100) + go startServer(&callback{}, protocol) + go startClient(nil, protocol) + select {} +} diff --git a/examples/defaultServer/server.go b/examples/defaultServer/server.go new file mode 100644 index 0000000..7975f6f --- /dev/null +++ b/examples/defaultServer/server.go @@ -0,0 +1,10 @@ +package main + +import ( + "github.com/wuyingsong/tcp" +) + +func startServer(callback tcp.CallBack, protocol tcp.Protocol) error { + srv := tcp.NewAsyncTCPServer("localhost:9001", callback, protocol) + return srv.ListenAndServe() +} diff --git a/examples/echoServer/callback.go b/examples/echoServer/callback.go new file mode 100644 index 0000000..a0b433b --- /dev/null +++ b/examples/echoServer/callback.go @@ -0,0 +1,23 @@ +package main + +import ( + "log" + + "github.com/wuyingsong/tcp" +) + +type EchoCallback struct{} + +func (ec *EchoCallback) OnConnected(conn *tcp.TCPConn) { + log.Println("new conn: ", conn.GetRemoteIPAddress()) +} +func (ec *EchoCallback) OnMessage(conn *tcp.TCPConn, p tcp.Packet) { + log.Printf("receive: %s", string(p.Bytes())) + conn.AsyncWritePacket(p) +} +func (ec *EchoCallback) OnDisconnected(conn *tcp.TCPConn) { + for err := range conn.Errors() { + log.Println(err) + } + log.Printf("%s disconnected\n", conn.GetRemoteIPAddress()) +} diff --git a/examples/echoServer/protocol.go b/examples/echoServer/protocol.go new file mode 100644 index 0000000..2eda8e7 --- /dev/null +++ b/examples/echoServer/protocol.go @@ -0,0 +1,29 @@ +package main + +import ( + "bufio" + "io" + + "github.com/wuyingsong/tcp" +) + +type EchoProtocol struct { + data []byte +} + +func (ep *EchoProtocol) Bytes() []byte { + return ep.data +} + +func (ep *EchoProtocol) ReadPacket(reader io.Reader) (tcp.Packet, error) { + rd := bufio.NewReader(reader) + bytesData, err := rd.ReadBytes('\n') + if err != nil { + return nil, err + } + return &EchoProtocol{data: bytesData}, nil +} +func (ep *EchoProtocol) WritePacket(writer io.Writer, msg tcp.Packet) error { + _, err := writer.Write(msg.Bytes()) + return err +} diff --git a/examples/echoServer/server.go b/examples/echoServer/server.go new file mode 100644 index 0000000..5d922b4 --- /dev/null +++ b/examples/echoServer/server.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + "time" + + "github.com/wuyingsong/tcp" +) + +func main() { + srv := tcp.NewAsyncTCPServer("localhost:9001", &EchoCallback{}, &EchoProtocol{}) + srv.SetReadDeadline(time.Second * 30) + srv.SetWriteDeadline(time.Second * 30) + log.Println(srv.ListenAndServe()) +}