/// <summary> /// 触发异常事件 /// </summary> /// <param name="state"></param> private void RaiseOtherException(TCPClientHandle handle, string descrip) { if (OtherException != null) { OtherException(this, new TCPEventArgs(descrip, handle)); } }
/// <summary> /// 触发网络错误事件 /// </summary> /// <param name="state"></param> private void RaiseNetError(TCPClientHandle handle) { if (NetError != null) { NetError(this, new TCPEventArgs(handle)); } }
/// <summary> /// 触发数据发送事件 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(TCPClientHandle handle) { if (CompletedSend != null) { CompletedSend(this, new TCPEventArgs(handle)); } }
private void RaiseDataReceived(TCPClientHandle handle) { if (DataReceived != null) { DataReceived(this, new TCPEventArgs(handle)); } }
/// <summary> /// 触发客户端连接事件 /// </summary> /// <param name="state"></param> private void RaiseClientConnected(TCPClientHandle handle) { if (ClientConnected != null) { ClientConnected(this, new TCPEventArgs(handle)); } }
/// <summary> /// 关闭一个与客户端之间的会话 /// </summary> /// <param name="handle">需要关闭的客户端会话对象</param> public void Close(TCPClientHandle handle) { if (handle != null) { _clients.Remove(handle); handle.Dispose(); _clientCount--; //TODO 触发关闭事件 } }
/// <summary> /// 开始进行监听 /// </summary> private void Accept() { TCPClientHandle handle; while (IsRunning) { TcpClient client = _listener.AcceptTcpClient(); if (_clientCount >= _maxClient) { //TODO 触发事件 } else { handle = new TCPClientHandle(client); _clientCount++; _clients.Add(handle); //TODO 创建一个处理客户端的线程并启动 //使用线程池来操作 new Thread(new ThreadStart(handle.RecevieData)).Start(); } } }
public TCPEventArgs(string msg, TCPClientHandle handle) { this._msg = msg; this._handle = handle; IsHandled = false; }
public TCPEventArgs(TCPClientHandle handle) { this._handle = handle; IsHandled = false; }
private void RaiseOtherException(TCPClientHandle handle) { RaiseOtherException(handle, ""); }