コード例 #1
0
ファイル: CacheHostServer.cs プロジェクト: Damian-Pumar/dache
        /// <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>
        /// <param name="timeoutMilliseconds">The communication timeout, in milliseconds.</param>
        /// <param name="maxMessageSize">The maximum message size, in bytes.</param>
        public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, ILogger logger, int port, int maximumConnections, int messageBufferSize, int timeoutMilliseconds, int maxMessageSize)
        {
            // Sanitize
            if (memCache == null)
            {
                throw new ArgumentNullException("memCache");
            }
            if (tagRoutingTable == null)
            {
                throw new ArgumentNullException("tagRoutingTable");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            // 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 SimplSocketServer(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), messageBufferSize: messageBufferSize, 
                communicationTimeout: timeoutMilliseconds, maxMessageSize: maxMessageSize, maximumConnections: maximumConnections);

            // Hook into events
            _server.ClientConnected += (sender, e) =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("CONN: Dache Client Connected");
                Console.ForegroundColor = ConsoleColor.Cyan;
            };
            _server.MessageReceived += ReceiveMessage;
            _server.Error += (sender, e) =>
            {
                _logger.Warn("Dache Client Disconnected", e.Exception);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("WARN: Dache Client Disconnected");
                Console.WriteLine("WARN: Reason = " + e.Exception.Message);
                Console.ForegroundColor = ConsoleColor.Cyan;
            };
        }
コード例 #2
0
ファイル: CacheHostServer.cs プロジェクト: najlepejsi/dache
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="memCache">The mem cache.</param>
        /// <param name="tagRoutingTable">The tag routing table.</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, int port, int maximumConnections, int messageBufferSize)
        {
            // Sanitize
            if (memCache == null)
            {
                throw new ArgumentNullException("memCache");
            }
            if (tagRoutingTable == null)
            {
                throw new ArgumentNullException("tagRoutingTable");
            }
            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 mem cache
            _memCache = memCache;
            // Set the tag routing table
            _tagRoutingTable = tagRoutingTable;

            // 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 = SimplSocket.CreateServer(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                (sender, e) => { /* Ignore it, client's toast */ }, ReceiveMessage, messageBufferSize, maximumConnections, false);

            // Load custom logging
            _logger = CustomLoggerLoader.LoadLogger();
        }