-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
吴迎松
committed
Oct 16, 2017
1 parent
afae40a
commit 852d4e3
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |