internal void HandlePacket <TPlayer>(Type type, TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, Stream data) where TPlayer : Player, new() { switch (Activator.CreateInstance(type)) { case IAuthentication auth: { if (ProtoSerializer.Deserialize <v1.Authentication>(data, out var __)) { auth = __; HandleAuth(ws, auther, rooms, auth); } else { ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your authentication packet."))); } } break; case IRegistration reg: { if (ProtoSerializer.Deserialize <v1.Registration>(data, out var __)) { reg = __; HandleReg(ws, auther, rooms, reg); } else { ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your registration packet."))); } } break; case IRoomRequest req: { if (ProtoSerializer.Deserialize <v1.RoomRequest>(data, out var __)) { req = __; HandleRoom(ws, rooms, req); } else { ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your roomrequest packet."))); } } break; case IMessage msg: { if (ProtoSerializer.Deserialize <v1.Message>(data, out var __)) { msg = __; HandleMessage(ws, rooms, msg); } else { ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your message packet."))); } } break; default: break; } }
public override async void Handle <TPlayer>(TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, Stream data) { using (var t = new MemoryStream()) { data.CopyTo(t); data.Seek(0, SeekOrigin.Begin); Console.WriteLine(Convert.ToBase64String(t.ToArray())); } if (!ProtoSerializer.Deserialize <SimpleIProtoMessageInheriter>(data, out var res)) { await ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your message."))); return; } if (!ProtocolDefinition.FindTypeFor(res, out var type)) { await ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to find the type for your message."))); return; } HandlePacket(type, ws, auther, rooms, data); }