//private static UTF8Encoding encoding = new UTF8Encoding();

        public PacketDataReceivedEventArgs(int bufferSize, byte[] dataBuffer, DestinationTuple d)
        {
            TotalBytesRead = bufferSize;
            BytesRead      = new byte[bufferSize];
            Array.Copy(dataBuffer, 0, this.BytesRead, 0, bufferSize);
            DataRead = ByteStreamUtil.ByteToHexBit(BytesRead);
            //DataRead = encoding.GetString(BytesRead, 0, bufferSize);
            DestinationTuple = d;
        }
 public void DataSent(object sender, PacketDataSentEventArgs e)
 {
     Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"Send {e.Data.Count()} byte(s) 0x{ByteStreamUtil.ByteToHexBit(e.Data)} from {e.Destination.LocalEndPoint} to {e.Destination.RemoteEndPoint}");
 }
 public void DataSent(object sender, PacketDataSentEventArgs e)
 {
     Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"Send {e.Data.Count()} byte(s) 0x{ByteStreamUtil.ByteToHexBit(e.Data)}");
 }
        private void StartConversation()
        {
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{_clientSocketAddress} Start");
            DateTime utcNow = DateTime.UtcNow;

            _lastReceiveDateTime    = utcNow;
            _currentReceiveDateTime = utcNow;
            Timer t        = new Timer(new TimerCallback(CheckClientCommInterval), null, 15000, 15000);
            int   bytesRec = 0;

            byte[] dataBytes = new byte[4096];

            // An incoming connection needs to be processed.
            while (!_stopClient)
            {
                try
                {
                    if (!IsConnected())
                    {
                        break;
                    }
                    bytesRec = _clientSocket.Receive(dataBytes);
                    _currentReceiveDateTime = DateTime.UtcNow;
                    if (bytesRec > 0)
                    {
                        byte[] shrinkDataBytes = new byte[bytesRec];
                        Array.Copy(dataBytes, shrinkDataBytes, bytesRec);
                        Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{_clientSocketAddress} received {bytesRec} bytes: {ByteStreamUtil.ByteToHexBit(shrinkDataBytes)}");
                        byte[] answer = Encoding.ASCII.GetBytes("<Ack>").Concat(shrinkDataBytes).Concat(Encoding.ASCII.GetBytes("<\\Ack>")).ToArray();
                        SendMessage(answer);
                        Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{_clientSocketAddress} sent {answer.Count()} bytes: {ByteStreamUtil.ByteToHexBit(answer)}");
                    }
                }
                catch (Exception exception)
                {
                    Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, $"{_clientSocketAddress} exception {exception}");
                    _stopClient        = true;
                    _markedForDeletion = true;
                }
            }
            _stopClient        = true;
            _markedForDeletion = true;
            t.Change(Timeout.Infinite, Timeout.Infinite);
            t = null;
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{_clientSocketAddress} End");
        }