Beispiel #1
0
            public void ReadHeader_Empty()
            {
                using (Stream input = new MemoryStream()) {
                    MessageIO io      = new MessageIO(input, output: null);
                    IAdapter  adapter = this.Adapter;
                    using (TMessage message = adapter.Create(io)) {
                        // ACT
                        Debug.Assert(input.Length == 0);
                        bool actual = adapter.ReadHeader(message, null);

                        // ASSERT
                        Assert.Equal(false, actual);
                        Assert.Equal(MessageReadingState.None, message.ReadingState);
                    }
                }
            }
Beispiel #2
0
        private void Communicate()
        {
            // communicate
            try {
                Communication.Communicate(this);
            } catch (Exception exception) {
                LogError($"Error: {exception.Message}");
                // continue
            } finally {
                LogVerbose("Communication completed.");
                lock (this.instanceLocker) {
                    CloseTcpConnections();
                    this.proxyCredential   = null;
                    this.connectingToProxy = false;
                    this.responseIO        = null;
                    this.requestIO         = null;
                }
            }

            return;
        }
Beispiel #3
0
        public void StartCommunication(TcpClient client)
        {
            // argument checks
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            try {
                // log
                bool verbose = ShouldLog(TraceEventType.Verbose);
                if (verbose)
                {
                    LogVerbose($"Starting for {client.Client.RemoteEndPoint.ToString()} ...");
                }

                lock (this.instanceLocker) {
                    // state checks
                    if (this.owner == null)
                    {
                        throw new ObjectDisposedException(this.ComponentName);
                    }
                    Task communicatingTask = this.Task;
                    if (communicatingTask != null)
                    {
                        throw new InvalidOperationException("It already started communication.");
                    }

                    // prepare a communicating task
                    communicatingTask = new Task(Communicate);
                    communicatingTask.ContinueWith(
                        (t) => {
                        LogVerbose("Stopped.");
                        this.owner.OnConnectionCompleted(this);
                    }
                        );
                    this.Task = communicatingTask;

                    this.ComponentName = $"{ComponentNameBase} <{this.ComponentId}>";
                    Debug.Assert(this.client == null);
                    this.client = client;
                    Stream clientStream = client.GetStream();
                    this.requestIO  = new MessageIO(input: clientStream, output: null);
                    this.responseIO = new MessageIO(input: null, output: clientStream);

                    // start the communicating task
                    communicatingTask.Start();

                    // log
                    if (verbose)
                    {
                        LogVerbose("Started.");
                    }
                }
            } catch (Exception exception) {
                LogError($"Fail to start: {exception.Message}");
                throw;
            }

            return;
        }
Beispiel #4
0
 /// <summary>
 /// Constructor for the Message Repository
 /// </summary>
 public MessageRepository()
 {
     AppMessages = new List <AppMessage>();
     io          = new MessageIO();
 }