/// <summary> /// The constructor. /// </summary> /// <param name="memCache">The mem cache.</param> /// <param name="tagRoutingTable">The tag routing table.</param> /// <param name="logger">The logger.</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> public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, ILogger logger, int port, int maximumConnections, int messageBufferSize) { // Sanitize if (memCache == null) { throw new ArgumentNullException("memCache"); } if (tagRoutingTable == null) { throw new ArgumentNullException("tagRoutingTable"); } if (logger == null) { throw new ArgumentNullException("logger"); } if (port <= 0) { throw new ArgumentException("cannot be <= 0", "port"); } if (maximumConnections <= 0) { throw new ArgumentException("cannot be <= 0", "maximumConnections"); } if (messageBufferSize < 256) { throw new ArgumentException("cannot be < 256", "messageBufferSize"); } // Set the default cache item policies _defaultCacheItemPolicy = new CacheItemPolicy(); _defaultRemovedCallbackCacheItemPolicy = new CacheItemPolicy { RemovedCallback = CacheItemRemoved }; // Set the mem cache _memCache = memCache; // Set the tag routing table _tagRoutingTable = tagRoutingTable; // Set the logger _logger = logger; // Set maximum connections and message buffer size _maximumConnections = maximumConnections; _messageBufferSize = messageBufferSize; // Establish the endpoint for the socket var ipHostInfo = Dns.GetHostEntry(string.Empty); // Listen on all interfaces _localEndPoint = new IPEndPoint(IPAddress.Any, port); // Define the server _server = new SimplSocket(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), messageBufferSize, maximumConnections, false); // Hook into received message event _server.MessageReceived += ReceiveMessage; }
/// <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 256", "messageBufferSize"); } // Define the client _client = new SimplSocket(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), messageBufferSize, maximumConnections, false); // 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); }