public void Write(SocketMessager messager, ServerSocketReceiveEventHandler receiveHandler, TimeSpan timeout) { if (!messager._isChangeId) { messager.Id = -messager.Id; } SyncReceive syncReceive = null; try { if (receiveHandler != null) { syncReceive = new SyncReceive(receiveHandler); lock (this._receiveHandlers_lock) { if (!this._receiveHandlers.ContainsKey(messager.Id)) { this._receiveHandlers.Add(messager.Id, syncReceive); } else { this._receiveHandlers[messager.Id] = syncReceive; } } } lock (_write_lock) { NetworkStream ns = this._tcpClient.GetStream(); base.Write(ns, messager); } this._lastActive = DateTime.Now; if (syncReceive != null) { syncReceive.Wait.Reset(); syncReceive.Wait.WaitOne(timeout, false); syncReceive.Wait.Set(); lock (this._receiveHandlers_lock) { this._receiveHandlers.Remove(messager.Id); } } } catch (Exception ex) { this._running = false; this.OnError(ex); if (syncReceive != null) { syncReceive.Wait.Set(); lock (this._receiveHandlers_lock) { this._receiveHandlers.Remove(messager.Id); } } } }
public SyncReceive(ServerSocketReceiveEventHandler onReceive) { this._receiveHandler = onReceive; this._wait = new ManualResetEvent(false); }
public void Write(SocketMessager messager, ServerSocketReceiveEventHandler receiveHandler) { this.Write(messager, receiveHandler, TimeSpan.FromSeconds(20)); }