示例#1
0
            public void RecieveAsync([NotNull] Socket socket, int expectedMessageSize, [NotNull] TOuterToken outerToken, [NotNull] RecieverCallback callback)
            {
                if (socket == null)
                {
                    throw new ArgumentNullException("socket");
                }
                if (expectedMessageSize <= 0)
                {
                    throw new ArgumentOutOfRangeException("expectedMessageSize", "must be positive");
                }
                if (outerToken == null)
                {
                    throw new ArgumentNullException("outerToken");
                }
                if (callback == null)
                {
                    throw new ArgumentNullException("callback");
                }

                var buffer     = _bufferManager.CheckOut(expectedMessageSize);
                var socketArgs = _socketArgsPool.Get();

                var token = new RecieveToken(socket, outerToken, buffer, expectedMessageSize, callback);

                socketArgs.BufferList = buffer;
                socketArgs.Completed += RecieveCompletedHandler;
                socketArgs.UserToken  = token;

                try
                {
                    bool willRaiseEvent = socket.ReceiveAsync(socketArgs);
                    if (!willRaiseEvent)
                    {
                        RecieveCompletedHandler(this, socketArgs);
                    }
                }
                catch (SocketException socketException)
                {
                    Debug.Assert(socketException.SocketErrorCode != SocketError.Success);
                    ReleaseResourcesAndCallErrorCallback(socketArgs, token, socketException.SocketErrorCode);
                }
            }
示例#2
0
            private void ReleaseResourcesAndCallErrorCallback([NotNull] SocketAsyncEventArgs socketEventArgs, [NotNull] RecieveToken token, SocketError socketError)
            {
                ReleaseBuffer(_bufferManager, token.Buffer);
                ReleaseSocketArgs(_socketArgsPool, socketEventArgs, RecieveCompletedHandler);

                token.Callback(token.OuterToken, socketError, null);
            }