Пример #1
0
        // Add a SocketAsyncEventArg instance to the pool.
        // "item" = SocketAsyncEventArgs instance to add to the pool.
        public void Push(SocketAsyncEventArgs item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null");
            }

            SockState state = item.UserToken as SockState;

#if !SILVERLIGHT
            item.AcceptSocket = null;
#endif
            if (state != null)
            {
                state.Reset();
            }

            if (!state.IsCached)
            {
                return;
            }

            lock (this.pool)
            {
                this.pool.Push(item);
            }
        }
Пример #2
0
        public void AssembleInboundPacket(byte[] buffer, int bytesReceived, SockState state)
        {
            try
            {
                int incomingPointer = 0; // how much of the incoming data have we read

                while (incomingPointer < bytesReceived)
                {
                    if (state.MessageLength == -1) // don't know how long the message is, still need to read the envelope
                    {
                        int leftForEnvelope = SockState.EnvelopeLength - state.PacketBufferPointer.Position;
                        int numToCopy       = leftForEnvelope;
                        int leftInBlock     = bytesReceived - incomingPointer;
                        if (numToCopy > leftInBlock)
                        {
                            numToCopy = leftInBlock;
                        }

                        Util.Copy(buffer, state.BufferBlockOffset + incomingPointer, state.PacketBuffer, state.PacketBufferPointer.Advance(numToCopy), numToCopy);
                        incomingPointer += numToCopy;

                        if (state.PacketBufferPointer.Position >= SockState.EnvelopeLength)
                        {
                            state.MessageLength   = BitConverter.ToInt32(state.PacketBuffer, 0);
                            state.PacketTypeID    = BitConverter.ToInt32(state.PacketBuffer, 4);
                            state.PacketSubTypeID = BitConverter.ToInt32(state.PacketBuffer, 8);// state.PacketBuffer[8];
                            state.Flags           = (PacketFlags)state.PacketBuffer[12];
                            state.PacketBufferPointer.Reset();
                            state.PacketBuffer = new byte[state.MessageLength];
                            continue;
                        }

                        return;
                    }

                    int bytesNeededToCompleteMessage = state.PacketBuffer.Length - state.PacketBufferPointer.Position;
                    int bytesToRead = bytesNeededToCompleteMessage;
                    if (bytesToRead > bytesReceived - incomingPointer)
                    {
                        bytesToRead = bytesReceived - incomingPointer;
                    }

                    Util.Copy(buffer, state.BufferBlockOffset + incomingPointer, state.PacketBuffer, state.PacketBufferPointer.Advance(bytesToRead), bytesToRead);
                    incomingPointer += bytesToRead;

                    if (state.PacketBufferPointer.Position >= state.MessageLength)
                    {
                        DeserializePacket(state);
                        state.Reset();
                    }
                }
            }
            catch (Exception readExc)
            {
                Log.LogMsg(readExc.Message + ": Shutting down socket.\r\n" + readExc.StackTrace);
                KillConnection("");
            }
        }
Пример #3
0
        public static void PushAcceptEventArg(SocketAsyncEventArgs args, EventHandler <SocketAsyncEventArgs> oldIOCompletedHandler)
        {
            args.Completed -= oldIOCompletedHandler;
            if (m_PoolOfAcceptEventArgs != null)
            {
                m_PoolOfAcceptEventArgs.Push(args);
                //Log.LogMsg("==> Pushed accept arg #" + ((SockState)args.UserToken).ID.ToString() + "#.  There are " + m_PoolOfAcceptEventArgs.Count.ToString() + " left");
            }
            else
            {
#if !SILVERLIGHT
                args.AcceptSocket = null;
#endif
                SockState state = args.UserToken as SockState;
                if (state != null)
                {
                    state.Reset();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Gets called when a send operation resolves.
        /// </summary>
        private void OnSendResolved(SocketAsyncEventArgs args, SockState state)
        {
            //// Log.LogMsg("Testy 13");
            try
            {
                OwningConnection.SentBytes(state.PacketBufferPointer.Position);
                bool isUDP = (state.Flags & PacketFlags.UDP) != 0;
                //Log.LogMsg("==>#### Async SEND Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#");
                if (args.SocketError == SocketError.Success)
                {
                    //// Log.LogMsg("Testy 14");
                    state.PacketBufferPointer.Advance(args.BytesTransferred);
                    if (state.PacketBufferPointer.Position >= state.PacketBuffer.Length)
                    {
                        OwningConnection.PacketSent();
                        // Done sending packet.
                        state.Reset();
                        //Log.LogMsg("==>Done sending packet. Sent " + state.PacketBufferPointer.Position.ToString() + " bytes.");

                        // done sending packet, see if we have anything in the queue ready to go
                        bool             more  = false;
                        Queue <NetQItem> sendQ = isUDP ? m_SendQueueUDP : m_SendQueue;
                        lock (sendQ)
                        {
                            if (sendQ.Count > 0)
                            {
                                NetQItem itm = sendQ.Dequeue();
                                state.PacketBuffer = itm.Data;
                                state.Flags        = itm.Flags;
                                more = true;
                            }
                            else
                            {
                                // release the sending lock
                                if (isUDP)
                                {
                                    //Log.LogMsg("UDP send queue emptied.");
                                    Interlocked.Exchange(ref m_SendingUDP, 0);
                                }
                                else
                                {
                                    //Log.LogMsg("TCP send queue emptied.");
                                    Interlocked.Exchange(ref m_Sending, 0);
                                }
                            }
                        }

                        if (more)
                        {
                            //// Log.LogMsg("Testy 15");
                            SendBuffer(args, state, isUDP);
                        }

                        return;
                    }
                    else
                    {
                        //// Log.LogMsg("Testy 16");
                        // not done sending.  send again.
                        //Log.LogMsg("==>Continuing send.  " + state.PacketBufferPointer.Position.ToString() + " / " + state.PacketBuffer.Length.ToString() + " sent so far.");
                        SendBuffer(args, state, isUDP);
                    }
                }
                else
                {
                    //If we are in this else-statement, there was a socket error.
                    OwningConnection.KillConnection("Error sending packet. " + args.SocketError.ToString());
                }
            }
            catch (Exception ex)
            {
                //// Log.LogMsg("Testy 17");
                Log.LogMsg("Failed to ProcessSend. " + ex.Message);
                OwningConnection.KillConnection("Send error. " + ex.Message);
            }
        }