Exemple #1
0
func TestSayHi(t *testing.T) {
	opts := NewOptions()
	opts.ClientTimeout = 60 * time.Second
	tcpAddr, gfw := mustStartGFW(opts)
	defer os.RemoveAll(opts.DataPath)
	defer gfw.Exit()

	conn, err := mustConnectGFW(tcpAddr)
	Equal(t, err, nil)
	defer conn.Close()

	reqSayHi := &onepiece.SayHiRequest{
		Msg: "hi",
	}
	buffer, err := proto.Marshal(reqSayHi)
	_, err = protocol.SendFramedResponse(conn, int32(onepiece.NetMsgID_SAYHI), buffer)
	Equal(t, err, nil)

	msgid, buffer, err := protocol.ReadUnpackedResponse(conn)
	Equal(t, err, nil)
	Equal(t, msgid, int32(onepiece.NetMsgID_SAYHI))

	resSayHi := &onepiece.SayHiResponse{}
	err = proto.Unmarshal(buffer, resSayHi)
	Equal(t, err, nil)
	Equal(t, resSayHi.Msg, "叔叔不约!")
}
Exemple #2
0
func (p *tcpServer) Handle(clientConn net.Conn) {
	p.ctx.gfw.Info("TCP: new client(%s)", clientConn.RemoteAddr())

	buf := make([]byte, 4)
	_, err := io.ReadFull(clientConn, buf)
	if err != nil {
		p.ctx.gfw.Error("ERROR: failed to read protocol version - %s", err)
	}
	protocolMagic := string(buf)

	p.ctx.gfw.Info("CLIENT(%s): desired protocol magic '%s'",
		clientConn.RemoteAddr(), protocolMagic)

	var prot protocol.Protocol
	switch protocolMagic {
	case "  V1":
		prot = &protocolV1{ctx: p.ctx}
	default:
		protocol.SendFramedResponse(clientConn, int32(onepiece.NetMsgID_ERROR), []byte("E_BAD_PROTOCOL"))
		clientConn.Close()
		p.ctx.gfw.Info("ERROR: client(%s) bad protocol magic '%s'",
			clientConn.RemoteAddr(), protocolMagic)
		return
	}

	err = prot.IOLoop(clientConn)
	if err != nil {
		p.ctx.gfw.Error("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err)
		return
	}
}
Exemple #3
0
func (p *protocolV1) Send(client *clientV1, frameType int32, data []byte) error {
	client.Lock()
	var zeroTime time.Time
	if client.HeartbeatInterval > 0 {
		client.SetWriteDeadline(time.Now().Add(client.HeartbeatInterval))
	} else {
		client.SetWriteDeadline(zeroTime)
	}

	_, err := protocol.SendFramedResponse(client.Writer, frameType, data)
	if err != nil {
		client.Unlock()
		return err
	}

	err = client.Flush()

	client.Unlock()

	return err
}