Esempio n. 1
0
        // 发出数据包1
        public void SendNetPacket(NetPacket packet)
        {
            ushort opcode = packet.opcode;
            uint   len    = packet.WritePosition;

            if ((len + NetPacketHeader.HEAD_SIZE + 2) > CLIENTSOCKET_SENDBUF_SIZE)
            {
                Debuger.LogError(string.Format("WARNING: Tried to send a packet of {0} bytes (which is too large) to a socket. Opcode was: {1}", (uint)len, (uint)opcode));
                return;
            }

            //Buffer装不下下一个字节流,就直接发出去
            if (_writeBuffer.GetSpace() < len + NetPacketHeader.HEAD_SIZE)
            {
                SendByteBySocket();
            }

            NetworkTools.WriteToBuffer(len, __headerbuf, 0);
            NetworkTools.WriteToBuffer((uint)opcode, __headerbuf, 4);
            _writeBuffer.Write(__headerbuf, NetPacketHeader.HEAD_SIZE);
            if (len > 0)
            {
                _writeBuffer.Write(packet.GetBuffer(), len);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 发出字节流包
        /// </summary>
        public void SendNetPacket(byte[] bytes, UInt16 opcode)
        {
            __sendPacketData.Reset();
            __sendPacketData.opcode = opcode;

            if (bytes != null && bytes.Length > 0)
            {
                UInt16 sizeCount = (UInt16)bytes.Length;
                NetworkTools.WriteToBuffer(sizeCount, __sizeCount_buf, 0);
                __sendPacketData.In(__sizeCount_buf, 4);
                __sendPacketData.In(bytes, (UInt32)bytes.Length);
            }
            m_clientSocket.SendNetPacket(__sendPacketData);
        }