/// <summary> /// 接收分发封包 /// </summary> /// <param name="channel"></param> private static async void ProcessChannel(AChannel channel) { while (true) { byte[] messageBytes = await channel.RecvAsync(); Opcode opcode = (Opcode)BitConverter.ToUInt16(messageBytes, 0); if (!OpcodeHelper.IsClientMessage(opcode)) { continue; } ObjectId unitId = channel.GetComponent <ChannelUnitInfoComponent>().UnitId; Actor actor = World.Instance.GetComponent <ActorComponent>().Get(unitId); actor.Add(messageBytes); } }
/// <summary> /// 接收分发封包 /// </summary> /// <param name="channel"></param> private async void ProcessChannel(AChannel channel) { while (true) { byte[] messageBytes = await channel.RecvAsync(); Opcode opcode = (Opcode)BitConverter.ToUInt16(messageBytes, 0); // rpc异常 if (opcode == Opcode.RpcException) { int id = BitConverter.ToInt32(messageBytes, 2); this.RpcCallback(channel, id, messageBytes, RpcResponseStatus.Exception); continue; } // 表示消息是rpc响应消息 if (opcode == Opcode.RpcResponse) { int id = BitConverter.ToInt32(messageBytes, 2); this.RpcCallback(channel, id, messageBytes, RpcResponseStatus.Succee); continue; } // 如果是server message(发给client的消息),说明这是gate server,需要根据unitid查到channel,进行发送 if (OpcodeHelper.IsServerMessage(opcode)) { byte[] idBuffer = new byte[12]; Array.Copy(messageBytes, 2, idBuffer, 0, 12); ObjectId unitId = new ObjectId(idBuffer); byte[] buffer = new byte[messageBytes.Length - 6]; Array.Copy(messageBytes, 6, buffer, 0, buffer.Length); World.Instance.GetComponent <GateNetworkComponent>().SendAsync(unitId, buffer); continue; } // 处理Rpc请求,并且返回结果 RpcDo(channel, opcode, messageBytes); } }