/// <summary> /// The constructor. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> /// <param name="messageBufferSize">The buffer size to use for sending and receiving data.</param> /// <param name="timeoutMilliseconds">The communication timeout, in milliseconds.</param> /// <param name="maxMessageSize">The maximum message size, in bytes.</param> /// <param name="hostReconnectIntervalMilliseconds">The cache host reconnect interval, in milliseconds.</param> public CommunicationClient(string address, int port, int hostReconnectIntervalMilliseconds, int messageBufferSize, int timeoutMilliseconds, int maxMessageSize) { // Sanitize if (String.IsNullOrWhiteSpace(address)) { throw new ArgumentException("cannot be null, empty, or white space", "address"); } if (port <= 0) { throw new ArgumentException("must be greater than 0", "port"); } if (hostReconnectIntervalMilliseconds <= 0) { throw new ArgumentException("must be greater than 0", "hostReconnectIntervalMilliseconds"); } // Define the client _client = new SimplSocketClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), messageBufferSize: messageBufferSize, communicationTimeout: timeoutMilliseconds, maxMessageSize: maxMessageSize); // Wire into events _client.MessageReceived += (sender, e) => { var messageReceived = MessageReceived; if (messageReceived != null) { messageReceived(sender, e); } }; _client.Error += (sender, e) => { DisconnectFromServer(); }; // Establish the remote endpoint for the socket IPAddress ipAddress = null; if (!IPAddress.TryParse(address, out ipAddress)) { // Try and get DNS value var ipHostInfo = Dns.GetHostEntry(address); ipAddress = ipHostInfo.AddressList.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork // ignore link-local addresses (the 169.254.* is documented in IETF RFC 3927 => http://www.ietf.org/rfc/rfc3927.txt) && !i.ToString().StartsWith("169.254.")); if (ipAddress == null) { throw new ArgumentException("must be a valid host name or IP address", "address"); } } _remoteEndPoint = new IPEndPoint(ipAddress, port); // Set the cache host reconnect interval _hostReconnectIntervalMilliseconds = hostReconnectIntervalMilliseconds; // Initialize reconnect timer _reconnectTimer = new Timer(ReconnectToServer, null, Timeout.Infinite, Timeout.Infinite); // Always assume initially connected _isConnected = true; }
/// <summary> /// The constructor. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> /// <param name="maximumConnections">The maximum number of simultaneous connections.</param> /// <param name="messageBufferSize">The buffer size to use for sending and receiving data.</param> /// <param name="hostReconnectIntervalMilliseconds">The cache host reconnect interval, in milliseconds.</param> public CommunicationClient(string address, int port, int hostReconnectIntervalMilliseconds, int maximumConnections, int messageBufferSize) { // Sanitize if (String.IsNullOrWhiteSpace(address)) { throw new ArgumentException("cannot be null, empty, or white space", "address"); } if (port <= 0) { throw new ArgumentException("must be greater than 0", "port"); } if (hostReconnectIntervalMilliseconds <= 0) { throw new ArgumentException("must be greater than 0", "hostReconnectIntervalMilliseconds"); } if (maximumConnections <= 0) { throw new ArgumentException("must be greater than 0", "maximumConnections"); } if (messageBufferSize <= 256) { throw new ArgumentException("cannot be less than 512", "messageBufferSize"); } // Define the client _client = SimplSocket.CreateClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), (sender, e) => { DisconnectFromServer(); }, messageBufferSize, maximumConnections, false); // Establish the remote endpoint for the socket var ipHostInfo = Dns.GetHostEntry(address); var ipAddress = ipHostInfo.AddressList.First(i => i.AddressFamily == AddressFamily.InterNetwork); _remoteEndPoint = new IPEndPoint(ipAddress, port); // Set the cache host reconnect interval _hostReconnectIntervalMilliseconds = hostReconnectIntervalMilliseconds; // Initialize reconnect timer _reconnectTimer = new Timer(ReconnectToServer, null, Timeout.Infinite, Timeout.Infinite); }