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 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 Watch() { _messagingManager.Run(); _tokenSource.Token.WaitHandle.WaitOne(); _messagingManager.Stop(); foreach (var responseTask in _tasks.Values) { try { responseTask.Stop(); } catch (Exception) { } } }
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(); }