/// <summary>
 /// Geolocate an IP address
 /// </summary>
 /// Identify an IP address Country, State/Provence, City, Zip/Postal Code,
 /// etc.  Useful for security and UX applications.
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='value'>
 /// IP address to geolocate, e.g. "55.55.55.55"
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <PostOKResponse> PostAsync(this IIPAddress operations, string value, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.PostWithHttpMessagesAsync(value, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Parsed the given string representation into a new IIPAddress.
        /// </summary>
        /// <param name="IPAddressString">A string representation of an IP address.</param>
        /// <param name="IPAddress">The parsed IP address.</param>
        public static Boolean TryParse(String IPAddressString, out IIPAddress IPAddress)
        {
            IPv4Address _IPv4Address;

            if (IPAddressString.IndexOf('.') > 0)
            {
                if (IPv4Address.TryParse(IPAddressString, out _IPv4Address))
                {
                    IPAddress = _IPv4Address;
                    return(true);
                }
            }

            IPv6Address _IPv6Address;

            if (IPAddressString.IndexOf(':') > 0)
            {
                if (IPv6Address.TryParse(IPAddressString, out _IPv6Address))
                {
                    IPAddress = _IPv6Address;
                    return(true);
                }
            }

            throw new FormatException("The given string '" + IPAddressString + "' is not a valid IP address!");
        }
Beispiel #3
0
        /// <summary>
        /// Create a new UDP receiver listening on the given IP address and port.
        /// </summary>
        /// <param name="IPAddress">The IP address to listen.</param>
        /// <param name="Port">The port to listen.</param>
        /// <param name="ServiceBanner">Service banner.</param>
        /// <param name="Mapper">A delegate to transform the incoming UDP packets into custom data structures.</param>
        /// <param name="ReceiverThreadName">The optional name of the UDP receiver thread.</param>
        /// <param name="ReceiverThreadPriority">The optional priority of the UDP receiver thread.</param>
        /// <param name="ReceiverThreadIsBackground">Whether the UDP receiver thread is a background thread or not.</param>
        /// <param name="PacketThreadsNameCreator">An optional delegate to set the name of the UDP packet threads.</param>
        /// <param name="PacketThreadsPriority">The optional priority of the UDP packet threads.</param>
        /// <param name="PacketThreadsAreBackground">Whether the UDP packet threads are background threads or not.</param>
        /// <param name="Autostart">Start the UDP receiver thread immediately.</param>
        public UDPReceiver(IIPAddress IPAddress,
                           IPPort Port,
                           String ServiceBanner                  = DefaultServiceBanner,
                           MapperDelegate Mapper                 = null,
                           MapReduceDelegate MapReduce           = null,
                           String ReceiverThreadName             = "UDP receiver thread",
                           ThreadPriority ReceiverThreadPriority = ThreadPriority.AboveNormal,
                           Boolean ReceiverThreadIsBackground    = true,
                           Func <UDPPacket <TData>, String> PacketThreadsNameCreator = null,
                           ThreadPriority PacketThreadsPriority = ThreadPriority.AboveNormal,
                           Boolean PacketThreadsAreBackground   = true,
                           Boolean Autostart = false)

        {
            if (Mapper == null && MapReduce == null)
            {
                throw new ArgumentNullException("The mapper and mapreduce delegate can not be both null!");
            }

            this._IPAddress               = IPAddress;
            this._IsMulticast             = IPAddress.IsMulticast;
            this._Port                    = Port;
            this._IPSocket                = new IPSocket(_IPAddress, _Port);
            this.ServiceBanner            = ServiceBanner;
            this.Mapper                   = Mapper;
            this.MapReduce                = MapReduce;
            this._ReceiverThreadName      = ReceiverThreadName;
            this._ReceiverThreadPriority  = ReceiverThreadPriority;
            this.PacketThreadsNameCreator = (PacketThreadsNameCreator == null)
                                                  ? UDPpacket => "UDP packet from " + UDPpacket.RemoteSocket.IPAddress + ":" + UDPpacket.RemoteSocket.Port
                                                  : PacketThreadsNameCreator;
            this._PacketThreadsPriority      = PacketThreadsPriority;
            this._PacketThreadsAreBackground = PacketThreadsAreBackground;

            var LocalIPEndPoint = new IPEndPoint(new System.Net.IPAddress(_IPAddress.GetBytes()), _Port.ToInt32());

            this.LocalSocket       = new IPSocket(LocalIPEndPoint);
            this.LocalDotNetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.LocalDotNetSocket.Bind(LocalIPEndPoint);

            this.BufferSize     = 65536;
            this.ReceiveTimeout = 5000;

            if (IsMulticast)
            {
                LocalDotNetSocket.SetSocketOption(SocketOptionLevel.IP,
                                                  SocketOptionName.AddMembership,
                                                  new MulticastOption(System.Net.IPAddress.Parse(_IPAddress.ToString()),
                                                                      System.Net.IPAddress.Any));
            }

            this.CancellationTokenSource = new CancellationTokenSource();
            this.CancellationToken       = CancellationTokenSource.Token;

            if (Autostart)
            {
                Start();
            }
        }
Beispiel #4
0
        /// <summary>Determines whether a string is a valid IP address.</summary>
        /// <returns>true if <paramref name="ipString" /> is a valid IP address; otherwise, false.</returns>
        /// <param name="ipString">The string to validate.</param>
        /// <param name="address">The <see cref="T:System.Net.IPAddress" /> version of the string.</param>
        public static bool TryParse([NotNull] string ipString, [CanBeNull] out IIPAddress address)
        {
            var parsed = IPAddress.TryParse(ipString, out var tempAddress);

            address = tempAddress.ToInterface();

            return(parsed);
        }
Beispiel #5
0
        /// <summary>
        /// Create a new DNS resolver client.
        /// </summary>
        /// <param name="DNSServer">The DNS server to query.</param>
        public DNSClient(IIPAddress DNSServer)

            : this(new IPSocket[1] {
            new IPSocket(DNSServer, new IPPort(53))
        })

        {
        }
Beispiel #6
0
        /// <summary>
        /// Create a new DNS resolver client.
        /// </summary>
        /// <param name="DNSServer">The DNS server to query.</param>
        /// <param name="Port">The IP port of the DNS server to query.</param>
        public DNSClient(IIPAddress DNSServer, IPPort Port)

            : this(new IPSocket[1] {
            new IPSocket(DNSServer, Port)
        })

        {
        }
        /// <summary>Determines whether a string is a valid IP address.</summary>
        /// <returns>true if <paramref name="ipString" /> is a valid IP address; otherwise, false.</returns>
        /// <param name="ipString">The string to validate.</param>
        /// <param name="address">The <see cref="T:System.Net.IPAddress" /> version of the string.</param>
        public static bool TryParse(string ipString, out IIPAddress address)
        {
            IPAddress tempAddress;
            var       parsed = IPAddress.TryParse(ipString, out tempAddress);

            address = tempAddress.ToInterface();

            return(parsed);
        }
Beispiel #8
0
        public static void SendTo(this UDPClient UDPClient, String UDPPacketString, IIPAddress RemoteIPAddress, IPPort IPPort, Encoding Encoding = null, SocketFlags SocketFlags = SocketFlags.None)
        {
            if (Encoding == null)
                Encoding = Encoding.UTF8;

            var UDPPacketData = Encoding.GetBytes(UDPPacketString);
            var RemoteIPEndPoint = new IPEndPoint(new IPAddress(RemoteIPAddress.GetBytes()), IPPort.ToInt32());

            UDPClient.SendTo(UDPPacketData, RemoteIPEndPoint, SocketFlags);
        }
 /// <summary>
 /// Start a new TCP/HTTP/REST based graph server.
 /// </summary>
 /// <param name="Graph">A graph.</param>
 /// <param name="IIPAddress">The listening IP address(es).</param>
 /// <param name="Port">The listening port.</param>
 /// <param name="Autostart">Autostart the server.</param>
 public static IBifrostHTTPServer StartHTTPServer(this IGenericPropertyGraph<String, Int64, String, String, Object,
                                                                       String, Int64, String, String, Object,
                                                                       String, Int64, String, String, Object,
                                                                       String, Int64, String, String, Object> Graph,
                                            IIPAddress IIPAddress,
                                            IPPort     Port,
                                            Boolean    Autostart = false)
 {
     var Server = new BifrostHTTPServer(IIPAddress, Port, Autostart);
     Server.AddGraph(Graph);
     return Server;
 }
Beispiel #10
0
 /// <summary>
 /// Create a new HTTPClient using the given optional parameters.
 /// </summary>
 /// <param name="RemoteIPAddress">The remote IP address to connect to.</param>
 /// <param name="RemoteCertificateValidator">A delegate to verify the remote TLS certificate.</param>
 /// <param name="ClientCert">The TLS client certificate to use.</param>
 /// <param name="RemotePort">An optional remote IP port to connect to [default: 443].</param>
 /// <param name="DNSClient">An optional DNS client.</param>
 public HTTPSClient(IIPAddress                           RemoteIPAddress,
                    RemoteCertificateValidationCallback  RemoteCertificateValidator,
                    X509Certificate                      ClientCert  = null,
                    IPPort                               RemotePort  = null,
                    DNSClient                            DNSClient   = null)
     : base(RemoteIPAddress,
            RemotePort != null ? RemotePort : IPPort.Parse(443),
            RemoteCertificateValidator,
            ClientCert,
            DNSClient)
 {
 }
Beispiel #11
0
        /// <summary>
        /// Create a new HTTPClient using the given optional parameters.
        /// </summary>
        /// <param name="RemoteIPAddress">The remote IP address to connect to.</param>
        /// <param name="RemoteCertificateValidator">A delegate to verify the remote TLS certificate.</param>
        /// <param name="ClientCert">The TLS client certificate to use.</param>
        /// <param name="RemotePort">An optional remote IP port to connect to [default: 443].</param>
        /// <param name="DNSClient">An optional DNS client.</param>
        public HTTPSClient(IIPAddress RemoteIPAddress,
                           RemoteCertificateValidationCallback RemoteCertificateValidator,
                           X509Certificate ClientCert = null,
                           IPPort RemotePort          = null,
                           DNSClient DNSClient        = null)

            : base(RemoteIPAddress,
                   RemotePort != null ? RemotePort : IPPort.Parse(443),
                   RemoteCertificateValidator,
                   ClientCert,
                   DNSClient)

        {
        }
Beispiel #12
0
        //    public LocalCertificateSelectionCallback ClientCertificateSelector { get; set; }

        #endregion

        #region Events


        #endregion

        #region Constructor(s)

        #region HTTPClient(RemoteIPAddress, RemotePort, RemoteCertificateValidator = null, ClientCert = null, DNSClient = null)

        /// <summary>
        /// Create a new HTTPClient using the given optional parameters.
        /// </summary>
        /// <param name="RemoteIPAddress">The remote IP address to connect to.</param>
        /// <param name="RemotePort">The remote IP port to connect to.</param>
        /// <param name="RemoteCertificateValidator">A delegate to verify the remote TLS certificate.</param>
        /// <param name="ClientCert">The TLS client certificate to use.</param>
        /// <param name="DNSClient">An optional DNS client.</param>
        public HTTPClient(IIPAddress RemoteIPAddress,
                          IPPort RemotePort,
                          RemoteCertificateValidationCallback RemoteCertificateValidator = null,
                          X509Certificate ClientCert = null,
                          DNSClient DNSClient        = null)
        {
            this.RemoteIPAddress            = RemoteIPAddress;
            this.Hostname                   = RemoteIPAddress.ToString();
            this.RemotePort                 = RemotePort;
            this.RemoteCertificateValidator = RemoteCertificateValidator;
            this.ClientCert                 = ClientCert;
            this.DNSClient                  = DNSClient == null
                                                   ? new DNSClient()
                                                   : DNSClient;
        }
Beispiel #13
0
        /// <summary>
        /// Initialize the TCP server using the given parameters.
        /// </summary>
        /// <param name="IIPAddress">The listening IP address(es)</param>
        /// <param name="Port">The listening port</param>
        /// <param name="ServiceBanner">Service banner.</param>
        /// <param name="SplitCharacters">An enumeration of delimiters to split the incoming CSV line into individual elements.</param>
        /// <param name="ServerThreadName">The optional name of the TCP server thread.</param>
        /// <param name="ServerThreadPriority">The optional priority of the TCP server thread.</param>
        /// <param name="ServerThreadIsBackground">Whether the TCP server thread is a background thread or not.</param>
        /// <param name="ConnectionIdBuilder">An optional delegate to build a connection identification based on IP socket information.</param>
        /// <param name="ConnectionThreadsNameBuilder">An optional delegate to set the name of the TCP connection threads.</param>
        /// <param name="ConnectionThreadsPriorityBuilder">An optional delegate to set the priority of the TCP connection threads.</param>
        /// <param name="ConnectionThreadsAreBackground">Whether the TCP connection threads are background threads or not (default: yes).</param>
        /// <param name="ConnectionTimeout">The TCP client timeout for all incoming client connections in seconds (default: 30 sec).</param>
        /// <param name="MaxClientConnections">The maximum number of concurrent TCP client connections (default: 4096).</param>
        /// <param name="Autostart">Start the TCP/CSV server thread immediately (default: no).</param>
        public TCPCSVServer(IIPAddress IIPAddress,
                            IPPort Port,
                            String ServiceBanner = __DefaultServiceBanner,
                            IEnumerable <Char> SplitCharacters      = null,
                            String ServerThreadName                 = null,
                            ThreadPriority ServerThreadPriority     = ThreadPriority.AboveNormal,
                            Boolean ServerThreadIsBackground        = true,
                            ConnectionIdBuilder ConnectionIdBuilder = null,
                            ConnectionThreadsNameBuilder ConnectionThreadsNameBuilder         = null,
                            ConnectionThreadsPriorityBuilder ConnectionThreadsPriorityBuilder = null,
                            Boolean ConnectionThreadsAreBackground = true,
                            TimeSpan?ConnectionTimeout             = null,
                            UInt32 MaxClientConnections            = __DefaultMaxClientConnections,
                            Boolean Autostart = false)

            : base(IIPAddress,
                   Port,
                   ServiceBanner,
                   ServerThreadName,
                   ServerThreadPriority,
                   ServerThreadIsBackground,
                   ConnectionIdBuilder,
                   ConnectionThreadsNameBuilder,
                   ConnectionThreadsPriorityBuilder,
                   ConnectionThreadsAreBackground,
                   ConnectionTimeout,
                   MaxClientConnections,
                   false)

        {
            this.ServiceBanner   = ServiceBanner;
            this.SplitCharacters = SplitCharacters != null?SplitCharacters.ToArray() : DefaultSpitCharacters;

            this._TCPCSVProcessor = new TCPCSVProcessor(this.SplitCharacters);
            this.SendTo(_TCPCSVProcessor);
            //  this.OnNewConnection         += (TCPServer, Timestamp, RemoteSocket, ConnectionId, TCPConnection) => SendNewConnection   (Timestamp, RemoteSocket, ConnectionId, TCPConnection);
            //  this.OnConnectionClosed      += (TCPServer, Timestamp, RemoteSocket, ConnectionId, ClosedBy)      => SendConnectionClosed(Timestamp, RemoteSocket, ConnectionId, ClosedBy);

            this._TCPCSVCommandProcessor = new TCPCSVCommandProcessor();
            this._TCPCSVProcessor.ConnectTo(_TCPCSVCommandProcessor);
            this._TCPCSVCommandProcessor.OnNotification += ProcessBoomerang;

            if (Autostart)
            {
                Start();
            }
        }
Beispiel #14
0
        /// <summary>
        /// Compares two instances of this object.
        /// </summary>
        /// <param name="Object">An object to compare with.</param>
        /// <returns>true|false</returns>
        public Int32 CompareTo(IIPAddress IIPAddress)
        {
            if (IIPAddress == null)
            {
                throw new ArgumentNullException("The given IIPAddress must not be null!");
            }

            // Check if the given object can be casted to an IPv4Address
            var _IPv4Address = IIPAddress as IPv4Address;

            if ((Object)_IPv4Address == null)
            {
                throw new ArgumentException("The given IIPAddress is not an IPv4Address!");
            }

            return(this.CompareTo(_IPv4Address));
        }
Beispiel #15
0
        private Socket CreateAndConnectTCPSocket(IIPAddress _IPAddress, IPPort Port)
        {
            Socket _TCPSocket = null;

            if (_IPAddress is IPv4Address)
            {
                _TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }

            else if (_IPAddress is IPv6Address)
            {
                _TCPSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }

            _TCPSocket.Connect(IPAddress.Parse(_IPAddress.ToString()), Port.ToUInt16());

            return(_TCPSocket);
        }
Beispiel #16
0
 public IOTDevice(CLI CLI,
                  SIM_Id?SIMNumber,
                  String SOC,
                  String InternalSOC,
                  String Description,
                  IIPAddress IPAddress,
                  DateTime?StartDate,
                  DateTime?EndDate,
                  String FriendlyName,
                  String Hints,
                  String ProvisioningMetaData,
                  Byte?Status,
                  Byte?Version,
                  Byte?ActorStatus,
                  Byte?NetworkStatus,
                  Boolean?Enabled,
                  Boolean?LockIMEI,
                  String IMEI,
                  DateTime?LastSync,
                  Last_Connected?LastConnected_Start)
 {
     this.CLI                  = CLI;
     this.SIMNumber            = SIMNumber;
     this.SOC                  = SOC;
     this.InternalSOC          = InternalSOC;
     this.Description          = Description;
     this.IPAddress            = IPAddress;
     this.StartDate            = StartDate;
     this.EndDate              = EndDate;
     this.FriendlyName         = FriendlyName;
     this.Hints                = Hints;
     this.ProvisioningMetaData = ProvisioningMetaData;
     this.Status               = Status;
     this.Version              = Version;
     this.ActorStatus          = ActorStatus;
     this.NetworkStatus        = NetworkStatus;
     this.Enabled              = Enabled;
     this.LockIMEI             = LockIMEI;
     this.IMEI                 = IMEI;
     this.LastSync             = LastSync;
     this.LastConnected        = LastConnected;
 }
Beispiel #17
0
        /// <summary>
        /// Create a new UDP/CSV receiver listening on the given IP address and port.
        /// </summary>
        /// <param name="IPAddress">The IP address to listen.</param>
        /// <param name="Port">The port to listen.</param>
        /// <param name="ServiceBanner">Service banner.</param>
        /// <param name="Splitter">An array of delimiters to split the incoming CSV line into individual elements.</param>
        /// <param name="ReceiverThreadName">The optional name of the UDP receiver thread.</param>
        /// <param name="ReceiverThreadPriority">The optional priority of the UDP receiver thread.</param>
        /// <param name="ReceiverThreadIsBackground">Whether the UDP receiver thread is a background thread or not.</param>
        /// <param name="PacketThreadsNameCreator">An optional delegate to set the name of the UDP packet threads.</param>
        /// <param name="PacketThreadsPriority">The optional priority of the UDP packet threads.</param>
        /// <param name="PacketThreadsAreBackground">Whether the UDP packet threads are background threads or not.</param>
        /// <param name="Autostart">Start the UDP receiver thread immediately.</param>
        public UDPCSVReceiver(IIPAddress IPAddress,
                              IPPort Port,
                              String ServiceBanner                  = DefaultServiceBanner,
                              IEnumerable <String> Splitter         = null,
                              String ReceiverThreadName             = "UDP receiver thread",
                              ThreadPriority ReceiverThreadPriority = ThreadPriority.AboveNormal,
                              Boolean ReceiverThreadIsBackground    = true,
                              Func <UDPPacket <IEnumerable <String> >, String> PacketThreadsNameCreator = null,
                              ThreadPriority PacketThreadsPriority = ThreadPriority.AboveNormal,
                              Boolean PacketThreadsAreBackground   = true,
                              Boolean Autostart = false)

            : base(IPAddress,
                   Port,
                   ServiceBanner,

                   // Mapper delegate <= do not use!
                   null,

                   // MapReduce delegate <= will automatically be reduced to multiple events!
                   (UDPReceiver, Timestamp, LocalSocket, RemoteSocket, Message) =>
                   Message.ToUTF8String().
                   Trim().
                   Split(LineEndings,
                         StringSplitOptions.RemoveEmptyEntries).
                   Select(CSVLine => CSVLine.Trim().
                          Split((Splitter != null) ? Splitter.ToArray() : DefaultSplitter,
                                StringSplitOptions.None).
                          Select(CSVElement => CSVElement.Trim())),

                   ReceiverThreadName,
                   ReceiverThreadPriority,
                   ReceiverThreadIsBackground,
                   PacketThreadsNameCreator,
                   PacketThreadsPriority,
                   PacketThreadsAreBackground,
                   Autostart)

        {
            this._Splitter = (Splitter != null) ? Splitter.ToArray() : DefaultSplitter;
        }
Beispiel #18
0
        /// <summary>
        /// Compares two instances of this object.
        /// </summary>
        /// <param name="IIPAddress">An IIPAddress.</param>
        /// <returns>true|false</returns>
        public Boolean Equals(IIPAddress IIPAddress)
        {
            if ((Object)IIPAddress == null)
            {
                throw new ArgumentNullException("The given IIPAddress must not be null!");
            }

            if (_Length != IIPAddress.Length)
            {
                return(false);
            }

            // Check if the given IIPAddress can be casted to an IPv4Address
            var _IPv4Address = IIPAddress as IPv4Address;

            if ((Object)_IPv4Address == null)
            {
                return(false);
            }

            return(this.Equals(_IPv4Address));
        }
Beispiel #19
0
        /// <summary>
        /// Parsed the given string representation into a new IIPAddress.
        /// </summary>
        /// <param name="IPAddressString">A string representation of an IP address.</param>
        /// <param name="IPAddress">The parsed IP address.</param>
        public static Boolean TryParse(String IPAddressString, out IIPAddress IPAddress)
        {
            IPv4Address _IPv4Address;

            if (IPAddressString.IndexOf('.') > 0)
                if (IPv4Address.TryParse(IPAddressString, out _IPv4Address))
                {
                    IPAddress = _IPv4Address;
                    return true;
                }

            IPv6Address _IPv6Address;

            if (IPAddressString.IndexOf(':') > 0)
                if (IPv6Address.TryParse(IPAddressString, out _IPv6Address))
                {
                    IPAddress = _IPv6Address;
                    return true;
                }

            throw new FormatException("The given string '" + IPAddressString + "' is not a valid IP address!");
        }
 /// <inheritdoc />
 public void JoinMulticastGroup(IIPAddress multicastAddr)
 {
     _client.JoinMulticastGroup(multicastAddr.ToImplementation());
 }
 /// <inheritdoc />
 public void JoinMulticastGroup(int ifindex, IIPAddress multicastAddr)
 {
     _client.JoinMulticastGroup(ifindex, multicastAddr.ToImplementation());
 }
 /// <inheritdoc />
 public void DropMulticastGroup(IIPAddress multicastAddr, int ifindex)
 {
     _client.DropMulticastGroup(multicastAddr.ToImplementation(), ifindex);
 }
Beispiel #23
0
        /// <summary>
        /// Initialize the TCP server using the given parameters.
        /// </summary>
        /// <param name="IIPAddress">The listening IP address(es)</param>
        /// <param name="Port">The listening port</param>
        /// <param name="ServiceBanner">Service banner.</param>
        /// <param name="ServerThreadName">The optional name of the TCP server thread.</param>
        /// <param name="ServerThreadPriority">The optional priority of the TCP server thread.</param>
        /// <param name="ServerThreadIsBackground">Whether the TCP server thread is a background thread or not.</param>
        /// <param name="ConnectionIdBuilder">An optional delegate to build a connection identification based on IP socket information.</param>
        /// <param name="ConnectionThreadsNameBuilder">An optional delegate to set the name of the TCP connection threads.</param>
        /// <param name="ConnectionThreadsPriorityBuilder">An optional delegate to set the priority of the TCP connection threads.</param>
        /// <param name="ConnectionThreadsAreBackground">Whether the TCP connection threads are background threads or not (default: yes).</param>
        /// <param name="ConnectionTimeout">The TCP client timeout for all incoming client connections in seconds (default: 30 sec).</param>
        /// <param name="MaxClientConnections">The maximum number of concurrent TCP client connections (default: 4096).</param>
        /// <param name="Autostart">Start the TCP server thread immediately (default: no).</param>
        public TCPServer(IIPAddress IIPAddress,
                         IPPort Port,
                         String ServiceBanner                    = __DefaultServiceBanner,
                         String ServerThreadName                 = null,
                         ThreadPriority ServerThreadPriority     = ThreadPriority.AboveNormal,
                         Boolean ServerThreadIsBackground        = true,
                         ConnectionIdBuilder ConnectionIdBuilder = null,
                         ConnectionThreadsNameBuilder ConnectionThreadsNameBuilder         = null,
                         ConnectionThreadsPriorityBuilder ConnectionThreadsPriorityBuilder = null,
                         Boolean ConnectionThreadsAreBackground = true,
                         TimeSpan?ConnectionTimeout             = null,
                         UInt32 MaxClientConnections            = __DefaultMaxClientConnections,
                         Boolean Autostart = false)

        {
            #region TCP Socket

            this._IPAddress   = IIPAddress;
            this._Port        = Port;
            this._IPSocket    = new IPSocket(_IPAddress, _Port);
            this._TCPListener = new TcpListener(new System.Net.IPAddress(_IPAddress.GetBytes()), _Port.ToInt32());

            #endregion

            #region TCP Server

            this._ServiceBanner = (ServiceBanner.IsNotNullOrEmpty())
                                                          ? ServiceBanner
                                                          : __DefaultServiceBanner;

            this.ServerThreadName = (ServerThreadName != null)
                                                          ? ServerThreadName
                                                          : __DefaultServerThreadName + this.IPSocket.ToString();

            this.ServerThreadPriority     = ServerThreadPriority;
            this.ServerThreadIsBackground = ServerThreadIsBackground;

            #endregion

            #region TCP Connections

            this._TCPConnections = new ConcurrentDictionary <IPSocket, TCPConnection>();


            this.ConnectionIdBuilder = (ConnectionIdBuilder != null)
                                                          ? ConnectionIdBuilder
                                                          : (Sender, Timestamp, LocalSocket, RemoteIPSocket) => "TCP:" + RemoteIPSocket.IPAddress + ":" + RemoteIPSocket.Port;

            this.ConnectionThreadsNameBuilder = (ConnectionThreadsNameBuilder != null)
                                                          ? ConnectionThreadsNameBuilder
                                                          : (Sender, Timestamp, LocalSocket, RemoteIPSocket) => "TCP thread " + RemoteIPSocket.IPAddress + ":" + RemoteIPSocket.Port;

            this.ConnectionThreadsPriorityBuilder = (ConnectionThreadsPriorityBuilder != null)
                                                          ? ConnectionThreadsPriorityBuilder
                                                          : (Sender, Timestamp, LocalSocket, RemoteIPSocket) => ThreadPriority.AboveNormal;

            this.ConnectionThreadsAreBackground = ConnectionThreadsAreBackground;

            this._ConnectionTimeout = ConnectionTimeout.HasValue
                                                          ? ConnectionTimeout.Value
                                                          : TimeSpan.FromSeconds(30);

            this._MaxClientConnections = MaxClientConnections;

            #endregion

            #region TCP Listener Thread

            this.CancellationTokenSource = new CancellationTokenSource();
            this.CancellationToken       = CancellationTokenSource.Token;

            _ListenerThread = new Thread(() => {
#if __MonoCS__
                // Code for Mono C# compiler
#else
                Thread.CurrentThread.Name         = this.ServerThreadName;
                Thread.CurrentThread.Priority     = this.ServerThreadPriority;
                Thread.CurrentThread.IsBackground = this.ServerThreadIsBackground;
#endif

                #region SetSocketOptions

                // IOControlCode.*

                // fd.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, tcpKeepalive);

                // bytes.PutInteger(endian, tcpKeepalive,      0);
                // bytes.PutInteger(endian, tcpKeepaliveIdle,  4);
                // bytes.PutInteger(endian, tcpKeepaliveIntvl, 8);

                // fd.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null);

                #endregion

                try
                {
                    _IsRunning = true;

                    while (!_StopRequested)
                    {
                        // Wait for a new/pending client connection
                        while (!_StopRequested && !_TCPListener.Pending())
                        {
                            Thread.Sleep(5);
                        }

                        // Break when a server stop was requested
                        if (_StopRequested)
                        {
                            break;
                        }

                        // Processing the pending client connection within its own task
                        var NewTCPClient = _TCPListener.AcceptTcpClient();
                        //  var NewTCPConnection = _TCPListener.AcceptTcpClientAsync().
                        //                                      ContinueWith(a => new TCPConnection(this, a.Result));
                        //                                  //    ConfigureAwait(false);

                        // Store the new connection
                        //_SocketConnections.AddOrUpdate(_TCPConnection.Value.RemoteSocket,
                        //                               _TCPConnection.Value,
                        //                               (RemoteEndPoint, TCPConnection) => TCPConnection);

                        Task.Factory.StartNew(Tuple => {
                            try
                            {
                                var _Tuple = Tuple as Tuple <TCPServer, TcpClient>;

                                var NewTCPConnection = new ThreadLocal <TCPConnection>(
                                    () => new TCPConnection(_Tuple.Item1, _Tuple.Item2)
                                    );

                                #region Copy ExceptionOccured event handlers

                                //foreach (var ExceptionOccuredHandler in MyEventStorage)
                                //    _TCPConnection.Value.OnExceptionOccured += ExceptionOccuredHandler;

                                #endregion

                                #region OnNewConnection

                                // If this event closes the TCP connection the OnNotification event will never be fired!
                                // Therefore you can use this event for filtering connection initiation requests.
                                OnNewConnection?.Invoke(NewTCPConnection.Value.TCPServer,
                                                        NewTCPConnection.Value.ServerTimestamp,
                                                        NewTCPConnection.Value.RemoteSocket,
                                                        NewTCPConnection.Value.ConnectionId,
                                                        NewTCPConnection.Value);

                                if (!NewTCPConnection.Value.IsClosed)
                                {
                                    OnNotification?.Invoke(NewTCPConnection.Value);
                                }

                                #endregion
                            }
                            catch (Exception e)
                            {
                                while (e.InnerException != null)
                                {
                                    e = e.InnerException;
                                }

                                OnExceptionOccured?.Invoke(this, DateTime.Now, e);
                                Console.WriteLine(DateTime.Now + " " + e.Message + Environment.NewLine + e.StackTrace);
                            }
                        }, new Tuple <TCPServer, TcpClient>(this, NewTCPClient));
                    }

                    #region Shutdown

                    // Request all client connections to finish!
                    foreach (var _SocketConnection in _TCPConnections)
                    {
                        _SocketConnection.Value.StopRequested = true;
                    }

                    // After stopping the TCPListener wait for
                    // all client connections to finish!
                    while (_TCPConnections.Count > 0)
                    {
                        Thread.Sleep(5);
                    }

                    #endregion
                }

                #region Exception handling

                catch (Exception Exception)
                {
                    var OnExceptionLocal = OnExceptionOccured;
                    if (OnExceptionLocal != null)
                    {
                        OnExceptionLocal(this, DateTime.Now, Exception);
                    }
                }

                #endregion

                _IsRunning = false;
            });

            #endregion

            if (Autostart)
            {
                Start();
            }
        }
 /// <summary>
 /// Geolocate an IP address
 /// </summary>
 /// Identify an IP address Country, State/Provence, City, Zip/Postal Code,
 /// etc.  Useful for security and UX applications.
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='value'>
 /// IP address to geolocate, e.g. "55.55.55.55"
 /// </param>
 public static PostOKResponse Post(this IIPAddress operations, string value)
 {
     return(Task.Factory.StartNew(s => ((IIPAddress)s).PostAsync(value), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
 /// <inheritdoc />
 public Task ConnectAsync(IIPAddress address, int port)
 {
     return(_client.ConnectAsync(address.ToImplementation(), port));
 }
Beispiel #26
0
 /// <summary>
 /// Create a new UDP sender.
 /// </summary>
 /// <param name="IPAddress">The IP address to send the UDP data.</param>
 /// <param name="Port">The IP port to send the UDP data.</param>
 public UDPSender(IIPAddress IPAddress, IPPort Port)
     : base(Message => Message, IPAddress, Port)
 {
 }
 /// <inheritdoc />
 public void JoinMulticastGroup(IIPAddress multicastAddr, int timeToLive)
 {
     _client.JoinMulticastGroup(multicastAddr.ToImplementation(), timeToLive);
 }
Beispiel #28
0
 /// <summary>
 /// Generates a new IPSocket based on the given IPAddress and IPPort.
 /// </summary>
 /// <param name="IPAddress">The IPAdress of the socket.</param>
 /// <param name="Port">The port of the socket.</param>
 public IPSocket(IIPAddress IPAddress,
                 IPPort Port)
 {
     this.IPAddress = IPAddress;
     this.Port      = Port;
 }
Beispiel #29
0
        //    public LocalCertificateSelectionCallback ClientCertificateSelector { get; set; }

        #endregion

        #region Events


        #endregion

        #region Constructor(s)

        #region HTTPClient(RemoteIPAddress, RemotePort, RemoteCertificateValidator = null, ClientCert = null, DNSClient = null)

        /// <summary>
        /// Create a new HTTPClient using the given optional parameters.
        /// </summary>
        /// <param name="RemoteIPAddress">The remote IP address to connect to.</param>
        /// <param name="RemotePort">The remote IP port to connect to.</param>
        /// <param name="RemoteCertificateValidator">A delegate to verify the remote TLS certificate.</param>
        /// <param name="ClientCert">The TLS client certificate to use.</param>
        /// <param name="DNSClient">An optional DNS client.</param>
        public HTTPClient(IIPAddress                           RemoteIPAddress,
                          IPPort                               RemotePort,
                          RemoteCertificateValidationCallback  RemoteCertificateValidator  = null,
                          X509Certificate                      ClientCert                  = null,
                          DNSClient                            DNSClient                   = null)
        {

            this.RemoteIPAddress             = RemoteIPAddress;
            this.Hostname                    = RemoteIPAddress.ToString();
            this.RemotePort                  = RemotePort;
            this.RemoteCertificateValidator  = RemoteCertificateValidator;
            this.ClientCert                  = ClientCert;
            this.DNSClient                   = DNSClient == null
                                                   ? new DNSClient()
                                                   : DNSClient;

        }
Beispiel #30
0
        private Socket CreateAndConnectTCPSocket(IIPAddress _IPAddress, IPPort Port)
        {
            Socket _TCPSocket = null;

            if (_IPAddress is IPv4Address)
                _TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            else if (_IPAddress is IPv6Address)
                _TCPSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

            _TCPSocket.Connect(IPAddress.Parse(_IPAddress.ToString()), Port.ToUInt16());

            return _TCPSocket;
        }
Beispiel #31
0
 /// <summary>Determines whether a string is a valid IP address.</summary>
 /// <returns>true if <paramref name="ipString" /> is a valid IP address; otherwise, false.</returns>
 /// <param name="ipString">The string to validate.</param>
 /// <param name="address">The <see cref="T:System.Net.IPAddress" /> version of the string.</param>
 public bool TryParse(string ipString, out IIPAddress address)
 {
     return(IPAddressAdapter.TryParse(ipString, out address));
 }
 /// <inheritdoc />
 public void JoinMulticastGroup(IIPAddress multicastAddr, IIPAddress localAddress)
 {
     _client.JoinMulticastGroup(multicastAddr.ToImplementation(), localAddress.ToImplementation());
 }
Beispiel #33
0
 /// <summary>
 /// Create a new HTTPClient using the given optional parameters.
 /// </summary>
 /// <param name="RemoteIPAddress">The IP address to connect to.</param>
 /// <param name="RemotePort">The IP port to connect to.</param>
 public HTTPClient(IIPAddress RemoteIPAddress = null, IPPort RemotePort = null)
 {
     this.RemoteIPAddress = RemoteIPAddress;
     this.RemotePort      = RemotePort;
 }
Beispiel #34
0
 /// <inheritdoc />
 public void Connect(IIPAddress address, int port)
 {
     _socket.Connect(address.ToImplementation(), port);
 }
 /// <summary>
 /// Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.
 /// </summary>
 /// <param name="localaddr">An IPAddress that represents the local IP address.</param>
 /// <param name="port">The port on which to listen for incoming connection attempts.</param>
 public TcpListenerAdapter(IIPAddress localaddr, int port)
     : this(new TcpListener(localaddr.ToImplementation(), port))
 {
 }
Beispiel #36
0
        /// <summary>
        /// Create a new http request header based on the given string representation.
        /// </summary>
        /// <param name="HTTPHeader">A valid string representation of a http request header.</param>
        public HTTPRequest(IIPAddress RemoteHost, IPPort RemotePort, String HTTPHeader)
        {
            this.RemoteHost = RemoteHost;
            this.RemotePort = RemotePort;

            if (!ParseHeader(HTTPHeader))
                return;

            #region Parse HTTPMethod (first line of the http request)

            var _HTTPMethodHeader = FirstPDULine.Split(_SpaceSeperator, StringSplitOptions.RemoveEmptyEntries);

            // e.g: PROPFIND /file/file Name HTTP/1.1
            if (_HTTPMethodHeader.Length != 3)
            {
                this.HTTPStatusCode = HTTPStatusCode.BadRequest;
                return;
            }

            // Parse HTTP method
            // Propably not usefull to define here, as we can not send a response having an "Allow-header" here!
            HTTPMethod _HTTPMethod = null;
            if (!HTTPMethod.TryParseString(_HTTPMethodHeader[0], out _HTTPMethod))
            {
                this.HTTPStatusCode = HTTPStatusCode.MethodNotAllowed;
                return;
            }

            this.HTTPMethod = _HTTPMethod;

            #endregion

            #region Parse URL and QueryString (first line of the http request)

            var RawUrl     = _HTTPMethodHeader[1];
            var _ParsedURL = RawUrl.Split(_URLSeperator, 2, StringSplitOptions.RemoveEmptyEntries);
            UrlPath        = _ParsedURL[0];

            if (UrlPath == "" || UrlPath == null)
                UrlPath = "/";

            // Parse QueryString after '?'
            if (RawUrl.IndexOf('?') > -1)
            {
                //var a = HttpUtility.ParseQueryString(_ParsedURL[1]);
                //foreach (var b in a.AllKeys)
                //    QueryString.Add(b, a[b]);
                this.QueryString = new QueryString(_ParsedURL[1]);
            }

            #endregion

            #region Parse protocol name and -version (first line of the http request)

            var _ProtocolArray  = _HTTPMethodHeader[2].Split(_SlashSeperator, 2, StringSplitOptions.RemoveEmptyEntries);
            ProtocolName        = _ProtocolArray[0].ToUpper();

            if (ProtocolName.ToUpper() != "HTTP")
            {
                this.HTTPStatusCode = HTTPStatusCode.InternalServerError;
                return;
            }

            HTTPVersion _HTTPVersion = null;
            if (HTTPVersion.TryParseVersionString(_ProtocolArray[1], out _HTTPVersion))
                ProtocolVersion = _HTTPVersion;
            if (ProtocolVersion != HTTPVersion.HTTP_1_1)
            {
                this.HTTPStatusCode = HTTPStatusCode.HTTPVersionNotSupported;
                return;
            }

            #endregion

            if (!HeaderFields.ContainsKey("Host"))
                HeaderFields.Add("Host", "*");

            this.HTTPStatusCode = HTTPStatusCode.OK;
        }
 /// <summary>
 /// Establishes a connection to a remote host. The host is specified by an IP address and a port number.
 /// </summary>
 /// <param name="socket">The socket to perform the connect operation on.</param>
 /// <param name="address">The IP address of the remote host.</param>
 /// <param name="port">The port number of the remote host.</param>
 /// <returns>An asynchronous Task.</returns>
 public static Task ConnectAsync(this ISocket socket, IIPAddress address, int port)
 {
     return(socket.ToImplementation().ConnectAsync(address.ToImplementation(), port));
 }
Beispiel #38
0
        /// <summary>
        /// Execute the given HTTP request and return its result.
        /// </summary>
        /// <param name="Request">A HTTP request.</param>
        /// <param name="RequestLogDelegate">A delegate for logging the HTTP request.</param>
        /// <param name="ResponseLogDelegate">A delegate for logging the HTTP request/response.</param>
        /// <param name="Timeout">An optional timeout.</param>
        /// <param name="CancellationToken">A cancellation token.</param>
        public async Task <HTTPResponse> Execute(HTTPRequest Request,
                                                 ClientRequestLogHandler RequestLogDelegate   = null,
                                                 ClientResponseLogHandler ResponseLogDelegate = null,
                                                 TimeSpan?Timeout = null,
                                                 CancellationToken?CancellationToken = null)
        {
            #region Call the optional HTTP request log delegate

            try
            {
                RequestLogDelegate?.Invoke(DateTime.Now, this, Request);
            }
            catch (Exception e)
            {
                e.Log(nameof(HTTPClient) + "." + nameof(RequestLogDelegate));
            }

            #endregion

            var task = Task <HTTPResponse> .Factory.StartNew(() => {
                HTTPResponse Response = null;

                try
                {
                    if (Environment.MachineName.Contains("QUAD2QUANTOR") && Request.URI.Contains("eRoaming"))
                    {
                        throw new Exception("Catch me if you can!");
                    }

                    Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;

                    #region Data

                    var HTTPHeaderBytes = new Byte[0];
                    var sw = new Stopwatch();

                    if (!Timeout.HasValue)
                    {
                        Timeout = TimeSpan.FromSeconds(60);
                    }

                    #endregion

                    #region Create TCP connection (possibly also do DNS lookups)

                    if (TCPClient == null)
                    {
                        System.Net.IPEndPoint _FinalIPEndPoint = null;
                        IIPAddress _ResolvedRemoteIPAddress    = null;

                        if (RemoteIPAddress == null)
                        {
                            if (Hostname.Trim() == "127.0.0.1")
                            {
                                _ResolvedRemoteIPAddress = IPv4Address.Localhost;
                            }

                            else
                            {
                                var RegExpr = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");

                                if (RegExpr.IsMatch(Hostname))
                                {
                                    _ResolvedRemoteIPAddress = IPv4Address.Parse(Hostname);
                                }
                            }

                            #region DNS lookup...

                            if (_ResolvedRemoteIPAddress == null)
                            {
                                try
                                {
                                    var IPv4AddressTask = DNSClient.
                                                          Query <A>(Hostname).
                                                          ContinueWith(QueryTask => QueryTask.Result.
                                                                       Select(ARecord => ARecord.IPv4Address).
                                                                       FirstOrDefault());

                                    IPv4AddressTask.Wait();

                                    _ResolvedRemoteIPAddress = IPv4AddressTask.Result;
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("[" + DateTime.Now + "] " + e.Message);
                                }
                            }

                            #endregion
                        }

                        else
                        {
                            _ResolvedRemoteIPAddress = RemoteIPAddress;
                        }

                        _FinalIPEndPoint = new System.Net.IPEndPoint(new System.Net.IPAddress(_ResolvedRemoteIPAddress.GetBytes()), RemotePort.ToInt32());

                        sw.Start();

                        TCPClient = new TcpClient();
                        TCPClient.Connect(_FinalIPEndPoint);
                        TCPClient.ReceiveTimeout = (Int32)Timeout.Value.TotalMilliseconds;
                    }

                    #endregion

                    #region Create (Crypto-)Stream

                    TCPStream             = TCPClient.GetStream();
                    TCPStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds;

                    TLSStream = RemoteCertificateValidator != null
                                     ? new SslStream(TCPStream,
                                                     false,
                                                     RemoteCertificateValidator)
                                //    ClientCertificateSelector,
                                //EncryptionPolicy.RequireEncryption)
                                     : null;

                    if (TLSStream != null)
                    {
                        TLSStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds;
                    }

                    HTTPStream = null;

                    if (RemoteCertificateValidator != null)
                    {
                        HTTPStream = TLSStream;
                        TLSStream.AuthenticateAsClient(Hostname);//, new X509CertificateCollection(new X509Certificate[] { ClientCert }), SslProtocols.Default, false);
                    }

                    else
                    {
                        HTTPStream = TCPStream;
                    }

                    HTTPStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds;

                    #endregion

                    #region Send Request

                    HTTPStream.Write(String.Concat(Request.EntireRequestHeader,
                                                   Environment.NewLine,
                                                   Environment.NewLine).
                                     ToUTF8Bytes());

                    var RequestBodyLength = Request.HTTPBody == null
                                                ? Request.ContentLength.HasValue ? (Int32)Request.ContentLength.Value : 0
                                                : Request.ContentLength.HasValue ? Math.Min((Int32)Request.ContentLength.Value, Request.HTTPBody.Length) : Request.HTTPBody.Length;

                    if (RequestBodyLength > 0)
                    {
                        HTTPStream.Write(Request.HTTPBody, 0, RequestBodyLength);
                    }

                    var _MemoryStream = new MemoryStream();
                    var _Buffer       = new Byte[10485760]; // 10 MBytes, a smaller value leads to read errors!

                    #endregion

                    #region Wait timeout for the server to react!

                    //Debug.WriteLine("[" + DateTime.Now + "] HTTPClient timeout: " + Timeout.Value.ToString());

                    while (!TCPStream.DataAvailable)
                    {
                        if (sw.ElapsedMilliseconds >= Timeout.Value.TotalMilliseconds)
                        {
                            TCPClient.Close();
                            throw new Exception("[" + DateTime.Now + "] Could not read from the TCP stream for " + sw.ElapsedMilliseconds + "ms!");
                        }

                        Thread.Sleep(1);
                    }

                    //Debug.WriteLine("[" + DateTime.Now + "] HTTPClient (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") got first response after " + sw.ElapsedMilliseconds + "ms!");

                    #endregion

                    #region Read the entire HTTP header, and maybe some of the HTTP body

                    var CurrentDataLength = 0;

                    do
                    {
                        #region When data available, write it to the buffer...

                        while (TCPStream.DataAvailable)
                        {
                            CurrentDataLength = HTTPStream.Read(_Buffer, 0, _Buffer.Length);

                            if (CurrentDataLength > -1)
                            {
                                _MemoryStream.Write(_Buffer, 0, CurrentDataLength);
                                //                        Debug.WriteLine("[" + DateTime.Now + "] Read " + CurrentDataLength + " bytes from HTTP connection (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") (" + sw.ElapsedMilliseconds + "ms)!");
                            }
                        }

                        #endregion

                        #region Check if the entire HTTP header was already read into the buffer

                        if (_MemoryStream.Length > 4)
                        {
                            var MemoryCopy = _MemoryStream.ToArray();

                            for (var pos = 3; pos < MemoryCopy.Length; pos++)
                            {
                                if (MemoryCopy[pos] == 0x0a &&
                                    MemoryCopy[pos - 1] == 0x0d &&
                                    MemoryCopy[pos - 2] == 0x0a &&
                                    MemoryCopy[pos - 3] == 0x0d)
                                {
                                    Array.Resize(ref HTTPHeaderBytes, pos - 3);
                                    Array.Copy(MemoryCopy, 0, HTTPHeaderBytes, 0, pos - 3);
                                    break;
                                }
                            }

                            //if (HTTPHeaderBytes.Length > 0)
                            //    Debug.WriteLine("[" + DateTime.Now + "] End of (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") HTTP header at " + HTTPHeaderBytes.Length + " bytes (" + sw.ElapsedMilliseconds + "ms)!");
                        }

                        #endregion

                        Thread.Sleep(1);
                    }
                    // Note: Delayed parts of the HTTP body may not be read into the buffer
                    //       => Must be read later!
                    while (TCPStream.DataAvailable ||
                           ((sw.ElapsedMilliseconds < HTTPStream.ReadTimeout) && HTTPHeaderBytes.Length == 0));

                    //Debug.WriteLine("[" + DateTime.Now + "] Finally read " + _MemoryStream.Length + " bytes of HTTP client (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") data (" + sw.ElapsedMilliseconds + "ms)!");

                    #endregion

                    #region Copy HTTP header data and create HTTP response

                    if (HTTPHeaderBytes.Length == 0)
                    {
                        throw new ApplicationException(DateTime.Now + " Could not find the end of the HTTP protocol header!");
                    }

                    Response = HTTPResponse.Parse(HTTPHeaderBytes.ToUTF8String(),
                                                  Request);

                    #endregion

                    #region Read 'Content-Length' bytes...

                    // Copy only the number of bytes given within
                    // the HTTP header element 'Content-Length'!
                    if (Response.ContentLength.HasValue && Response.ContentLength.Value > 0)
                    {
                        _MemoryStream.Seek(HTTPHeaderBytes.Length + 4, SeekOrigin.Begin);
                        var _Read        = _MemoryStream.Read(_Buffer, 0, _Buffer.Length);
                        var _StillToRead = (Int32)Response.ContentLength.Value - _Read;
                        Response.HTTPBodyStream.Write(_Buffer, 0, _Read);
                        var _CurrentBufferSize = 0;

                        do
                        {
                            while (TCPStream.DataAvailable && _StillToRead > 0)
                            {
                                _CurrentBufferSize = Math.Min(_Buffer.Length, (Int32)_StillToRead);
                                _Read = HTTPStream.Read(_Buffer, 0, _CurrentBufferSize);
                                Response.HTTPBodyStream.Write(_Buffer, 0, _Read);
                                _StillToRead -= _Read;
                            }

                            if (_StillToRead <= 0)
                            {
                                break;
                            }

                            Thread.Sleep(1);
                        }while (sw.ElapsedMilliseconds < HTTPStream.ReadTimeout);

                        Response.ContentStreamToArray();
                    }

                    #endregion

                    #region ...or read till timeout (e.g. for chunked transport)!

                    else
                    {
                        try
                        {
                            _MemoryStream.Seek(HTTPHeaderBytes.Length + 4, SeekOrigin.Begin);
                            Response.NewContentStream();
                            Response.HTTPBodyStream.Write(_Buffer, 0, _MemoryStream.Read(_Buffer, 0, _Buffer.Length));

                            var Retries = 0;

                            while (Retries < 10)
                            {
                                while (TCPStream.DataAvailable)
                                {
                                    Response.HTTPBodyStream.Write(_Buffer, 0, HTTPStream.Read(_Buffer, 0, _Buffer.Length));
                                    Retries = 0;
                                }

                                Thread.Sleep(10);
                                Retries++;
                            }

                            if (Response.TransferEncoding == "chunked")
                            {
                                //Debug.WriteLine(DateTime.Now + " Chunked encoding detected");

                                var TEContent       = ((MemoryStream)Response.HTTPBodyStream).ToArray();
                                var TEString        = TEContent.ToUTF8String();
                                var ReadBlockLength = true;
                                var TEMemStram      = new MemoryStream();
                                var LastPos         = 0;

                                var i = 0;
                                do
                                {
                                    if (i > 2 &&
                                        ReadBlockLength &&
                                        TEContent[i - 1] == '\n' &&
                                        TEContent[i - 2] == '\r')
                                    {
                                        var len = TEContent.ReadTEBlockLength(LastPos, i - LastPos - 2);

                                        //Debug.WriteLine(DateTime.Now + " Chunked encoded block of length " + len + " bytes detected");

                                        if (len == 0)
                                        {
                                            break;
                                        }

                                        if (i + len <= TEContent.Length)
                                        {
                                            TEMemStram.Write(TEContent, i, len);
                                            i = i + len;

                                            if (TEContent[i] == 0x0d)
                                            {
                                                i++;
                                            }

                                            if (i < TEContent.Length - 1)
                                            {
                                                if (TEContent[i] == 0x0a)
                                                {
                                                    i++;
                                                }
                                            }
                                            else
                                            {
                                            }

                                            LastPos = i;

                                            ReadBlockLength = false;
                                        }

                                        else
                                        {
                                            // Reaching this point seems to be an endless loop!
                                            break;
                                        }
                                    }

                                    else
                                    {
                                        ReadBlockLength = true;
                                        i++;
                                    }
                                } while (i < TEContent.Length);

                                Response.ContentStreamToArray(TEMemStram);
                            }

                            else
                            {
                                Response.ContentStreamToArray();
                            }
                        }

                        catch (Exception e)
                        {
                            Debug.WriteLine(DateTime.Now + " " + e.Message);
                        }
                    }

                    #endregion

                    #region Close connection if requested!

                    if (Response.Connection == null ||
                        Response.Connection == "close")
                    {
                        TCPClient.Close();
                        HTTPStream = null;
                        TCPClient  = null;
                    }

                    #endregion
                }
                catch (Exception e)
                {
                    #region Create a HTTP response for the exception...

                    while (e.InnerException != null)
                    {
                        e = e.InnerException;
                    }

                    Response = new HTTPResponseBuilder(Request,
                                                       HTTPStatusCode.BadRequest)
                    {
                        ContentType = HTTPContentType.JSON_UTF8,
                        Content     = JSONObject.Create(new JProperty("Message", e.Message),
                                                        new JProperty("StackTrace", e.StackTrace)).
                                      ToUTF8Bytes()
                    };

                    #endregion
                }

                #region Call the optional HTTP response log delegate

                try
                {
                    ResponseLogDelegate?.Invoke(DateTime.Now, this, Request, Response);
                }
                catch (Exception e2)
                {
                    e2.Log(nameof(HTTPClient) + "." + nameof(ResponseLogDelegate));
                }

                #endregion


                return(Response);
            }, TaskCreationOptions.AttachedToParent);

            return(await task);
        }
Beispiel #39
0
 public static void SendTo(this UDPClient UDPClient, Byte[] UDPPacketData, IIPAddress RemoteIPAddress, IPPort IPPort, SocketFlags SocketFlags = SocketFlags.None)
 {
     var RemoteIPEndPoint = new IPEndPoint(new IPAddress(RemoteIPAddress.GetBytes()), IPPort.ToInt32());
     UDPClient.SendTo(UDPPacketData, RemoteIPEndPoint, SocketFlags);
 }