예제 #1
0
        IAsyncResult IAsyncRelayTransport.BeginSendMessageList(List <RelayMessage> messages, AsyncCallback callback, object state)
        {
            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            var result = new RoundTripAsyncResult <List <RelayMessage> >(callback, state)
            {
                SentMessage = messages
            };

            if (messages.Count > 0)
            {
                _asyncSocketClient.SendRoundTripAsync <List <RelayMessage> >(
                    (short)SocketCommand.HandleSyncMessages,
                    messages,
                    RelayMessageFormatter.WriteRelayMessageList,
                    args =>
                {
                    try
                    {
                        if (args.Error != null)
                        {
                            result.Error = args.Error;
                            return;
                        }

                        if (args.Response != null)
                        {
                            result.ResponseMessage = RelayMessageFormatter.ReadRelayMessageList(args.Response);
                        }
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                    }
                    finally
                    {
                        result.Complete(args.CompletedSynchronously);
                    }
                });
            }
            else
            {
                result.ResponseMessage = messages;
                result.Complete(true);
            }

            return(result);
        }
        IAsyncResult IAsyncRelayTransport.BeginSendMessage(RelayMessage message, bool forceRoundTrip, AsyncCallback callback, object state)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (message.IsTwoWayMessage)
            {
                var result = new RoundTripAsyncResult <RelayMessage>(callback, state)
                {
                    SentMessage = message
                };
                asyncSocketClient.SendRoundTripAsync <RelayMessage>(
                    (short)SocketCommand.HandleSyncMessage,
                    message,
                    RelayMessageFormatter.WriteRelayMessage,
                    args =>
                {
                    try
                    {
                        if (args.Error != null)
                        {
                            result.Error = args.Error;
                            return;
                        }

                        if (args.Response != null)
                        {
                            result.ResponseMessage = RelayMessageFormatter.ReadRelayMessage(args.Response);
                        }
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                    }
                    finally
                    {
                        result.Complete(args.CompletedSynchronously);
                    }
                });
                return(result);
            }
            else
            {
                var result = new SimpleAsyncResult(callback, state);
                if (forceRoundTrip)
                {
                    asyncSocketClient.SendRoundTripAsync <RelayMessage>(
                        (short)SocketCommand.HandleSyncMessage,
                        message,
                        RelayMessageFormatter.WriteRelayMessage,
                        args =>
                    {
                        result.Error = args.Error;
                        result.CompleteOperation(args.CompletedSynchronously);
                    });
                }
                else
                {
                    asyncSocketClient.SendOneWayAsync <RelayMessage>(
                        (short)SocketCommand.HandleOneWayMessage,
                        message,
                        RelayMessageFormatter.WriteRelayMessage,
                        args =>
                    {
                        result.Error = args.Error;
                        result.CompleteOperation(args.CompletedSynchronously);
                    });
                }
                return(result);
            }
        }