tcptemplate/test/cli_test.go

110 lines
2.1 KiB
Go
Raw Normal View History

2019-03-04 10:05:34 +00:00
package test
import (
"bytes"
2019-02-28 10:01:09 +00:00
"encoding/binary"
_ "github.com/golang/protobuf/proto"
_ "github.com/hprose/hprose-golang/io"
2019-02-28 10:01:09 +00:00
_ "github.com/trysh/ttb"
2019-03-13 01:20:09 +00:00
_ "go-tcpFramework-rudy/protocol/pb"
2019-02-28 10:01:09 +00:00
_ "io"
"log"
"net"
_ "net/http/pprof"
_ "runtime"
2020-04-07 08:10:32 +00:00
"testing"
2019-02-28 10:01:09 +00:00
"time"
)
2020-04-08 15:09:06 +00:00
/*
tcp
1.
*/
type PackHeader struct {
Identify [2]byte
2019-02-28 10:01:09 +00:00
Length int32
Verify byte
}
var rBuf []byte
2019-02-28 10:01:09 +00:00
func init() {
rBuf = make([]byte, 1024)
}
2019-02-28 10:01:09 +00:00
func SendPackage(writer *net.TCPConn, payload []byte, length int) error {
var header PackHeader
header.Identify[0] = 0x40
header.Identify[1] = 0x41
header.Length = 4
packbuf := bytes.Buffer{}
buf := bytes.NewBuffer([]byte{})
2019-02-28 10:01:09 +00:00
e := binary.Write(buf, binary.BigEndian, header.Length)
if e != nil {
log.Println(e.Error())
}
2019-02-28 10:01:09 +00:00
for _, v := range buf.Bytes() {
header.Verify += v
}
2019-02-28 10:01:09 +00:00
err := binary.Write(&packbuf, binary.BigEndian, header.Identify)
err = binary.Write(&packbuf, binary.BigEndian, header.Length)
err = binary.Write(&packbuf, binary.BigEndian, header.Verify)
if err != nil {
log.Println(err.Error())
}
b := []byte{}
//log.Println("Send length",wb)
2019-02-28 10:01:09 +00:00
b = append(b, packbuf.Bytes()...)
b = append(b, payload[0:length]...)
log.Println(b)
2019-02-28 10:01:09 +00:00
_, e = writer.Write(b)
if e != nil {
log.Println(e.Error())
return e
}
return nil
}
2020-04-07 08:10:32 +00:00
func TestCli(t *testing.T) {
2019-03-04 10:05:34 +00:00
for i := 0; i < 1; i++ {
go func() {
seq := int32(0)
payload := bytes.NewBuffer([]byte{})
2019-03-04 10:05:34 +00:00
server := "118.24.238.198:8052"
servAddr, err := net.ResolveTCPAddr("tcp", server)
2019-02-28 10:01:09 +00:00
if err != nil {
log.Println(err.Error())
}
2019-02-28 10:01:09 +00:00
conn, err := net.DialTCP("tcp", nil, servAddr)
if err != nil {
log.Println(err.Error())
return
}
for {
2019-02-28 10:01:09 +00:00
seq++
payload.Reset()
2019-02-28 10:01:09 +00:00
binary.Write(payload, binary.BigEndian, seq)
log.Println("start send", seq)
e := SendPackage(conn, payload.Bytes(), payload.Len())
//n ,e := conn.Write([]byte{'1','2','3','4'})
2019-02-28 10:01:09 +00:00
if e != nil {
log.Println("client exit")
log.Println(e)
return
}
time.Sleep(time.Second)
}
log.Println("client exit")
}()
}
2019-02-28 10:01:09 +00:00
for {
time.Sleep(time.Millisecond * 100)
}
2019-02-28 10:01:09 +00:00
}