示例#1
0
func (msg *gatewayMsg) Marshal(w *binary.Writer) error {
	w.WriteUint8(msg.Command)
	//	DEBUG("发送:" + strconv.Itoa(int(msg.Command)))
	switch msg.Command {
	case CMD_NEW_1:
		w.WriteUint64BE(msg.ClientId)
		w.WritePacket(msg.Data, binary.SplitByUint8)
	case CMD_NEW_2:
		w.WriteUint64BE(msg.ClientId)
		w.WriteUint64BE(msg.ClientIds[0])
	case CMD_NEW_3:
		w.WriteUint64BE(msg.ClientId)
	case CMD_DEL:
		w.WriteUint64BE(msg.ClientId)
	case CMD_MSG:
		w.WriteUint64BE(msg.ClientId)
		goto ENCODE
	case CMD_BRD:
		w.WriteUvarint(uint64(len(msg.ClientIds)))
		for i := 0; i < len(msg.ClientIds); i++ {
			w.WriteUvarint(msg.ClientIds[i])
		}
		goto ENCODE
	}
	return w.Flush()
ENCODE:
	if fast, ok := msg.Message.(packet.FastOutMessage); ok {
		binary.SplitByUvarint.WriteHead(w, fast.MarshalSize())
		if err := fast.Marshal(w); err != nil {
			return err
		}
	} else {
		data, err := msg.Message.(packet.OutMessage).Marshal()
		if err != nil {
			return err
		}
		w.WritePacket(data, binary.SplitByUvarint)
	}
	return w.Flush()
}
示例#2
0
文件: message.go 项目: 616050468/link
func (msg *gatewayMsg) BinaryEncode(w *binary.Writer) error {
	w.WriteUint8(msg.Command)
	switch msg.Command {
	case CMD_NEW_1:
		w.WriteUint64BE(msg.ClientId)
		w.WritePacket(msg.Data, binary.SplitByUint8)
	case CMD_NEW_2:
		w.WriteUint64BE(msg.ClientId)
		w.WriteUint64BE(msg.ClientIds[0])
	case CMD_DEL:
		w.WriteUint64BE(msg.ClientId)
	case CMD_MSG:
		w.WriteUint64BE(msg.ClientId)
		w.WritePacket(msg.Data, binary.SplitByUvarint)
	case CMD_BRD:
		w.WriteUvarint(uint64(len(msg.ClientIds)))
		for i := 0; i < len(msg.ClientIds); i++ {
			w.WriteUvarint(msg.ClientIds[i])
		}
		w.WritePacket(msg.Data, binary.SplitByUvarint)
	}
	return nil
}
示例#3
0
func (obj *TestObject) BinaryEncode(w *binary.Writer) error {
	w.WriteVarint(int64(obj.X))
	w.WriteVarint(int64(obj.Y))
	w.WriteVarint(int64(obj.Z))
	return nil
}
示例#4
0
func (msg TestMessage) Marshal(w *binary.Writer) error {
	w.WritePacket(msg, binary.SplitByUvarint)
	return nil
}
示例#5
0
func (msg RAW) Marshal(w *binary.Writer) error {
	_, err := w.Write(msg)
	return err
}