Skip to content

Commit

Permalink
增加示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
吴迎松 committed Oct 16, 2017
1 parent afae40a commit 852d4e3
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/defaultServer/callback.go
Original file line number Diff line number Diff line change
@@ -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())
}
34 changes: 34 additions & 0 deletions examples/defaultServer/client.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 11 additions & 0 deletions examples/defaultServer/main.go
Original file line number Diff line number Diff line change
@@ -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 {}
}
10 changes: 10 additions & 0 deletions examples/defaultServer/server.go
Original file line number Diff line number Diff line change
@@ -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()
}
23 changes: 23 additions & 0 deletions examples/echoServer/callback.go
Original file line number Diff line number Diff line change
@@ -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())
}
29 changes: 29 additions & 0 deletions examples/echoServer/protocol.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions examples/echoServer/server.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 852d4e3

Please sign in to comment.