Ejemplo n.º 1
0
//自定义写消息
func (c *DummyConn) WriteMsg(msg []byte) error {
	sendMsg := make([]byte, 8+len(msg))
	copy(sendMsg[:2], msg[:2])
	binary.PutUint64LE(sendMsg[2:10], c.id)
	copy(sendMsg[10:], msg[2:])
	return c.proxySession.Send(sendMsg)
}
Ejemplo n.º 2
0
func (c *TransferProxyConn) Send(data interface{}) error {
	msg := data.(packet.RAW)
	result := make([]byte, 8+len(msg))
	copy(result[:2], msg[:2])
	binary.PutUint64LE(result[2:10], c.id)
	copy(result[10:], msg[2:])
	c.proxySession.Send(packet.RAW(result))
	return nil
}
Ejemplo n.º 3
0
//序列化
func MarshalProtoMsg(identification uint64, args proto.Message) []byte {
	msgID := protos.GetMsgID(args)

	msgBody, _ := proto.Marshal(args)

	result := make([]byte, 2+8+len(msgBody))
	binary.PutUint16LE(result[:2], msgID)
	binary.PutUint64LE(result[2:10], identification)
	copy(result[10:], msgBody)

	return result
}
//发送消息到TransferServer, 网关调用
func SendToGameServer(userSession *link.Session, msg []byte) {
	send_msg := make([]byte, 8+len(msg))
	copy(send_msg[:2], msg[:2])
	binary.PutUint64LE(send_msg[2:10], userSession.Id())
	copy(send_msg[10:], msg[2:])

	//C2S消息,发送到GameServer或者LoginServer
	msgID := binary.GetUint16LE(send_msg[:2])
	if gameProto.IsValidLoginID(msgID) {
		sendGameMsg("LoginServer", send_msg)
	} else {
		sendGameMsg("GameServer", send_msg)
	}
}
Ejemplo n.º 5
0
// Do DH64 key exchange and return a RC4 reader.
func conn_init(conn net.Conn) (*binary.Writer, *binary.Reader, error) {
	var (
		writer = binary.NewWriter(conn)
		reader = binary.NewReader(conn)
	)

	rand.Seed(time.Now().UnixNano())

	privateKey, publicKey := dh64.KeyPair()
	log.Print("server public key: ", publicKey)

	writer.WriteUint64LE(publicKey)
	if writer.Error() != nil {
		return nil, nil, writer.Error()
	}
	clientPublicKey := reader.ReadUint64LE()
	if reader.Error() != nil {
		return nil, nil, reader.Error()
	}
	log.Print("client public key: ", clientPublicKey)

	secert := dh64.Secret(privateKey, clientPublicKey)
	log.Print("secert: ", secert)

	key := make([]byte, 8)
	binary.PutUint64LE(key, secert)
	rc4stream, err := rc4.NewCipher(key)
	if err != nil {
		return nil, nil, err
	}
	log.Print("key: ", hex.EncodeToString(key))

	reader = binary.NewReader(cipher.StreamReader{
		R: conn,
		S: rc4stream,
	})
	return writer, reader, nil
}