Пример #1
0
        public void Send(byte[] buffer, int length)
        {
            if (Socket == null)
            {
                return;
            }

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

            var shouldBegin = false;

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

            if (shouldBegin)
            {
                var sendLength = 0;
                var sendBuffer = m_SendQueue.Peek(ref sendLength);

                try
                {
                    Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, OnSend, null);
                    m_Sending = true;
                }
                catch (Exception ex)
                {
                    TraceException(ex);
                    Dispose(false);
                }
            }
        }
Пример #2
0
        private void InternalSend(Packet packet)
        {
            if (Running == false)
            {
                // 表示已处理发送
                packet.Release();
                return;
            }

            // 连接的通知事件
            EventHandler <NetStateSendPacketEventArgs> tempEvent = m_EventSendPacket;

            if (tempEvent != null)
            {
                NetStateSendPacketEventArgs sendEventArgs = new NetStateSendPacketEventArgs(this, packet);
                tempEvent(this, sendEventArgs);

                if (sendEventArgs.IsCancelSend == true)
                {
                    // 表示已处理发送
                    packet.Release();
                    return;
                }
            }

            long iLength = 0;

            PacketProfile packetProfile = PacketProfile.GetOutgoingProfile((byte)packet.PacketID);
            DateTime      dateTimeStart = (packetProfile == null ? DateTime.MinValue : OneServer.NowTime);

            {
                PacketBuffer packetBuffer = packet.AcquireBuffer();
                if (packetBuffer.IsNULL == true)
                {
                    throw new Exception("NetState.InternalSend(...) - packetBuffer.IsNULL == true error!");
                }

                byte[] byteBuffer = packetBuffer.Buffer;
                long   lLength    = packetBuffer.Length;

                // 加密数据
                if (PacketEncoder != null)
                {
                    PacketEncoder.EncodeOutgoingPacket(this, ref byteBuffer, ref lLength);
                }

                m_SendQueue.Enqueue(byteBuffer, 0, lLength);

                // 表示已处理发送
                packet.Release();

                // 实际压缩后的数据长度
                iLength = lLength;
            }
            if (packetProfile != null)
            {
                packetProfile.Record(iLength, OneServer.NowTime - dateTimeStart);
            }
        }
Пример #3
0
        public virtual void Send(Packet p)
        {
            if (Socket == null || BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            int length;

            var buffer = p.Compile(CompressionEnabled, out length);

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

                PacketSendProfile prof = null;

                if (Core.Profiling)
                {
                    prof = PacketSendProfile.Acquire(p.GetType());
                }

                if (prof != null)
                {
                    prof.Start();
                }

                var buffered = false;

                if (PacketEncoder != null || PacketEncryptor != null)
                {
                    var packetBuffer = buffer;
                    var packetLength = length;

                    if (BufferStaticPackets && p.State.HasFlag(PacketState.Acquired))
                    {
                        if (packetLength <= SendBufferSize)
                        {
                            packetBuffer = m_SendBufferPool.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        System.Buffer.BlockCopy(buffer, 0, packetBuffer, 0, packetLength);
                    }

                    if (PacketEncoder != null)
                    {
                        PacketEncoder.EncodeOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    if (PacketEncryptor != null)
                    {
                        PacketEncryptor.EncryptOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    buffered = buffer != packetBuffer && packetBuffer.Length == SendBufferSize;

                    buffer = packetBuffer;
                    length = packetLength;
                }

                try
                {
                    SendQueue.Gram gram;

                    lock (_SendLock)
                    {
                        lock (m_SendQueue)
                        {
                            gram = m_SendQueue.Enqueue(buffer, length);
                        }

                        if (buffered && m_SendBufferPool.Count < SendBufferCapacity)
                        {
                            m_SendBufferPool.ReleaseBuffer(buffer);
                        }

                        if (gram != null && !_Sending)
                        {
                            _Sending = true;

                            try
                            {
                                Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, Socket);
                            }
                            catch (Exception ex)
                            {
                                TraceException(ex);
                                Dispose(false);
                            }
                        }
                    }
                }
                catch (CapacityExceededException)
                {
                    Utility.PushColor(ConsoleColor.Red);
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Utility.PopColor();

                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Finish(length);
                }
            }
            else
            {
                Utility.PushColor(ConsoleColor.Red);
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                Utility.PopColor();

                using (var op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.UtcNow, this);
                    op.WriteLine(new StackTrace());
                }

                Dispose();
            }
        }