Example #1
0
        private void readCallback(IAsyncResult ar)
        {
            try
            {
                netStream.EndRead(ar);
                NetworkMessage netMesasge = new NetworkMessageFormatter <NetworkMessage>().Deserialize(ar.AsyncState as byte[]);
                if (netMesasge != null)
                {
                    ReadCompleted?.Invoke(this, new NetworkMessageReaderReadCompletedEventArgs(netMesasge, TcpClient));
                }
                else
                {
                    ReadError?.Invoke(this, new NetworkMessageReaderReadErrorEventArgs(TcpClient, new ArgumentNullException("NetworkMessage is null")));
                }


                if (readLoop)
                {
                    ReadAsync(readLoop);
                }
            }
            catch (Exception ex)
            {
                ReadError?.Invoke(this, new NetworkMessageReaderReadErrorEventArgs(TcpClient, ex));
            }
        }
        private void ReadThread()
        {
            byte[] dataLength = new byte[4];

            while (running)
            {
                try
                {
                    stream.Read(dataLength, 0, 4);

                    int    readedData = 0, totalData = BitConverter.ToInt32(dataLength, 0);
                    byte[] data = new byte[totalData];

                    do
                    {
                        readedData += stream.Read(data, readedData, totalData - readedData);
                    } while (readedData < totalData);

                    if (data.Length > 0)
                    {
                        Received?.Invoke(serializer.Deserialize(data));
                    }
                }
                catch (Exception ex)
                {
                    ReadError?.Invoke(ex);
                }

                Thread.Sleep(THREAD_WAITING_TIME);
            }
        }
        public void Open(ILogRecordFormat recordFormat)
        {
            var fileName = Properties.Settings.Default.TextFilePath;

            _logger.Info("Открытие файла {0}", fileName);
            _recordFormat   = recordFormat;
            _cts            = new CancellationTokenSource();
            _file           = new StreamReader(fileName);
            _messageCounter = 0;

            var readTask = Task.Run(async() =>
            {
                _logger.Info("Запуск потока чтения файла");

                string line;
                while ((line = _file.ReadLine()) != null)
                {
                    _messageCounter++;
                    DataReceived?.Invoke(this, new ReceiveEventArgs()
                    {
                        Data = Encoding.ASCII.GetBytes(line)
                    });
                    await Task.Delay(10, _cts.Token);
                }
            }, _cts.Token);

            readTask.ContinueWith(_ => ReadError?.Invoke(this, new ErrorEventArgs(_.Exception)), TaskContinuationOptions.OnlyOnFaulted);
        }
Example #4
0
        public void Open(ILogRecordFormat recordFormat)
        {
            _recordFormat   = recordFormat;
            _messageCounter = 0;

            InitReceiver();

            Task.Run(() => Read(), _cts.Token).ContinueWith(_ => ReadError?.Invoke(this, new ErrorEventArgs(_.Exception)), TaskContinuationOptions.OnlyOnFaulted);
        }
        private void Connect(TcpClient client)
        {
            connection = ConnectionFactory.GetConnection(client);

            connection.Open();
            connection.Received   += message => ResponseReceived(message);
            connection.ReadError  += exception => ReadError?.Invoke(exception);
            connection.WriteError += exception => WriteError?.Invoke(exception);
        }
Example #6
0
        public void ReadAsync(bool readLoop = false)
        {
            try
            {
                this.readLoop = readLoop;

                byte[] buffer = new byte[TcpClient.ReceiveBufferSize = NetworkMessage.MAX_DATA_SIZE_IN_BYTES];
                netStream = TcpClient.GetStream();
                netStream.BeginRead(buffer, 0, buffer.Length, readCallback, buffer);
            }
            catch (Exception ex)
            {
                ReadError?.Invoke(this, new NetworkMessageReaderReadErrorEventArgs(TcpClient, ex));
            }
        }
        private void ListenThread()
        {
            listener.Start();

            while (running)
            {
                TcpClient        client           = listener.AcceptTcpClient();
                ClientConnection clientConnection = new ClientConnection(ConnectionFactory.GetConnection(client), new Client(false));

                clientConnection.Connection.Received   += message => MessageReceived(message, clientConnection);
                clientConnection.Connection.ReadError  += exception => ReadError?.Invoke(exception, clientConnection.Connection, clientConnection.Client);
                clientConnection.Connection.WriteError += exception => WriteError?.Invoke(exception, clientConnection.Connection, clientConnection.Client);
                clientConnection.Connection.Open();

                connections.Add(clientConnection);
            }
        }