private void OnDataReceived(DNetworkPacket packetRaw) { Console.WriteLine($"Got packet of type: {packetRaw.type}"); switch (packetRaw.type) { // Authentication case nameof(DLoginResponsePacket): { DNetworkPacket <DLoginResponsePacket> packet = packetRaw.DataAsType <DLoginResponsePacket>(); if (packet.data.status == ResponsePacketStatus.Success) { this.Username = packet.data.username; } this.SetState(new ClientActiveState(this)); this.OnLoginResponseReceived?.Invoke(this, packet.data.status == ResponsePacketStatus.Success, packet.data.errorMessage); } break; // Private Messages case nameof(DPrivateMessagePacket): { DNetworkPacket <DPrivateMessagePacket> packet = packetRaw.DataAsType <DPrivateMessagePacket>(); this.OnPrivateMessageReceived?.Invoke(this, packet.data.sender, packet.data.message); } break; case nameof(DPrivateMessageSendResponsePacket): { DNetworkPacket <DPrivateMessageSendResponsePacket> packet = packetRaw.DataAsType <DPrivateMessageSendResponsePacket>(); this.OnPrivateMessageSendResponse?.Invoke(this, packet.data.status == ResponsePacketStatus.Success, packet.data.errorMessage); } break; // Groups case nameof(DGroupCreateResponsePacket): { DNetworkPacket <DGroupCreateResponsePacket> packet = packetRaw.DataAsType <DGroupCreateResponsePacket>(); this.OnGroupCreatedResponse?.Invoke(this, packet.data.status == ResponsePacketStatus.Success, packet.data.errorMessage); } break; case nameof(DGroupChatJoinPacket): { DNetworkPacket <DGroupChatJoinPacket> packet = packetRaw.DataAsType <DGroupChatJoinPacket>(); this.OnGroupJoin?.Invoke(this, packet.data.groupName, packet.data.usersInGroup, packet.data.welcomeMessage); } break; case nameof(DGroupChatMessagePacket): { DNetworkPacket <DGroupChatMessagePacket> packet = packetRaw.DataAsType <DGroupChatMessagePacket>(); this.OnGroupMesssageReceived?.Invoke(this, packet.data.sender, packet.data.groupName, packet.data.message); } break; case nameof(DGroupMessageSendResponsePacket): { DNetworkPacket <DGroupMessageSendResponsePacket> packet = packetRaw.DataAsType <DGroupMessageSendResponsePacket>(); this.OnGroupMessageSendResponse?.Invoke(this, packet.data.status == ResponsePacketStatus.Success, packet.data.errorMessage); } break; case nameof(DServerClosingPacket): { DNetworkPacket <DServerClosingPacket> packet = packetRaw.DataAsType <DServerClosingPacket>(); this.OnServerClosing?.Invoke(this, packet.data.reason); } break; } }
private void OnDataReceived(DNetworkPacket packetRaw) { Console.WriteLine($"Got packet of type: {packetRaw.type}"); switch (packetRaw.type) { // Authentication case nameof(DLoginPacket): { DNetworkPacket <DLoginPacket> packet = packetRaw.DataAsType <DLoginPacket>(); this.Login(packet.data.username, packet.data.password); } break; case nameof(DSignUpPacket): { DNetworkPacket <DSignUpPacket> packet = packetRaw.DataAsType <DSignUpPacket>(); this.SignUp(packet.data.username, packet.data.password); } break; // Private Message case nameof(DPrivateMessagePacket): { DNetworkPacket <DPrivateMessagePacket> packet = packetRaw.DataAsType <DPrivateMessagePacket>(); this.SendPrivateMessage(packet.data.receiver, packet.data.message); } break; // Group case nameof(DGroupCreatePacket): { DNetworkPacket <DGroupCreatePacket> packet = packetRaw.DataAsType <DGroupCreatePacket>(); this.CreateGroup(packet.data.groupName, packet.data.usersToAdd, packet.data.welcomeMessage); } break; case nameof(DGroupChatMessagePacket): { DNetworkPacket <DGroupChatMessagePacket> packet = packetRaw.DataAsType <DGroupChatMessagePacket>(); this.SendGroupMessage(packet.data.groupName, packet.data.message); } break; // Disconnect case nameof(DClientDisconnectPacket): { this.server.ServerClientManager.Remove(this); Console.WriteLine($"Client {this.State.Username ?? this.client.Client.RemoteEndPoint.ToString()} disconnected!"); this.client.Close(); this.client.Dispose(); } break; } }