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; }
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(); } }); }
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(); }
private void NotifyNewSocketAccepted(SocketInfo socketInfo) { if (_socketEventListener != null) { Task.Factory.StartNew(() => _socketEventListener.OnNewSocketAccepted(socketInfo)); } }
public void ReceiveMessage(SocketInfo sourceSocket, Action<byte[]> messageReceivedCallback) { ReceiveInternal(new ReceiveState(sourceSocket, messageReceivedCallback), 4); }
public ReceiveState(SocketInfo sourceSocket, Action<byte[]> messageReceivedCallback) { SourceSocket = sourceSocket; MessageReceivedCallback = messageReceivedCallback; }
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); }
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); } }
public ReceiveContext(SocketInfo replySocketInfo, byte[] receivedMessage, Action<ReceiveContext> messageHandledCallback) { ReplySocketInfo = replySocketInfo; ReceivedMessage = receivedMessage; MessageHandledCallback = messageHandledCallback; }
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); } }
private void NotifySocketReceiveException(SocketInfo socketInfo, Exception exception) { if (_socketEventListener != null) { Task.Factory.StartNew(() => { _clientSocketDict.Remove(socketInfo.SocketRemotingEndpointAddress); _socketEventListener.OnSocketReceiveException(socketInfo, exception); }); } }