private void HandleRemotingRequest(ReceiveContext receiveContext)
        {
            var remotingRequest = RemotingUtil.ParseRequest(receiveContext.ReceivedMessage);
            IRequestHandler requestHandler;
            if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler))
            {
                var errorMessage = string.Format("No request handler found for remoting request, request code:{0}", remotingRequest.Code);
                _logger.Error(errorMessage);
                var remotingResponse = new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(errorMessage));
                receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                receiveContext.MessageHandledCallback(receiveContext);
                return;
            }

            try
            {
                var remotingResponse = requestHandler.HandleRequest(new SocketRequestHandlerContext(receiveContext), remotingRequest);
                if (!remotingRequest.IsOneway && remotingResponse != null)
                {
                    receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                    receiveContext.MessageHandledCallback(receiveContext);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("Exception raised when handling remoting request, request code:{0}.", remotingRequest.Code);
                _logger.Error(errorMessage, ex);
                if (!remotingRequest.IsOneway)
                {
                    var remotingResponse = new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(ex.Message));
                    receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                    receiveContext.MessageHandledCallback(receiveContext);
                }
            }
        }
 public SocketRequestHandlerContext(ReceiveContext receiveContext)
 {
     Channel = new SocketChannel(receiveContext.ReplySocketInfo);
     SendRemotingResponse = remotingResponse =>
     {
         receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
         receiveContext.MessageHandledCallback(receiveContext);
     };
 }
Esempio n. 3
0
        public void Start(Action<ReceiveContext> messageReceivedCallback)
        {
            if (_isRunning) return;

            _isRunning = true;

            _listenNewClientWorker = new Worker("ServerSocket.AcceptNewClient", () =>
            {
                if (!_isRunning) return;

                _messageReceivedCallback = messageReceivedCallback;
                _newClientSocketSignal.Reset();

                try
                {
                    _socket.BeginAccept((asyncResult) =>
                    {
                        if (!_isRunning) return;

                        var clientSocket = _socket.EndAccept(asyncResult);
                        var socketInfo = new SocketInfo(clientSocket);
                        NotifyNewSocketAccepted(socketInfo);
                        _newClientSocketSignal.Set();

                        if (!_isRunning) return;

                        _socketService.ReceiveMessage(socketInfo, receivedMessage =>
                        {
                            var receiveContext = new ReceiveContext(socketInfo, receivedMessage, context =>
                            {
                                _socketService.SendMessage(context.ReplySocketInfo, context.ReplyMessage, sendResult => { });
                            });
                            _messageReceivedCallback(receiveContext);
                        });
                    }, _socket);
                }
                catch (SocketException socketException)
                {
                    _logger.Error(string.Format("Socket accept exception, ErrorCode:{0}", socketException.SocketErrorCode), socketException);
                }
                catch (Exception ex)
                {
                    _logger.Error("Unknown socket accept exception.", ex);
                }

                if (_isRunning)
                {
                    _newClientSocketSignal.WaitOne();
                }
            });
            _listenNewClientWorker.Start();
        }
Esempio n. 4
0
        public void Start(Action<ReceiveContext> messageReceivedCallback)
        {
            Task.Factory.StartNew(() =>
            {
                _messageReceivedCallback = messageReceivedCallback;
                _running = true;

                while (_running)
                {
                    _newClientSocketSignal.Reset();

                    try
                    {
                        _socket.BeginAccept((asyncResult) =>
                        {
                            var clientSocket = _socket.EndAccept(asyncResult);
                            var socketInfo = new SocketInfo(clientSocket);
                            _clientSocketDict.TryAdd(socketInfo.SocketRemotingEndpointAddress, socketInfo);
                            NotifyNewSocketAccepted(socketInfo);
                            _newClientSocketSignal.Set();
                            _socketService.ReceiveMessage(socketInfo, receivedMessage =>
                            {
                                var receiveContext = new ReceiveContext(socketInfo, receivedMessage, context =>
                                {
                                    _socketService.SendMessage(context.ReplySocketInfo.InnerSocket, context.ReplyMessage, sendResult => { });
                                });
                                _messageReceivedCallback(receiveContext);
                            });
                        }, _socket);
                    }
                    catch (SocketException socketException)
                    {
                        _logger.Error(string.Format("Socket accept exception, ErrorCode:{0}", socketException.SocketErrorCode), socketException);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Unknown socket accept exception.", ex);
                    }

                    _newClientSocketSignal.WaitOne();
                }
            });
        }