Esempio n. 1
0
        /// <summary>
        /// Start udp server listening on a specific port using TcpServerParam parameter.
        /// </summary>
        /// <param name="listenPort">The listening port of the server.</param>
        /// <param name="serverConfig">The server's parameter, see TcpServerParam.</param>
        public void Start(ushort listenPort, TcpServerConfig serverConfig)
        {
            if (_isRunning)
            {
                throw new AlreadyRunningException("the server is already running");
            }
            // _clientContextPool = new ClientContextPool(serverConfig.MaxClientCount, serverConfig.SocketAsyncBufferSize);
            Contract.Requires(listenPort > 0 && listenPort < 65535);
            Contract.Requires(serverConfig != null);
            _tcpConfig  = serverConfig;
            _listenPort = listenPort;
            try
            {
                //create listen socket
                _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                if (ServerConfig.EnableKeepAlive)
                {
                    TcpUtility.SetKeepAlive(_serverSocket, ServerConfig.KeepAliveTime, ServerConfig.KeepAliveInterval);
                }
                _serverSocket.Bind(new IPEndPoint(IPAddress.Any, listenPort));
                _serverSocket.Listen(ServerConfig.MaxPendingCount);
                _groups = new ConcurrentDictionary <string, HashSet <long> >();
            }
            catch (Exception ex)
            {
                throw new Exception("Start tcp server failed", ex);
            }

            _isRunning = true;
            StartAccept(null);
        }
Esempio n. 2
0
 private void InitClientSocket()
 {
     _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     if (ClientConfig.EnableKeepAlive)
     {
         TcpUtility.SetKeepAlive(_clientSocket, ClientConfig.KeepAliveTime, ClientConfig.KeepAliveInterval);
     }
 }