2019-03-04 10:05:34 +00:00
|
|
|
|
package test
|
2019-02-28 02:41:16 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2019-02-28 10:01:09 +00:00
|
|
|
|
"encoding/binary"
|
|
|
|
|
_ "github.com/golang/protobuf/proto"
|
2019-02-28 02:41:16 +00:00
|
|
|
|
_ "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"
|
2019-02-28 02:41:16 +00:00
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
_ "runtime"
|
2020-04-07 08:10:32 +00:00
|
|
|
|
"testing"
|
2019-02-28 10:01:09 +00:00
|
|
|
|
"time"
|
2019-02-28 02:41:16 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-04-08 15:09:06 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
tcp 测试程序,测试以下场景
|
|
|
|
|
1.
|
|
|
|
|
*/
|
2019-02-28 02:41:16 +00:00
|
|
|
|
type PackHeader struct {
|
|
|
|
|
Identify [2]byte
|
2019-02-28 10:01:09 +00:00
|
|
|
|
Length int32
|
|
|
|
|
Verify byte
|
2019-02-28 02:41:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rBuf []byte
|
|
|
|
|
|
2019-02-28 10:01:09 +00:00
|
|
|
|
func init() {
|
|
|
|
|
rBuf = make([]byte, 1024)
|
2019-02-28 02:41:16 +00:00
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
|
func SendPackage(writer *net.TCPConn, payload []byte, length int) error {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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 {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
log.Println(e.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 10:01:09 +00:00
|
|
|
|
for _, v := range buf.Bytes() {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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 {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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]...)
|
2019-02-28 02:41:16 +00:00
|
|
|
|
log.Println(b)
|
|
|
|
|
|
2019-02-28 10:01:09 +00:00
|
|
|
|
_, e = writer.Write(b)
|
|
|
|
|
if e != nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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++ {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
go func() {
|
|
|
|
|
seq := int32(0)
|
|
|
|
|
payload := bytes.NewBuffer([]byte{})
|
2019-03-04 10:05:34 +00:00
|
|
|
|
server := "118.24.238.198:8052"
|
2019-02-28 02:41:16 +00:00
|
|
|
|
servAddr, err := net.ResolveTCPAddr("tcp", server)
|
2019-02-28 10:01:09 +00:00
|
|
|
|
if err != nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
|
conn, err := net.DialTCP("tcp", nil, servAddr)
|
|
|
|
|
if err != nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for {
|
2019-02-28 10:01:09 +00:00
|
|
|
|
seq++
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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())
|
2019-02-28 02:41:16 +00:00
|
|
|
|
//n ,e := conn.Write([]byte{'1','2','3','4'})
|
|
|
|
|
|
2019-02-28 10:01:09 +00:00
|
|
|
|
if e != nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
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 {
|
2019-02-28 02:41:16 +00:00
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
|
}
|