Exemplo n.º 1
0
    // 发送数据包
    public void Send(NetProto.Api.ENetMsgId msgId, NetProto.Proto.NetBase packet)
    {
        Int16 id = (Int16)msgId;

        seqId++;
        UInt16 payloadSize = 6; // sizeof(seqid) + sizeof(msgid)

        byte[] data = null;

        if (packet != null)
        {
            NetProto.Proto.ByteArray ba = new NetProto.Proto.ByteArray();
            packet.Pack(ba);
            data = ba.Data();
            ba.Dispose();
            if (data.Length > UInt16.MaxValue - 6)
            {
                Debug.LogError(data.Length + " > UInt16.MaxValue-6");
                return;
            }
            payloadSize += (UInt16)data.Length;
        }

        if (_beforeSendHook != null)
        {
            _beforeSendHook(msgId, data);
        }

        // payload
        byte[] payload = new byte[payloadSize];

        // seqid
        Byte[] _seqid = BitConverter.GetBytes(seqId);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_seqid);
        }
        _seqid.CopyTo(payload, 0);

        // opcode
        Byte[] _opcode = BitConverter.GetBytes(id);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_opcode);
        }
        _opcode.CopyTo(payload, 4);

        // data
        if (data != null)
        {
            data.CopyTo(payload, 6);
        }

        // try encrypt
        byte[] encrypted = EncryptStream(payload);

        // =>pack
        byte[] buffer = new byte[2 + payloadSize]; // sizeof(header) + payloadSize

        // =>header
        Byte[] _header = BitConverter.GetBytes(payloadSize);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_header);
        }
        _header.CopyTo(buffer, 0);

        // =>payload
        encrypted.CopyTo(buffer, 2);

        sendDone.Reset();
        try
        {
            socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), socket);
        }
        catch (Exception e)
        {
            // 如果socket已经断了,报这个异常
            // System.Net.Sockets.SocketException: The socket is not connected
            Debug.Log(e.ToString());
        }
    }
Exemplo n.º 2
0
    // 发送数据包
    public void Send(NetProto.Proto.NetBase packet)
    {
        NetProto.Proto.ByteArray ba = new NetProto.Proto.ByteArray();
        packet.Pack(ba);
        byte[] data = ba.Data();
        ba.Dispose();
        Int16 id = packet.NetMsgId;

        seqid++;
        UInt16 payloadSize = 6; // sizeof(seqid) + sizeof(opcode)

        if (data != null)
        {
            if (data.Length > UInt16.MaxValue - 6)
            {
                Debug.LogError(data.Length + " > UInt16.MaxValue-6");
                return;
            }
            payloadSize += (UInt16)data.Length;
        }

        // payload
        byte[] payload = new byte[payloadSize];

        // seqid
        Byte[] _seqid = BitConverter.GetBytes(seqid);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_seqid);
        }
        _seqid.CopyTo(payload, 0);

        // opcode
        Byte[] _opcode = BitConverter.GetBytes(id);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_opcode);
        }
        _opcode.CopyTo(payload, 4);

        // data
        if (data != null)
        {
            data.CopyTo(payload, 6);
        }

        // try encrypt
        byte[] encrypted = EncryptStream(payload);

        // =>pack
        byte[] buffer = new byte[2 + payloadSize]; // sizeof(header) + payloadSize

        // =>header
        Byte[] _header = BitConverter.GetBytes(payloadSize);
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(_header);
        }
        _header.CopyTo(buffer, 0);

        // =>payload
        encrypted.CopyTo(buffer, 2);

        if (socket.Connected)
        {
            socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), socket);
        }
    }