Exemple #1
0
 public ClientSocket Connect(string address, int port)
 {
     _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _socket.Connect(address, port);
     _socketInfo = new SocketInfo(_socket);
     return this;
 }
Exemple #2
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();
                }
            });
        }
Exemple #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();
        }
Exemple #4
0
 private void NotifyNewSocketAccepted(SocketInfo socketInfo)
 {
     if (_socketEventListener != null)
     {
         Task.Factory.StartNew(() => _socketEventListener.OnNewSocketAccepted(socketInfo));
     }
 }
Exemple #5
0
 public void ReceiveMessage(SocketInfo sourceSocket, Action<byte[]> messageReceivedCallback)
 {
     ReceiveInternal(new ReceiveState(sourceSocket, messageReceivedCallback), 4);
 }
Exemple #6
0
 public ReceiveState(SocketInfo sourceSocket, Action<byte[]> messageReceivedCallback)
 {
     SourceSocket = sourceSocket;
     MessageReceivedCallback = messageReceivedCallback;
 }
Exemple #7
0
 public SocketChannel(SocketInfo socketInfo)
 {
     SocketInfo = socketInfo;
 }
 public void OnSocketReceiveException(SocketInfo socketInfo, Exception exception)
 {
     var socketException = exception as SocketException;
     if (socketException != null)
     {
         _logger.DebugFormat("Producer SocketException, address:{0}, errorCode:{1}", socketInfo.SocketRemotingEndpointAddress, socketException.SocketErrorCode);
     }
     else
     {
         _logger.DebugFormat("Producer Exception, address:{0}, errorMsg:", socketInfo.SocketRemotingEndpointAddress, exception.Message);
     }
 }
 public void OnNewSocketAccepted(SocketInfo socketInfo)
 {
     _logger.DebugFormat("Accepted new producer, address:{0}", socketInfo.SocketRemotingEndpointAddress);
 }
Exemple #10
0
 public void OnSocketReceiveException(SocketInfo socketInfo, Exception exception)
 {
     _brokerController.ConsumerManager.RemoveConsumer(socketInfo.SocketRemotingEndpointAddress);
     var socketException = exception as SocketException;
     if (socketException != null)
     {
         _logger.DebugFormat("Consumer SocketException, address:{0}, errorCode:{1}", socketInfo.SocketRemotingEndpointAddress, socketException.SocketErrorCode);
     }
     else
     {
         _logger.DebugFormat("Consumer Exception, address:{0}, errorMsg:", socketInfo.SocketRemotingEndpointAddress, exception.Message);
     }
 }
Exemple #11
0
 public ReceiveContext(SocketInfo replySocketInfo, byte[] receivedMessage, Action<ReceiveContext> messageHandledCallback)
 {
     ReplySocketInfo = replySocketInfo;
     ReceivedMessage = receivedMessage;
     MessageHandledCallback = messageHandledCallback;
 }
Exemple #12
0
 public void OnNewSocketAccepted(SocketInfo socketInfo)
 {
     _logger.InfoFormat("Accepted new consumer, address:{0}", socketInfo.SocketRemotingEndpointAddress);
 }
 public void OnSocketException(SocketInfo socketInfo, SocketException socketException)
 {
     if (!SocketUtils.IsSocketDisconnectedException(socketException))
     {
         _logger.ErrorFormat("Admin SocketException, address:{0}, errorCode:{1}", socketInfo.SocketRemotingEndpointAddress, socketException.SocketErrorCode);
     }
 }
 public void OnNewSocketAccepted(SocketInfo socketInfo) { }
 public void OnSocketException(SocketInfo socketInfo, SocketException socketException)
 {
     if (SocketUtils.IsSocketDisconnectedException(socketException))
     {
         _brokerController.ConsumerManager.RemoveConsumer(socketInfo.SocketRemotingEndpointAddress);
     }
     else
     {
         _logger.ErrorFormat("Consumer SocketException, address:{0}, errorCode:{1}", socketInfo.SocketRemotingEndpointAddress, socketException.SocketErrorCode);
     }
 }
Exemple #16
0
 private void NotifySocketReceiveException(SocketInfo socketInfo, Exception exception)
 {
     if (_socketEventListener != null)
     {
         Task.Factory.StartNew(() =>
         {
             _clientSocketDict.Remove(socketInfo.SocketRemotingEndpointAddress);
             _socketEventListener.OnSocketReceiveException(socketInfo, exception);
         });
     }
 }