public Task Watch <TService>(TService serviceManager, IPEndPoint endpoint) where TService : StateManagerBase, IService { return(Task.Run(() => { var tcpListener = new TcpListener(endpoint); tcpListener.Start(); using (var socket = tcpListener.AcceptSocket()) using (var networkStream = new NetworkStream(socket)) using (var messagingManager = new MessagingManager(networkStream, _bufferManager)) using (var listenTokenSource = new CancellationTokenSource()) { messagingManager.ReceiveEvent += (stream) => this.MessagingManager_ReceiveEvent(serviceManager, stream, messagingManager.Send, () => listenTokenSource.Cancel()); messagingManager.Run(); listenTokenSource.Token.WaitHandle.WaitOne(); messagingManager.Stop(); } foreach (var responseTask in _tasks.Values) { try { responseTask.Stop(); } catch (Exception) { } } })); }
public AmoebaDaemonManager(Socket socket, TService serviceManager, BufferManager bufferManager) { _socket = socket; _serviceManager = serviceManager; _bufferManager = bufferManager; _messagingManager = new MessagingManager(_socket, _bufferManager); _messagingManager.ReceiveEvent += this._messagingManager_ReceiveEvent; }
public void Connect(IPEndPoint endpoint) { _tcpClient = new TcpClient(); _tcpClient.Connect(endpoint); _networkStream = new NetworkStream(_tcpClient.Client); _messagingManager = new MessagingManager(_networkStream, _bufferManager); _messagingManager.ReceiveEvent += MessagingManager_ReceiveEvent; _messagingManager.Run(); }
public void Connect(IPEndPoint endpoint, CancellationToken token) { _tcpClient = new TcpClient(); for (; ;) { try { _tcpClient.Connect(endpoint); break; } catch (Exception) { } token.WaitHandle.WaitOne(1000); } _messagingManager = new MessagingManager(_tcpClient.Client, _bufferManager); _messagingManager.ReceiveEvent += _messagingManager_ReceiveEvent; _messagingManager.Run(); }