/// <summary> /// 派发协议 /// </summary> /// <param name="protoId"></param> /// <param name="buff"></param> public void DispatchProto(int protoId, ByteBuffer buff) { if (!ProtoDic.ContainProtoId(protoId)) { Debug.LogError("未知协议号"); return; } Type protoType = ProtoDic.GetProtoTypeByProtoId(protoId); object toc = ProtoBuf.Serializer.Deserialize(protoType, new MemoryStream(buff.ReadBytes())); sEvents.Enqueue(new KeyValuePair <Type, object>(protoType, toc)); }
/// <summary> /// 派发协议 /// </summary> /// <param name="protoId"></param> /// <param name="buff"></param> public void DispatchProto(int protoId, byte[] buff) { if (!ProtoDic.ContainProtoId(protoId)) { Debug.LogError("未知协议号"); return; } Type protoType = ProtoDic.GetProtoTypeByProtoId(protoId); try { MessageParser messageParser = ProtoDic.GetMessageParser(protoType.TypeHandle); object toc = messageParser.ParseFrom(buff); sEvents.Enqueue(new KeyValuePair <Type, object>(protoType, toc)); } catch { Debug.Log("DispatchProto Error:" + protoType.ToString()); } }
private static void ReceiveMessage(object clientSocket) { Socket myClientSocket = (Socket)clientSocket; while (true) { try { int receiveNumber = myClientSocket.Receive(result); Console.WriteLine("接收客户端{0}消息, 长度为{1}", myClientSocket.RemoteEndPoint.ToString(), receiveNumber); ByteBuffer buff = new ByteBuffer(result); int len = buff.ReadShort(); int protoId = buff.ReadShort(); if (!ProtoDic.ContainProtoId(protoId)) { Console.WriteLine("未知协议号"); return; } if (protoId == 1003) { TosChat tos = ProtoBuf.Serializer.Deserialize <TosChat>(new MemoryStream(buff.ReadBytes())); Console.WriteLine(tos.name + " " + tos.content); TocChat toc = new TocChat(); toc.name = "服务端:"; toc.content = tos.content; SendMessage(toc, myClientSocket); } else if (protoId == 1002) { } } catch (Exception ex) { Console.WriteLine(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } }