|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/Cai-ki/cinx/ciface" |
| 8 | + "github.com/Cai-ki/cinx/cnet" |
| 9 | +) |
| 10 | + |
| 11 | +type helloRouter struct { |
| 12 | + cnet.BaseRouter |
| 13 | +} |
| 14 | + |
| 15 | +func (h *helloRouter) Handle(request ciface.IRequest) { |
| 16 | + fmt.Println("[Cinx] Received:", string(request.GetData())) |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + |
| 21 | + fmt.Println("Client Test ... start") |
| 22 | + //3秒之后发起测试请求,给服务端开启服务的机会 |
| 23 | + time.Sleep(1 * time.Second) |
| 24 | + |
| 25 | + client := cnet.NewClient("Client", "tcp4", "127.0.0.1", 7777) |
| 26 | + client.AddRouter(0, &helloRouter{}) |
| 27 | + client.Start() |
| 28 | + |
| 29 | + for { |
| 30 | + time.Sleep(3 * time.Second) |
| 31 | + client.Conn().SendMsg(0, []byte("hello")) |
| 32 | + } |
| 33 | + |
| 34 | + // conn, err := net.Dial("tcp4", "127.0.0.1:7777") |
| 35 | + // if err != nil { |
| 36 | + // fmt.Println("client start err, exit!") |
| 37 | + // return |
| 38 | + // } |
| 39 | + |
| 40 | + // //发封包message消息 |
| 41 | + // dp := cnet.NewDataPack() |
| 42 | + // call := func(id uint32, data string) { |
| 43 | + // fmt.Println("==> Call Msg: ID=", id, ", data=", data) |
| 44 | + |
| 45 | + // msg, _ := dp.Pack(cnet.NewMsgPackage(id, []byte(data))) |
| 46 | + // _, err := conn.Write(msg) |
| 47 | + // if err != nil { |
| 48 | + // fmt.Println("write error err ", err) |
| 49 | + // return |
| 50 | + // } |
| 51 | + |
| 52 | + // } |
| 53 | + |
| 54 | + // get := func() (msg *cnet.Message) { |
| 55 | + // msg = &cnet.Message{} |
| 56 | + // msg.Id = 404 |
| 57 | + // //先读出流中的head部分 |
| 58 | + // headData := make([]byte, dp.GetHeadLen()) |
| 59 | + // _, err = io.ReadFull(conn, headData) //ReadFull 会把msg填充满为止 |
| 60 | + // if err != nil { |
| 61 | + // fmt.Println("read head error") |
| 62 | + // return |
| 63 | + // } |
| 64 | + // //将headData字节流 拆包到msg中 |
| 65 | + // msgHead, err := dp.Unpack(headData) |
| 66 | + // if err != nil { |
| 67 | + // fmt.Println("server unpack err:", err) |
| 68 | + // return |
| 69 | + // } |
| 70 | + |
| 71 | + // if msgHead.GetDataLen() > 0 { |
| 72 | + // //msg 是有data数据的,需要再次读取data数据 |
| 73 | + // msg = msgHead.(*cnet.Message) |
| 74 | + // msg.Data = make([]byte, msg.GetDataLen()) |
| 75 | + |
| 76 | + // //根据dataLen从io中读取字节流 |
| 77 | + // _, err := io.ReadFull(conn, msg.Data) |
| 78 | + // if err != nil { |
| 79 | + // fmt.Println("server unpack data err:", err) |
| 80 | + // return |
| 81 | + // } |
| 82 | + |
| 83 | + // fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data)) |
| 84 | + // } |
| 85 | + |
| 86 | + // return |
| 87 | + // } |
| 88 | + |
| 89 | + // call(0, "hello") |
| 90 | + |
| 91 | + // for { |
| 92 | + // msg := get() |
| 93 | + // if msg.Id == 0 { |
| 94 | + // call(0, "get: "+string(msg.Data)) |
| 95 | + // } else if msg.Id == 1001 { |
| 96 | + // // call(1002, "get: "+string(msg.Data)) |
| 97 | + // } else if msg.Id == 1002 { |
| 98 | + // call(1001, "get: "+string(msg.Data)) |
| 99 | + // } else { |
| 100 | + // fmt.Println("error") |
| 101 | + // break |
| 102 | + // } |
| 103 | + // time.Sleep(1 * time.Second) |
| 104 | + // } |
| 105 | +} |
0 commit comments