void ReceiveThread() { IoBuffer temBuffer = new IoBuffer(ONCE_SIZE * 2); while (m_isIOThreadActive) { //如果空间不够 if (temBuffer.WriteRemain < ONCE_SIZE) { //如果移到太靠后的位置了,那么整体挪到开始的位置 if (temBuffer.ReadPos > ONCE_SIZE * 3) { temBuffer.MoveToFirst(); } //否则再申请点 else { temBuffer.EnsureCapacity(ONCE_SIZE); } } try { SocketError error = SocketError.Success; int length = m_socket.Receive(temBuffer.Buffer, temBuffer.WritePos, ONCE_SIZE, SocketFlags.None, out error); if (error != SocketError.Success)//接收不成功的话可能因为延迟很高,暂时的处理是掉线,经常出现的话可以考虑改为重新接收 { AddError(error, "m_socket.Receive error"); return; } if (length == 0) { AddError(error, "Socket shutdown gracefully"); return; } temBuffer.WritePos += length; //解码 decode(temBuffer, m_recvMessageList); } catch (System.Net.Sockets.SocketException e) { AddError(e.SocketErrorCode, e.Message + "\n" + e.StackTrace);//告诉主线程,然后主线程会传给上层 return; } catch (Exception e) { AddError(SocketError.SocketError, e.Message + "\n" + e.StackTrace);//告诉主线程,然后主线程会传给上层 return; } //如果完全读取完成了重置下 if (temBuffer.ReadSize == 0) { temBuffer.Reset(); } } }
void SendThread() { var tempBuffer = new IoBuffer(ONCE_SIZE); while (m_isIOThreadActive) { if (m_socket == null || !m_socket.Connected) { Thread.Sleep(20); continue; } if (m_sendMessageList.Count == 0) { Thread.Sleep(20); continue; } lock (m_sendMessageList) { m_sendTempMsgList.AddRange(m_sendMessageList); m_sendMessageList.Clear(); } while (m_sendTempMsgList.Count > 0) { try { tempBuffer.Reset(); encode(m_sendTempMsgList[0], tempBuffer); } catch (System.Exception e) { Util.SafeLogError(e.Message + "\n" + e.StackTrace); continue; } try { while (tempBuffer.ReadSize > 0) { int sendSize = Math.Min(tempBuffer.ReadSize, ONCE_SIZE); SocketError error = SocketError.Success; sendSize = m_socket.Send(tempBuffer.Buffer, tempBuffer.ReadPos, sendSize, SocketFlags.None, out error); if (error != SocketError.Success) { AddError(error, "m_socket.Send error"); return; } tempBuffer.ReadPos += sendSize; } Message.put(m_sendTempMsgList[0]); m_sendTempMsgList.RemoveAt(0); } catch (System.Net.Sockets.SocketException e) { AddError(e.SocketErrorCode, e.Message + "\n" + e.StackTrace); return; } catch (Exception e) { AddError(SocketError.SocketError, e.Message + "\n" + e.StackTrace); return; } } //发送速度不要太快 Thread.Sleep(5); } }
public bool SetRequestBodyObject(object obj) { try { //如果是null,那就直接body为null if (obj == null) { m_body = null; m_msg = null; m_code = 0; m_flag = 0; return(true); } if (USE_JSON_FORMAT) { var jsonStr = LitJson.JsonMapper.ToJson(obj); var jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonStr); //如果要压缩,且达到要压缩的字符数,那就压缩,compress可以直接得到Buffer,再直接转为IOBuffer if (jsonStr.Length > JSON_COMPRESS_LEN && ENABLE_COMPRESS) { //压缩并直接转为IOBuffer m_body = new IoBuffer(Snappy.Sharp.Snappy.Compress(jsonBytes)); //错误消息为空 m_msg = null; //错误码为空 m_code = 0; //加上标记 m_flag = MSG_FLAG_JSON | MSG_FLAG_COMPRESS; } //否则那就把字符串转为IOBuffer吧 else { //把字符串转成IOBuffer m_body = new IoBuffer(jsonBytes); //错误消息为空 m_msg = null; //错误码为空 m_code = 0; //加上标记 m_flag = MSG_FLAG_JSON; } } else { if (m_body == null) { m_body = new IoBuffer(256); } else { m_body.Reset(); } //错误消息为空 m_msg = null; //错误码为空 m_code = 0; //设置标记 m_flag = 0; ProtocolCoder.instance.Encode(m_body, obj); } return(true); } catch (Exception err) { Util.SafeLogError("Message~setRequestBodyObject发生错误{0}", err.StackTrace); m_body = null; m_msg = null; m_code = 0; m_flag = 0; return(false); } }