/// <summary> /// 发送消息(得先从队列里把要发送的数据拿出来) /// This method is invoked when an asynchronous send operation completes. /// The method issues another receive on the socket to read any additional data sent from the client /// </summary> /// <param name="e"></param> /// <returns></returns> private bool ProcessSend(SocketAsyncEventArgs e) { AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken; userToken.ActiveDateTime = DateTime.Now; if (e.SocketError == SocketError.Success) { AsyncSendBufferManager bufferManager = userToken.SendBufferManager; bufferManager.ClearFirstPacket(); //清除已发送的包 int offset = 0; int count = 0; if (bufferManager.GetFirstPacket(ref offset, ref count)) { return(SendAsyncEvent(userToken.ConnectSocket, userToken.SendEventArgs, bufferManager.DynamicBufferManager.Buffer, offset, count)); } else { return(true); } //return SendCallback(); } else { CloseClientSocket(userToken); return(false); } }
public bool DoSendResult(byte[] buffer, int offset, int count) { string commandText = m_outgoingDataAssembler.GetProtocolText(); byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText); int totalLength = sizeof(int) + bufferUTF8.Length + count; //获取总大小 AsyncSendBufferManager asyncSendBufferManager = m_asyncSocketUserToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.DynamicBufferManager.WriteInt(totalLength, false); //写入总大小 asyncSendBufferManager.DynamicBufferManager.WriteInt(bufferUTF8.Length, false); //写入命令大小 asyncSendBufferManager.DynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容 asyncSendBufferManager.DynamicBufferManager.WriteBuffer(buffer, offset, count); //写入二进制数据 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(m_asyncSocketUserToken.ConnectSocket, m_asyncSocketUserToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount); } } return(result); }
//写入Command和二进制数据 public bool DoSendResult(byte[] buffer, int offset, int count) { string commandText = m_outgoingDataAssembler.GetProtocolText(); byte[] sendBuffer = Encoding.UTF8.GetBytes(commandText); int totalLength = sizeof(int) + sendBuffer.Length + buffer.Length; AsyncSendBufferManager asyncSendBufferManager = m_asyncSocketUserToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.DynamicBufferManager.WriteInt(totalLength, false); //写入总大小 asyncSendBufferManager.DynamicBufferManager.WriteInt(sendBuffer.Length, false); //写入command命令大小 asyncSendBufferManager.DynamicBufferManager.WriteBuffer(sendBuffer); //写入命令内容 asyncSendBufferManager.DynamicBufferManager.WriteBuffer(buffer); //写入二进制内容 asyncSendBufferManager.EndPacket(); bool result = true; if (!m_sendAsync) { int sendOffset = 0; int sendCount = 0; result = m_asyncSocketServer.SendAsyncEvent(m_asyncSocketUserToken.ConnectSocket, m_asyncSocketUserToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, sendOffset, sendCount); } return(result); }
public AsyncSocketUserToken(int asyncReceiveBufferSize) { m_connectSocket = null; m_asyncSocketInvokeElement = null; m_receiveEventArgs = new SocketAsyncEventArgs(); m_receiveEventArgs.UserToken = this; m_asyncReceiveBuffer = new byte[asyncReceiveBufferSize]; m_receiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length); m_sendEventArgs = new SocketAsyncEventArgs(); m_sendEventArgs.UserToken = this; m_receiveBuffer = new DynamicBufferManager(ProtocolConst.InitBufferSize); m_sendBuffer = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);; }
protected SocketAsyncEventArgs m_sendEventArgs; //异步发送事件 #endregion Fields #region Constructors public AsyncSocketUserToken(int asyncReceiveBufferSize) { m_connectSocket = null;//清空connectSocket m_asyncSocketInvokeElement = null;//清空数据传输协议 m_receiveEventArgs = new SocketAsyncEventArgs();//新建接收SocketAsyncEventArgs事件 m_receiveEventArgs.UserToken = this;//指定UserToken m_asyncReceiveBuffer = new byte[asyncReceiveBufferSize];//设置接收缓冲区大小 m_receiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length);//设置异步套接字方法的数据缓冲区 m_sendEventArgs = new SocketAsyncEventArgs();//新建发送SocketAsyncEventArgs事件 m_sendEventArgs.UserToken = this;//指定UserToken m_receiveBuffer = new DynamicBufferManager(ProtocolConst.InitBufferSize);//动态buffer管理对象(接收) m_sendBuffer = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);//动态buffer管理对象(发送) }
/// <summary> /// 构造函数 /// </summary> /// <param name="asyncReceiveBufferSize"></param> public AsyncSocketUserToken(int asyncReceiveBufferSize) { m_connectSocket = null; ReceiveEventArgs = new SocketAsyncEventArgs(); ReceiveEventArgs.UserToken = this; m_asyncReceiveBuffer = new byte[asyncReceiveBufferSize]; ReceiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length);//设置要用于异步套接字方法的数据缓冲区。 SendEventArgs = new SocketAsyncEventArgs(); SendEventArgs.UserToken = this; //SendEventArgs.SetBuffer(m_asyncSendBuffer, 0, m_asyncReceiveBuffer.Length); ReceiveBufferManager = new DynamicBufferManager(ProtocolConst.InitBufferSize); SendBufferManager = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);; }
public virtual bool SendCompleted() { m_activeDT = DateTime.UtcNow; m_sendAsync = false; AsyncSendBufferManager asyncSendBufferManager = m_asyncSocketUserToken.SendBuffer; asyncSendBufferManager.ClearFirstPacket(); //清除已发送的包 int offset = 0; int count = 0; if (asyncSendBufferManager.GetFirstPacket(ref offset, ref count)) { m_sendAsync = true; return(m_asyncSocketServer.SendAsyncEvent(m_asyncSocketUserToken.ConnectSocket, m_asyncSocketUserToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, offset, count)); } else { return(SendCallback()); } }
public bool DoSendBuffer(byte[] buffer, int offset, int count) //不是按包格式下发一个内存块,用于日志这类下发协议 { AsyncSendBufferManager asyncSendBufferManager = m_asyncSocketUserToken.SendBuffer; asyncSendBufferManager.StartPacket(); asyncSendBufferManager.DynamicBufferManager.WriteBuffer(buffer, offset, count); 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(m_asyncSocketUserToken.ConnectSocket, m_asyncSocketUserToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount); } } return(result); }