Esempio n. 1
0
        public void Send(byte[] buffer, int length)
        {
            if (m_Socket == null)
            {
                return;
            }

            if (m_Encoder != null)
            {
                m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
            }

            bool shouldBegin = false;

            lock (m_SendQueue)
                shouldBegin = (m_SendQueue.Enqueue(buffer, length));

            if (shouldBegin)
            {
                int    sendLength = 0;
                byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                try
                {
                    m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, OnSend, null);
                    m_Sending = true;
                }
                catch (Exception ex)
                {
                    TraceException(ex);
                    Dispose(false);
                }
            }
        }
Esempio n. 2
0
        public void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            byte[] buffer = p.Compile(m_CompressionEnabled);

            if (buffer != null)
            {
                if (buffer.Length <= 0)
                {
                    return;
                }

                int length = buffer.Length;

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                bool shouldBegin = false;

                lock (m_SendQueue)
                    shouldBegin = (m_SendQueue.Enqueue(buffer, length));

                if (shouldBegin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        m_Sending = true;
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch                     // ( Exception ex )
                    {
                        //Console.WriteLine(ex);
                        Dispose(false);
                    }
                }

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }
Esempio n. 3
0
        public void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                SendEnqueueResult enqueueResult;

                lock (m_SendQueue)
                    enqueueResult = (m_SendQueue.Enqueue(buffer, length));

                if (enqueueResult == SendEnqueueResult.Begin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        IAsyncResult res = m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch                     // ( Exception ex )
                    {
                        //Console.WriteLine(ex);
                        Dispose(false);
                    }
                }
                else if (enqueueResult == SendEnqueueResult.Overflow)
                {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }