예제 #1
0
        public void Start()
        {
            if (_serverState != 0)
            {
                throw new Exception();
            }

            var oldValue = Interlocked.Exchange(ref _serverState, 1);

            if (oldValue == 1)
            {
                return;
            }

            if (oldValue == -1)
            {
                _serverState = -1;
                return;
            }

            try
            {
                var inOptionValues = NetworkSettings.GetServerKeepAliveInfo();
                var backlog        = NetworkSettings.ServerListenBacklog;

                _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _server.ExclusiveAddressUse = true;
                _server.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _server.Bind(new IPEndPoint(ServerIP, ServerPort));
                _server.Listen(backlog);
            }
            catch
            {
                _serverState = -1;
                throw;
            }

            try
            {
                _server.BeginAccept(AcceptSocketCallback, _server);
            }
            catch
            {
                Close();
                return;
            }
        }
예제 #2
0
        public void Start <T>() where T : IMessageProcessor, new()
        {
            if (ServerIP == null)
            {
                throw new ArgumentNullException("ServerIP");
            }

            if (ServerPort <= 0 || ServerPort >= 65536)
            {
                throw new ArgumentOutOfRangeException("ServerPort");
            }

            if (_serverState != 0)
            {
                throw new RpcException("RpcServer Start failed,state error");
            }

            var originState = Interlocked.CompareExchange(ref _serverState, 1, 0);

            if (originState != 0)
            {
                throw new RpcException("RpcServer Start failed,state error");
            }

            try
            {
                _messageProcessor = new T();
                _messageProcessor.Init();

                var inOptionValues = NetworkSettings.GetServerKeepAliveInfo();
                var backlog        = NetworkSettings.ServerListenBacklog;

                _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _server.ExclusiveAddressUse = true;
                _server.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _server.LingerState = new System.Net.Sockets.LingerOption(true, 0);

                _server.Bind(new IPEndPoint(ServerIP, ServerPort));
                _server.Listen(backlog);
            }
            catch
            {
                _serverState = -1;
                throw;
            }

            var originState2 = Interlocked.CompareExchange(ref _serverState, 2, 1);

            if (originState2 != 1)
            {
                return;
            }

            try
            {
                _server.BeginAccept(AcceptSocketCallback, _server);
            }
            catch
            {
                Close();
                return;
            }
        }