void ReadAsync(IAsyncResult result) { try { int readCount = stream.EndRead(result); if (readCount == 0) { throw new Exception("读取到0字节"); } NetMsgData receive = NetUtilcs.UnpackNetMsg(buffer); this.mRecvingMsgQueue.Enqueue(receive); //string msg = Encoding.UTF8.GetString(buffer, 0, readCount); Debug.Log("接收到消息 -> " + receive.ProtoId); lock (stream) //再次开启读取 { Array.Clear(buffer, 0, buffer.Length); stream.BeginRead(buffer, 0, BuffseSize, ReadAsync, null); } } catch (Exception e) { Console.WriteLine(e.Message); } }
public void Update() { if (!this.mIsRunning) { return; } if (this.mRecvingMsgQueue.Count == 0) { lock (this.mRecvLock) { if (this.mRecvWaitingMsgQueue.Count > 0) { Queue <NetMsgData> temp = this.mRecvingMsgQueue; this.mRecvingMsgQueue = this.mRecvWaitingMsgQueue; this.mRecvWaitingMsgQueue = temp; } } } else { while (this.mRecvingMsgQueue.Count > 0) { NetMsgData msg = this.mRecvingMsgQueue.Dequeue(); //发送给逻辑处理 NetManager.DispatcherMsg(msg); } } }
public static NetMsgData GetMsgData(ProtoDefine protoType, IExtensible protoData, ushort length = 0) { NetMsgData data = new NetMsgData(); data.ProtoId = (ushort)protoType; data.MsgLength = length; data.ProtoData = protoData; return(data); }
//发送消息到服务器 public void SendMsg(ProtoDefine protoType, IExtensible protoData) { //byte[] temp = Encoding.UTF8.GetBytes(msg); NetMsgData msg = NetMsgData.GetMsgData(protoType, protoData); byte[] data = NetUtilcs.PackNetMsg(msg); stream.Write(data, 0, data.Length); Debug.Log("发送消息 -> " + msg.ProtoId + data); }
public void SendMsg1(ProtoDefine protoType, IExtensible protoData) { if (!this.mIsRunning) { return; } lock (this.mSendLock) { mSendWaitingMsgQueue.Enqueue(NetMsgData.GetMsgData(protoType, protoData)); Monitor.Pulse(this.mSendLock); } }
public static void DispatcherMsg(NetMsgData msgData) { ProtoDefine protoType = (ProtoDefine)msgData.ProtoId; Delegate d; if (m_EventTable.TryGetValue(protoType, out d)) { NetCallBack callBack = d as NetCallBack; if (callBack != null) { callBack(msgData.ProtoData); } } }
public static byte[] PackNetMsg(NetMsgData data) { ushort protoId = data.ProtoId; MemoryStream ms = null; using (ms = new MemoryStream()) { ms.Position = 0; BinaryWriter writer = new BinaryWriter(ms); byte[] pbdata = Serialize(data.ProtoData); ushort msglen = (ushort)pbdata.Length; writer.Write(msglen); writer.Write(protoId); writer.Write(pbdata); writer.Flush(); return(ms.ToArray()); } }
public static NetMsgData UnpackNetMsg(byte[] msgData) { MemoryStream ms = null; using (ms = new MemoryStream(msgData)) { BinaryReader reader = new BinaryReader(ms); ushort msgLen = reader.ReadUInt16(); ushort protoId = reader.ReadUInt16(); if (msgLen <= msgData.Length - 4) { IExtensible protoData = CreateProtoBuf.GetProtoData((ProtoDefine)protoId, reader.ReadBytes(msgLen)); return(NetMsgData.GetMsgData((ProtoDefine)protoId, protoData, msgLen)); } else { Debug.LogError("协议长度错误"); } } return(null); }
void Send() { while (this.mIsRunning) { if (mSendingMsgQueue.Count == 0) { lock (this.mSendLock) { while (this.mSendWaitingMsgQueue.Count == 0) { Monitor.Wait(this.mSendLock); } Queue <NetMsgData> temp = this.mSendingMsgQueue; this.mSendingMsgQueue = this.mSendWaitingMsgQueue; this.mSendWaitingMsgQueue = temp; } } else { try { NetMsgData msg = this.mSendingMsgQueue.Dequeue(); byte[] data = NetUtilcs.PackNetMsg(msg); mSocket.Send(data, data.Length, SocketFlags.None); Debug.Log("client send: " + (ProtoDefine)msg.ProtoId); } catch (System.Exception e) { Debug.LogError(e.Message); Disconnect(); } } } this.mSendingMsgQueue.Clear(); this.mSendWaitingMsgQueue.Clear(); }
void Receive() { byte[] data = new byte[1024]; while (this.mIsRunning) { try { //将收到的数据取出来 int len = mSocket.Receive(data); NetMsgData receive = NetUtilcs.UnpackNetMsg(data); Debug.Log("client receive : " + (ProtoDefine)receive.ProtoId); lock (this.mRecvLock) { this.mRecvWaitingMsgQueue.Enqueue(receive); } } catch (System.Exception e) { Debug.LogError(e.Message); Disconnect(); } } }