Ejemplo n.º 1
0
        public bool SendAsync(IPacket oPacket)
        {
            DynamicBuffer sendBuffer = new DynamicBuffer();

            oPacket.Serialization(sendBuffer);
            int totalLength = sizeof(int) + sendBuffer.DataCount;

            SendBufferMgr.StartPacket();
            SendBufferMgr.DynamicBufferManager.WriteInt32(totalLength);
            SendBufferMgr.DynamicBufferManager.WriteInt32(ProtocolMgr.GetPacketID(oPacket));
            SendBufferMgr.DynamicBufferManager.WriteBytes(sendBuffer.Buffer);
            SendBufferMgr.EndPacket();

            bool result = true;

            if (!m_SendAsync)
            {
                int packetOffset = 0;
                int packetCount  = 0;
                if (SendBufferMgr.GetFirstPacket(ref packetOffset, ref packetCount))
                {
                    m_SendAsync = true;
                    result      = AsyncSocket.SendAsync(ConnectSocket, SendEventArgs, SendBufferMgr.DynamicBufferManager.Buffer, packetOffset, packetCount);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        internal bool Receive(byte[] buffer, int offset, int count)
        {
            ReceiveBuffer.WriteBytes(buffer, offset, count);
            bool result = true;

            while (ReceiveBuffer.DataCount > sizeof(int))
            {
                int packetLength = BitConverter.ToInt32(ReceiveBuffer.Buffer, 0);

                if ((packetLength > 10 * 1024 * 1024) | (buffer.Length > 10 * 1024 * 1024))
                {
                    Console.WriteLine("Memory out , will close remote connect");
                    return(false);
                }

                if ((ReceiveBuffer.DataCount - sizeof(int)) >= packetLength)
                {
                    if (ProtocolMgr != null)
                    {
                        result = ProtocolMgr.Process(this, ReceiveBuffer.Buffer, sizeof(int), packetLength);
                        if (result)
                        {
                            ReceiveBuffer.Clear(packetLength + sizeof(int));
                        }
                        else
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        return(result);
                    }
                }
                else
                {
                    return(true);
                }
            }
            return(true);
        }