private void Resize() { SocketSendBuf buffer = null; for (UInt32 index = 0; index < m_iQueueSize; ++index) { buffer = new SocketSendBuf(); if (buffer == null) { //-- 抛出异常 } m_SendList.Add(buffer); } }
public SocketSendBuf GetBuf() { SocketSendBuf buffer = null; //-- 01. 在当前list中查找free的。 for (Int32 index = 0; index < m_SendList.Count; ++index, m_iPos++) { if (m_iPos >= m_SendList.Count) { m_iPos = 0; } try { buffer = m_SendList[m_iPos]; } catch (System.Exception ex) { //-- log记录异常 //-- 从开始找到一个可以free的。 //Console.WriteLine("ex:==[" + ex.ToString() + "]"); UnityEngine.Debug.LogWarning("ex:=[" + ex.ToString() + "]"); m_iPos = 0; goto __NEW_SENDBUFFER; } if (buffer.GetEmptyFlag() == false) { continue; } buffer.Clear(); m_iPos++; if (m_iPos >= m_SendList.Count) { m_iPos = 0; } return(buffer); } __NEW_SENDBUFFER: //-- 02. 没有找到则需要添加 if ((buffer == null) || (buffer != null) && (buffer.GetEmptyFlag() == true) ) { buffer = new SocketSendBuf(); //-TODO: 下面这种情况就SB了。。。 if (buffer == null) { //-- log 怎么处理。 } m_SendList.Add(buffer); } buffer.Clear(); m_iPos++; if (m_iPos >= m_SendList.Count) { m_iPos = 0; } return(buffer); }
//-- 将逻辑发送缓冲区的数据转存到net层的发送缓冲区,开启异步发送过程。并立即返回。 public bool SendToSocketBuf(LogicSendHandler logicSendHandler) { if (logicSendHandler == null) { return(false); } //-TODO: 这里之后再考虑 if (!IsConnected()) { return(true); } int iSendLength = logicSendHandler.SendBufSize(); if (iSendLength == 0) { return(true); } MemoryStream oMemStream = logicSendHandler.GetLogicSendBuf(); if (oMemStream == null) { return(false); } //-- 将逻辑层缓冲区转到发送缓冲区 //-TODO: encode type SocketSendBuf sendBuff = m_SocketSendBufQueue.GetBuf(); if (sendBuff == null) { return(false); } MemoryStream sendMemStream = sendBuff.GetMemoryStream(); if (sendMemStream == null) { return(false); } //-- oMemStream.GetBuffer()不发生new byte[]操作。 if (oMemStream.Length == 0) { //Console.WriteLine("Error:==[logicSendHandler.SendBufSize() != 0 && oMemStream.Length == 0]"); return(false); } try { sendMemStream.Write(oMemStream.GetBuffer(), 0, (int)oMemStream.Length); } catch (System.Exception ex) { //-- log CommonDebugLog.LogWarning("ex:=[" + ex.ToString() + "]"); return(false); } sendBuff.SetEmptyFlag(false); logicSendHandler.ClearUp(); return(Send(sendBuff)); }
private bool Send(SocketSendBuf sendBuff) { if (sendBuff == null) { return(false); } MemoryStream sendMemStream = sendBuff.GetMemoryStream(); if (sendMemStream == null) { return(false); } if (sendMemStream.Length == 0) { return(true); } if (sendBuff.IsFinished()) { return(true); } //-- 异步将发送缓冲区数据发送出去。 OnSendObj sendObj = new OnSendObj(); sendObj.m_SendBuffer = sendBuff; sendObj.m_TSocket = this; SocketAsyncEventArgs sendCompleteArgs = new SocketAsyncEventArgs(); sendCompleteArgs.RemoteEndPoint = m_IPAndPoint; sendCompleteArgs.UserToken = sendObj; sendCompleteArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnSend); try { //-- 下面的以后放在一个pool中。 byte[] tmpbyteArray = new byte[(int)sendMemStream.Length]; Array.Copy(sendMemStream.GetBuffer(), tmpbyteArray, (int)sendMemStream.Length); sendCompleteArgs.SetBuffer(tmpbyteArray, 0, tmpbyteArray.Length); //sendCompleteArgs.SetBuffer(sendMemStream.GetBuffer(), sendBuff.GetTransferByteNum(), (int)sendMemStream.Length); //-- test log //Console.WriteLine("sendMemStream.Length:==[" + sendMemStream.Length + "]"); } catch (System.Exception ex) { //-- log CommonDebugLog.LogWarning("ex:=[" + ex.ToString() + "]"); return(false); } //-: true,io pend. waiting callback try { if (!m_Socket.SendAsync(sendCompleteArgs)) { OnSend(null, sendCompleteArgs); } } catch (System.Exception ex) { CommonDebugLog.LogWarning("ex:=[" + ex.ToString() + "]"); } return(true); }