Esempio n. 1
0
 private void WriteToDataStream(AsyncServerConnectionState state, int bytesToWrite)
 {
     try
     {
         state.DataStream.Write(state.ReadEventArgs.Buffer,
                                state.ReadEventArgs.Offset + state.DataProcessingState.DataBufferOffset,
                                bytesToWrite);
     }
     catch (Exception exception)
     {
         Console.Error.WriteLine("Buffer Length: {0}\r\nOffset + DataBuffer Offset: {1}\r\nCount: {2}\r\nLength: {3}\r\nData Size: {4}",
                                state.ReadEventArgs.Buffer.Length,
                                state.ReadEventArgs.Offset + state.DataProcessingState.DataBufferOffset,
                                bytesToWrite,
                                state.DataStream.Length,
                                state.DataSize);
         throw;
     }
     lock (state)
     {
         state.DataProcessingState.BytesLeft -= bytesToWrite;
         state.DataProcessingState.DataBufferOffset += bytesToWrite;
     }
 }
Esempio n. 2
0
 void AcceptCompleted(object sender, SocketAsyncEventArgs evtArgs)
 {
     if (evtArgs.SocketError == SocketError.Success)
     {
         SocketAsyncEventArgs readEventArgs;
         if (_readWriteEvtArgsPool.TryPop(out readEventArgs))
         {
             Socket clientSocket = evtArgs.AcceptSocket;
             AsyncServerConnectionState state = new AsyncServerConnectionState(_receiveBufferSize, readEventArgs);
             state.ClientSocket = clientSocket;
             state.ReadEventArgs.UserToken = state;
             //Really need this?
             //state.ReadEventArgs.AcceptSocket = clientSocket;
             //state.ReadEventArgs.Completed += IOCompleted;
             //state.ReadEventArgs.SetBuffer(state.ReadEventArgs.Buffer, 0, state.ReadEventArgs.Buffer.Length);
             if (!clientSocket.ReceiveAsync(state.ReadEventArgs))
             {
                 ProcessReceive(state.ReadEventArgs);
             }
             Interlocked.Increment(ref _currentConnections);
             if (ClientConnected != null)
                 ClientConnected(this, new ClientConnectedEventArgs((IPEndPoint) evtArgs.AcceptSocket.RemoteEndPoint));
             evtArgs.AcceptSocket = null;
             _acceptEvtArgsPool.Push(evtArgs);
             StartAccept();
         }
     }
     else
         throw new SocketException((int)evtArgs.SocketError);
 }
Esempio n. 3
0
 void ContinueReceiving(AsyncServerConnectionState state)
 {
     state.DataProcessingState.DataBufferOffset = 0;
     if (!state.ClientSocket.ReceiveAsync(state.ReadEventArgs))
         ProcessReceive(state.ReadEventArgs);
     //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " 6");
 }