void Send() { Debug.Log("NetMgr Send Thread start!"); while (this.mIsRunning) { if (this.mSendingMsgQueue.Count == 0) { lock (this.mSendLock) { while (this.mSendWaitingMsgQueue.Count == 0) { Monitor.Wait(this.mSendLock); } Queue <NetMsg> temp = this.mSendingMsgQueue; this.mSendingMsgQueue = this.mSendWaitingMsgQueue; this.mSendWaitingMsgQueue = temp; } } else { while (this.mSendingMsgQueue.Count > 0) { NetMsg msg = this.mSendingMsgQueue.Dequeue(); try { // 拼接TLV MemoryStream stream = new MemoryStream(); TProtocol proto = new TBinaryProtocol(new TStreamTransport(stream, stream)); // 类型 proto.WriteI32(msg.Type); // 长度 proto.WriteI32(msg.Length); // 值 stream.Write(msg.Data, 0, msg.Length); byte[] data = stream.ToArray(); this.mSocket.Send(data); stream.Close(); } catch (System.Exception) { Disconnect(); } } } } this.mSendingMsgQueue.Clear(); this.mSendWaitingMsgQueue.Clear(); Debug.Log("NetMgr Send Thread is over!"); }
/// <summary> /// 序列化<see cref="TApplicationException"/> /// </summary> /// <param name="message"></param> /// <param name="ex"></param> /// <param name="writeLength"></param> /// <returns></returns> static public byte[] Serialize(TMessage message, TApplicationException ex, bool writeLength = true) { using (var stream = new MemoryStream()) { var protocol = new TBinaryProtocol(new TStreamTransport(stream, stream)); if (writeLength) { stream.Write(new byte[4], 0, 4); } protocol.WriteMessageBegin(message); ex.Write(protocol); if (writeLength) { stream.Position = 0; protocol.WriteI32((int)stream.Length - 4); } return(stream.ToArray()); } }
/// <summary> /// 序列化<see cref="TMessage"/>以及指定<see cref="TBase"/>对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="message"></param> /// <param name="objectToSerialize"></param> /// <param name="writeLength"></param> /// <returns></returns> static public byte[] Serialize <T>(TMessage message, T objectToSerialize, bool writeLength = true) where T : TBase { using (var stream = new MemoryStream()) { var protocol = new TBinaryProtocol(new TStreamTransport(stream, stream)); if (writeLength) { stream.Write(new byte[4], 0, 4); } protocol.WriteMessageBegin(message); objectToSerialize.Write(protocol); if (writeLength) { stream.Position = 0; protocol.WriteI32((int)stream.Length - 4); } return(stream.ToArray()); } }
public override void WriteI32(int i32) { SafeAction(() => _binaryProtocol.WriteI32(i32)); }