/// <summary> /// 发送数据包 /// </summary> /// <param name="packet"></param> /// <returns></returns> public bool WritePacket(Packet packet) { try { this.ClientWriter.WritePacket(packet); return(true); } catch (Exception ex) { //如果异常就关闭 if (Common.SocketIsDebug) { Console.WriteLine(Common.Log_Prefix + "写操作异常捕获ERROR:" + ex.Message); } if (this.OnThreadException != null) { ClientThreadEventArgs args = new ClientThreadEventArgs(); args.EventType = ClientThreadEventType.write_error; this.OnThreadException(this, args); } //并且让客户端吧自己删除掉 this.Stop(); } return(false); }
/// <summary> /// 线程函数 /// </summary> private void _startThread() { while (ClientSocket.Connected && this.ClientReader.stream.CanRead) { try { //去接受一个完整的数据包 List <Packet> packets = this.ClientReader.ReadPackSync(); if (this.OnReceviedPacket != null) { ReceiveEventArgs args = new ReceiveEventArgs(packets, this.RemoteAddress); this.OnReceviedPacket(ClientSocket, args); } } catch (Exception ex) { //如果异常就关闭 Console.WriteLine("客户端读取数据异常ERROR:" + ex.Message); if (this.OnThreadException != null) { ClientThreadEventArgs args = new ClientThreadEventArgs(); args.EventType = ClientThreadEventType.read_error; this.OnThreadException(this, args); } //自动关闭 this.Stop(); return; } } }
/// <summary> /// 客户端异常 /// </summary> /// <param name="sender"></param> /// <param name="data"></param> void clientThread_OnThreadException(object sender, ClientThreadEventArgs data) { }