예제 #1
0
 private static void SendAsyncComplete(object sender, SocketAsyncEventArgs args)
 {
     args.Completed -= SendAsyncComplete;
     SocketHelpers.ReleaseSocketArg(args);
 }
예제 #2
0
        private void ProcessRecieve(SocketAsyncEventArgs args)
        {
            try
            {
                int bytesReceived = args.BytesTransferred;
                recvDataBuffer.SetCount(recvDataBuffer.Count + bytesReceived);

                if (bytesReceived == 0)
                {
                    // no bytes means the client disconnected, so clean up!
                    _server.DisconnectClient(this, true);
                }
                else
                {
                    // increment our counters
                    //unchecked
                    //{
                    //    _bytesReceived += (uint)bytesReceived;
                    //}

                    //Interlocked.Add(ref _totalBytesReceived, bytesReceived);

                    _remainingLength += bytesReceived;

                    if (OnReceive(recvDataBuffer))
                    {
                        _offset = 0;
                        recvDataBuffer.Clear();
                    }
                    else
                    {
                        //var newBuffer = new DataBuffer(BufferSize);
                        // Array.Copy(recvDataBuffer.Data, _offset, newBuffer.Data, 0, _remainingLength);

                        Array.Copy(recvDataBuffer.Data, _offset, recvDataBuffer.Data, 0, _remainingLength);
                        recvDataBuffer.SetPostion(_remainingLength);
                        recvDataBuffer.SetCount(_remainingLength);
                        //recvDataBuffer = newBuffer;

                        _offset = 0;
                        //_receBufferCount++;
                    }

                    ResumeReceive();
                }
            }
            catch (ObjectDisposedException)
            {
                if (_server != null)
                {
                    _server.DisconnectClient(this, true);
                }
            }
            catch (Exception e)
            {
                if (_server != null)
                {
                    //_server.Warning(this, e);

                    _server.DisconnectClient(this, true);
                    Csl.Wl(e);
                }
            }
            finally
            {
                args.Completed -= ReceiveAsyncComplete;
                SocketHelpers.ReleaseSocketArg(args);
            }
        }