예제 #1
0
 private void ConsumeData(ChannelData <byte[]> channelData)
 {
     if (channelData != null && _channelDataReceivedAction != null)
     {
         _channelDataReceivedAction(channelData);
     }
 }
예제 #2
0
        private ChannelData <byte[]> ProduceData()
        {
            ChannelData <byte[]> channelData = null;

            int receivedBytesCount = 0;

            try
            {
                receivedBytesCount = _socket.ReceiveFrom(_buffer, ref _remoteEndPoint);
            }
            catch { }

            if (receivedBytesCount > 0)
            {
                try
                {
                    byte[] receivedBytes = new byte[receivedBytesCount];
                    Buffer.BlockCopy(_buffer, 0, receivedBytes, 0, receivedBytesCount);
                    IPEndPoint currentEndPoint = new IPEndPoint(((IPEndPoint)_remoteEndPoint).Address, ((IPEndPoint)_remoteEndPoint).Port);
                    channelData = new ChannelData <byte[]>(receivedBytes, currentEndPoint, DateTime.Now);
                }
                catch (Exception ex)
                {
                    _logWriter.Log("UdpChannel ProduceData exception", ex);
                }
            }

            return(channelData);
        }
예제 #3
0
 private void NotifyRawDataReceived(byte[] rawData)
 {
     if (_channelDataReceivedAction != null)
     {
         ChannelData <byte[]> channelData = new ChannelData <byte[]>(rawData, _remoteIPEndPoint, DateTime.Now);
         _channelDataReceivedAction(channelData);
     }
 }
예제 #4
0
 public void RawDataReceived(TcpConnectionInfo connection, byte[] data)
 {
     if (_channelDataReceivedAction != null)
     {
         ChannelData <byte[]> channelData = new ChannelData <byte[]>(data, connection.RemoteEndPoint, DateTime.Now);
         _channelDataReceivedAction(channelData);
     }
 }
예제 #5
0
        private void ConnectToHost()
        {
            string url     = _requestUrl;
            string address = _ipHost + ":" + _port;

            while (!_stopRequest)
            {
                try
                {
                    string          responseLine = null;
                    HttpWebResponse response     = null;

                    while (response == null && !_stopRequest)
                    {
                        WebRequest request = WebRequest.Create(url);
                        request.ContentType = "text/plain";
                        request.Timeout     = 10 * 1000; // 10 seconds in milliseconds

                        response = (HttpWebResponse)request.GetResponse();

                        if (response != null)
                        {
                            Task.Factory.StartNew(() => OnConnected(_hostIPEndpoint));

                            using (Stream dataStream = response.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(dataStream, _currentEncoding))
                                {
                                    reader.BaseStream.ReadTimeout = 10 * 1000;
                                    while (!reader.EndOfStream && !_stopRequest)
                                    {
                                        responseLine = reader.ReadLine();
                                        if (responseLine != "" && _channelDataReceivedAction != null)
                                        {
                                            ChannelData <string> channelData = new ChannelData <string>(responseLine, _hostIPEndpoint, DateTime.Now);
                                            _channelDataReceivedAction(channelData);
                                        }
                                    }
                                }
                            }
                            response = null;
                        }

                        Thread.Sleep(_requestIntervalInMS);
                    }
                }
                catch (Exception ex)
                {
                    OnDisconnected(_hostIPEndpoint, ex.Message);

                    _logWriter.Log("HttpReaderChannel ConnectToHost function exception", ex);

                    Thread.Sleep(_exceptionIntervalInMS);
                }
            }

            Task.Factory.StartNew(() => OnDisconnected(_hostIPEndpoint, string.Empty));
        }
예제 #6
0
 public override void SendData(ChannelData <byte[]> channelData)
 {
     try
     {
         if (_ns != null && _ns.CanWrite)
         {
             _ns.Write(channelData.RawData, 0, channelData.RawData.Length);
         }
     }
     catch (Exception ex)
     {
         _logWriter.Log("TcpClient SendData function exception", ex);
     }
 }
예제 #7
0
 public override void SendData(ChannelData <string> channelData)
 {
 }
예제 #8
0
 public override void SendData(ChannelData <byte[]> channelData)
 {
     _TcpListener.Send(channelData.RemoteEndPoint, channelData.RawData);
 }
예제 #9
0
 public abstract void SendData(ChannelData <T> channelData);
예제 #10
0
 public override void SendData(ChannelData <byte[]> channelData)
 {
     _socket.SendTo(channelData.RawData, channelData.RawData.Length, SocketFlags.None, channelData.RemoteEndPoint);
 }