public void should_handle_getting_message_too_big_for_spare_buffer()
        {
            _frameAccumulator = new FrameAccumulator();

            should_be_able_to_return_multiple_messages_perfect_delimitation();
            setup();
            _frameAccumulator = new FrameAccumulator();
            should_handle_getting_length_then_message();
            setup();
            _frameAccumulator = new FrameAccumulator();
            should_handle_getting_length_then_split_message();
            setup();
            _frameAccumulator = new FrameAccumulator();
            should_handle_not_getting_full_length_2_buffers_with_offset_on_second();
        }
        private void OnAccept(object sender, SocketAsyncEventArgs e)
        {
            var socket = sender as Socket;
            var receiveSocket = e.AcceptSocket;

            if (e.SocketError != SocketError.Success)
            {
                try
                {
                    lock (_disposeLock)
                    {
                        _logger.ErrorFormat("Error : {0}", e.SocketError);
                        if (_disposing)
                            return;
                        //  if (e.SocketError == SocketError.OperationAborted)
                        //      return;
                        socket.Disconnect(false);
                        socket.Dispose();
                        var endpoint = (IPEndPoint)e.UserToken;
                        Socket s;
                        _endPointToAcceptSockets.TryRemove(endpoint, out s);
                        ListenToEndpoint((IPEndPoint)e.UserToken);
                        return;
                    }
                }

                catch (ObjectDisposedException ex)
                {
                    //silence when socket was disposed by stoplistening
                    _logger.Error(string.Format("object disposed exception raised on accept socket {0}", ex.Source));
                    return;
                }
            }
            receiveSocket.ReceiveBufferSize = 1024 * 500;
            receiveSocket.NoDelay = true;
            _logger.InfoFormat("AcceptingSocket from: {0}", e.AcceptSocket.RemoteEndPoint);
            Console.WriteLine("AcceptingSocket from: {0}", e.AcceptSocket.RemoteEndPoint);

            var socketProcessingInfo = new SocketProcessingInfo
            {
                EndPoint = (IPEndPoint)socket.LocalEndPoint,
                Buffer = _bufferPool.GetItem(),
            };

            lock (_endpointToReceiveSockets)
            {
                List<Socket> socketsForEndpoint;
                var ipEndPoint = socketProcessingInfo.EndPoint;
                if (!_endpointToReceiveSockets.TryGetValue(ipEndPoint, out socketsForEndpoint))
                {
                    socketsForEndpoint = new List<Socket>();
                    _endpointToReceiveSockets[ipEndPoint] = socketsForEndpoint;
                }
                socketsForEndpoint.Add(receiveSocket);
            }
            var frameAccumulator = new FrameAccumulator();
            var localEndPoint = (IPEndPoint)socket.LocalEndPoint;
            Action<Stream> act;
            lock (EventsForMessagesReceived)
            {
                if (!EventsForMessagesReceived.ContainsKey(localEndPoint))
                    EventsForMessagesReceived[localEndPoint] = DummyEvent;
                act = EventsForMessagesReceived[localEndPoint];
            }
            frameAccumulator.MessageReceived += (s) => act(s);
            socketProcessingInfo.FrameAccumulator = frameAccumulator;
            _receivingSockets.Add(socket);
            var receiveEventArgs = _eventArgsPool.GetItem();

            receiveEventArgs.UserToken = socketProcessingInfo;
            receiveEventArgs.Completed += OnReceive;
            receiveEventArgs.SetBuffer(socketProcessingInfo.Buffer, 0, socketProcessingInfo.Buffer.Length);

            if (!receiveSocket.ReceiveAsync(receiveEventArgs))
                OnReceive(receiveSocket, receiveEventArgs);

            e.AcceptSocket = null;
            if (!socket.AcceptAsync(e))
            {
                OnAccept(socket, _eventArgsPool.GetItem());
                _eventArgsPool.PutBackItem(e);

            }
        }
 public void setup()
 {
     _frameAccumulator = new FrameAccumulator();
     _messagesReceived = 0;
 }