Пример #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
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
}