From 852d4e3c607a3d409043fc833b9c2b4b17963c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E8=BF=8E=E6=9D=BE?= Date: Mon, 16 Oct 2017 16:40:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/defaultServer/callback.go | 24 +++++++++++++++++++++ examples/defaultServer/client.go | 34 ++++++++++++++++++++++++++++++ examples/defaultServer/main.go | 11 ++++++++++ examples/defaultServer/server.go | 10 +++++++++ examples/echoServer/callback.go | 23 ++++++++++++++++++++ examples/echoServer/protocol.go | 29 +++++++++++++++++++++++++ examples/echoServer/server.go | 15 +++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 examples/defaultServer/callback.go create mode 100644 examples/defaultServer/client.go create mode 100644 examples/defaultServer/main.go create mode 100644 examples/defaultServer/server.go create mode 100644 examples/echoServer/callback.go create mode 100644 examples/echoServer/protocol.go create mode 100644 examples/echoServer/server.go 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()) +}