/// <summary> /// 服务器端用于发送字节的方法 /// </summary> /// <param name="customer">用户自定义的命令头</param> /// <param name="bytes">实际发送的数据</param> public void Send(NetHandle customer, byte[] bytes) { if (IsClientStart) { SendBytes(session, InsideProtocol.CommandBytes(customer, Token, bytes)); } }
/// <summary> /// 心跳线程的方法 /// </summary> private void ThreadHeartCheck() { Thread.Sleep(2000); while (true) { Thread.Sleep(1000); if (!IsQuie) { byte[] send = new byte[16]; BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(send, 0); SendBytes(session, InsideProtocol.CommandBytes(InsideProtocol.ProtocolCheckSecends, 0, Token, send)); double timeSpan = (DateTime.Now - session.HeartTime).TotalSeconds; if (timeSpan > 1 * 8)//8次没有收到失去联系 { if (isConnecting == 0) { LogNet?.WriteDebug(ToString( ), $"Heart Check Failed int {timeSpan} Seconds."); ReconnectServer( ); } if (!IsQuie) { Thread.Sleep(1000); } } } else { break; } } }
/// <summary> /// 数据处理中心 /// </summary> /// <param name="session">会话对象</param> /// <param name="protocol">消息的代码</param> /// <param name="customer">用户消息</param> /// <param name="content">数据内容</param> internal override void DataProcessingCenter(AppSession session, int protocol, int customer, byte[] content) { if (protocol == InsideProtocol.ProtocolCheckSecends) { BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(content, 8); SendBytes(session, InsideProtocol.CommandBytes(InsideProtocol.ProtocolCheckSecends, customer, Token, content)); session.HeartTime = DateTime.Now; } else if (protocol == InsideProtocol.ProtocolClientQuit) { TcpStateDownLine(session, true); } else if (protocol == InsideProtocol.ProtocolUserBytes) { //接收到字节数据 AcceptByte?.Invoke(session, customer, content); } else if (protocol == InsideProtocol.ProtocolUserString) { //接收到文本数据 string str = Encoding.Unicode.GetString(content); AcceptString?.Invoke(session, customer, str); } else { // 其他一概不处理 } }
/// <summary> /// 关闭该客户端引擎 /// </summary> public void ClientClose( ) { IsQuie = true; if (IsClientStart) { SendBytes(session, InsideProtocol.CommandBytes(InsideProtocol.ProtocolClientQuit, 0, Token, null)); } IsClientStart = false; // 关闭客户端 thread_heart_check = null; LoginSuccess = null; // 清空所有的事件 LoginFailed = null; MessageAlerts = null; AcceptByte = null; AcceptString = null; try { session.WorkSocket?.Shutdown(SocketShutdown.Both); session.WorkSocket?.Close( ); } catch { } LogNet?.WriteDebug(ToString( ), "Client Close."); }
/// <summary> /// [自校验] 发送字节数据并确认对方接收完成数据,如果结果异常,则结束通讯 /// </summary> /// <param name="socket">网络套接字</param> /// <param name="headcode">头指令</param> /// <param name="customer">用户指令</param> /// <param name="send">发送的数据</param> /// <returns>是否发送成功</returns> protected OperateResult SendBaseAndCheckReceive(Socket socket, int headcode, int customer, byte[] send) { // 数据处理 send = InsideProtocol.CommandBytes(headcode, customer, Token, send); // 发送数据 OperateResult sendResult = Send(socket, send); if (!sendResult.IsSuccess) { return(sendResult); } // 检查对方接收完成 OperateResult <long> checkResult = ReceiveLong(socket); if (!checkResult.IsSuccess) { return(checkResult); } // 检查长度接收 if (checkResult.Content != send.Length) { socket?.Close(); return(new OperateResult(StringResources.Language.CommandLengthCheckFailed)); } return(checkResult); }
/// <summary> /// 客户端向服务器进行请求,请求字符串数据,并返回状态信息 /// </summary> /// <param name="customer">用户的指令头</param> /// <param name="send">发送数据</param> /// <returns>带返回消息的结果对象</returns> public OperateResult <NetHandle, string> ReadCustomerFromServer(NetHandle customer, string send = null) { var read = ReadCustomerFromServerBase(InsideProtocol.CommandBytes(customer, Token, send)); if (!read.IsSuccess) { return(OperateResult.CreateFailedResult <NetHandle, string>(read)); } return(OperateResult.CreateSuccessResult(read.Content1, Encoding.Unicode.GetString(read.Content2))); }
/// <summary> /// 客户端向服务器进行请求,请求字节数据 /// </summary> /// <param name="customer">用户的指令头</param> /// <param name="send">发送的字节内容</param> /// <returns>带返回消息的结果对象</returns> public OperateResult <byte[]> ReadFromServer(NetHandle customer, byte[] send) { return(ReadFromServerBase(InsideProtocol.CommandBytes(customer, Token, send))); }
/// <summary> /// 客户端向服务器进行请求,请求字符串数据,并返回状态信息,加密数据 /// </summary> /// <param name="customer">用户的指令头</param> /// <param name="send">发送数据</param> /// <returns>带返回消息的结果对象</returns> public OperateResult <NetHandle, byte[]> ReadCustomerFromServer(NetHandle customer, byte[] send) { return(ReadCustomerFromServerBaseInstructionsFirstEnDe(InsideProtocol.CommandBytes(customer, Token, send))); }
/// <summary> /// 向指定的通信对象发送字节数据 /// </summary> /// <param name="session">连接对象</param> /// <param name="customer">用户的指令头</param> /// <param name="bytes">实际的数据</param> public void SendMessage(AppSession session, int customer, byte[] bytes) { SendBytesAsync(session, InsideProtocol.CommandBytes(customer, Token, bytes)); }
/// <summary> /// 服务器端用于发送字节的方法 /// </summary> /// <param name="session">数据发送对象</param> /// <param name="customer">用户自定义的数据对象,如不需要,赋值为0</param> /// <param name="bytes">实际发送的数据</param> public void Send(AppSession session, NetHandle customer, byte[] bytes) { SendBytes(session, InsideProtocol.CommandBytes(customer, Token, bytes)); }
/// <summary> /// 服务器端用于数据发送文本的方法 /// </summary> /// <param name="session">数据发送对象</param> /// <param name="customer">用户自定义的数据对象,如不需要,赋值为0</param> /// <param name="str">发送的文本</param> public void Send(AppSession session, NetHandle customer, string str) { SendBytes(session, InsideProtocol.CommandBytes(customer, Token, str)); }