/// <summary> /// 重写ProcessReceive 信息直接发送 /// </summary> /// <param name="buffer">byte[]</param> /// <param name="offset">int</param> /// <param name="count">int</param> /// <returns>bool</returns> public override bool ProcessReceive(byte[] buffer, int offset, int count) //接收异步事件返回的数据,用于对数据进行缓存和分包 { socketUserToken.ActiveDateTime = DateTime.Now; if (socketUserToken.SendUserTokenAll.Count > 0) { if (socketUserToken.SendUserTokenAll[0].ConnectSocket != null) { AsyncSendBufferManager asyncSendBufferManager = socketUserToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.m_dynamicBufferManager.WriteInt(buffer.Length); //写入总大小 asyncSendBufferManager.m_dynamicBufferManager.WriteBuffer(buffer); //写入命令内容 asyncSendBufferManager.EndPacket(); bool result = true; if (!m_sendAsync) { int packetOffset = 0; int packetCount = 0; if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount)) { m_sendAsync = true; result = m_asyncSocketServer.SendAsyncEvent(socketUserToken.ConnectSocket, socketUserToken.SendEventArgs, socketUserToken.ReceiveBuffer.Buffer, packetOffset, packetCount); } } } } return(true); }
/// <summary> /// 发送返回值 /// </summary> /// <returns>bool</returns> public bool DoSendResult() { try { string commandText = OutDataParser.GetProtocolText(); //已经添加了返回信息 插入-1 byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText); int totalLength = bufferUTF8.Length; //获取总大小 AsyncSendBufferManager asyncSendBufferManager = socketUserToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.m_dynamicBufferManager.WriteInt(totalLength); //写入总大小 asyncSendBufferManager.m_dynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容 asyncSendBufferManager.EndPacket(); bool result = true; if (!m_sendAsync) { int packetOffset = 0; int packetCount = 0; if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount)) { m_sendAsync = true; result = m_asyncSocketServer.SendAsyncEvent(socketUserToken.ConnectSocket, socketUserToken.SendEventArgs, socketUserToken.ReceiveBuffer.Buffer, packetOffset, packetCount); } } } catch { m_asyncSocketServer.CloseClientSocket(socketUserToken); } return(true); }
/// <summary> /// 发送信息 /// </summary> /// <param name="commandText">string</param> /// <param name="userToken">SocketUserToken 连接客户端</param> /// <returns>bool</returns> public bool SendMsgClientMsg(string commandText, SocketUserToken userToken) { byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText); int totalLength = bufferUTF8.Length; //获取总大小 AsyncSendBufferManager asyncSendBufferManager = userToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.m_dynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容 asyncSendBufferManager.EndPacket(); bool result = true; if (!userToken.m_sendAsync) { int packetOffset = 0; int packetCount = 0; if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount)) { userToken.m_sendAsync = true; result = SendAsyncEvent(userToken.ConnectSocket, userToken.SendEventArgs, asyncSendBufferManager.m_dynamicBufferManager.Buffer, packetOffset, packetCount); } } return(result); }