Removed "PlayerDisconnectPacket()" from client to server. Small bug fixes and changes

This commit is contained in:
EntenKoeniq 2021-12-08 09:57:19 +01:00
parent 857b1b7622
commit dc4263ab9f
4 changed files with 31 additions and 39 deletions

View File

@ -30,7 +30,7 @@ namespace CoopClient.Menus
#region ITEMS
private readonly NativeItem UsernameItem = new NativeItem("Username") { AltTitle = Main.MainSettings.Username };
private readonly NativeItem ServerIpItem = new NativeItem("Server IP") { AltTitle = Main.MainSettings.LastServerAddress };
internal readonly NativeItem ServerIpItem = new NativeItem("Server IP") { AltTitle = Main.MainSettings.LastServerAddress };
private readonly NativeItem ServerConnectItem = new NativeItem("Connect");
private readonly NativeItem AboutItem = new NativeItem("About", "~y~SOURCE~s~~n~" +
"https://github.com/GTACOOP-R~n~" +
@ -104,7 +104,6 @@ namespace CoopClient.Menus
MainMenu.Visible = false;
ServerList.MainMenu.Visible = false;
MenuPool.RefreshAll();
}
internal void DisconnectedMenuSetting()

View File

@ -93,10 +93,13 @@ namespace CoopClient.Menus.Sub
try
{
MainMenu.Visible = false;
Main.MainMenu.MainMenu.Visible = true;
Main.MainNetworking.DisConnectFromServer(server.IP);
Main.MainMenu.ServerIpItem.AltTitle = server.IP;
Main.MainMenu.MainMenu.Visible = true;
Main.MainSettings.LastServerAddress = server.IP;
Util.SaveSettings();
}

View File

@ -27,10 +27,6 @@ namespace CoopClient
{
if (IsOnServer())
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new PlayerDisconnectPacket() { ID = Main.LocalClientID }.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();
Client.Disconnect("Bye!");
}
else

View File

@ -202,10 +202,10 @@ namespace CoopServer
switch (message.MessageType)
{
case NetIncomingMessageType.ConnectionApproval:
Logging.Info("New incoming connection from: " + message.SenderConnection.RemoteEndPoint.ToString());
Logging.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
if (message.ReadByte() != (byte)PacketTypes.HandshakePacket)
{
Logging.Info(string.Format("Player with IP {0} blocked, reason: Wrong packet!", message.SenderConnection.RemoteEndPoint.Address.ToString()));
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
message.SenderConnection.Deny("Wrong packet!");
}
else
@ -219,7 +219,7 @@ namespace CoopServer
}
catch (Exception e)
{
Logging.Info(string.Format("Player with IP {0} blocked, reason: {1}", message.SenderConnection.RemoteEndPoint.Address.ToString(), e.Message));
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
message.SenderConnection.Deny(e.Message);
}
}
@ -227,11 +227,9 @@ namespace CoopServer
case NetIncomingMessageType.StatusChanged:
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
long clientID = message.SenderConnection.RemoteUniqueIdentifier;
if (status == NetConnectionStatus.Disconnected && Clients.Any(x => x.ID == clientID))
if (status == NetConnectionStatus.Disconnected)
{
SendPlayerDisconnectPacket(new PlayerDisconnectPacket() { ID = clientID });
SendPlayerDisconnectPacket(message.SenderConnection.RemoteUniqueIdentifier);
}
break;
case NetIncomingMessageType.Data:
@ -255,18 +253,6 @@ namespace CoopServer
message.SenderConnection.Disconnect(e.Message);
}
break;
case (byte)PacketTypes.PlayerDisconnectPacket:
try
{
packet = new PlayerDisconnectPacket();
packet.NetIncomingMessageToPacket(message);
SendPlayerDisconnectPacket((PlayerDisconnectPacket)packet);
}
catch (Exception e)
{
message.SenderConnection.Disconnect(e.Message);
}
break;
case (byte)PacketTypes.FullSyncPlayerPacket:
try
{
@ -558,11 +544,6 @@ namespace CoopServer
return;
}
if (!string.IsNullOrEmpty(MainSettings.WelcomeMessage))
{
SendChatMessage(new ChatMessagePacket() { Username = "Server", Message = MainSettings.WelcomeMessage }, new List<NetConnection>() { local });
}
List<NetConnection> clients;
if ((clients = Util.FilterAllLocal(local)).Count > 0)
{
@ -600,33 +581,46 @@ namespace CoopServer
{
MainResource.InvokePlayerConnected(localClient);
}
else
{
Logging.Info($"Player {localClient.Player.Username} connected!");
}
if (!string.IsNullOrEmpty(MainSettings.WelcomeMessage))
{
SendChatMessage(new ChatMessagePacket() { Username = "Server", Message = MainSettings.WelcomeMessage }, new List<NetConnection>() { local });
}
}
// Send all players a message that someone has left the server
private static void SendPlayerDisconnectPacket(PlayerDisconnectPacket packet)
private static void SendPlayerDisconnectPacket(long clientID)
{
List<NetConnection> clients;
if ((clients = Util.FilterAllLocal(packet.ID)).Count > 0)
List<NetConnection> clients = MainNetServer.Connections;
if (clients.Count > 0)
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
new PlayerDisconnectPacket()
{
ID = clientID
}.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
}
Client localClient = Clients.FirstOrDefault(x => x.ID == packet.ID);
Client localClient = Clients.FirstOrDefault(x => x.ID == clientID);
if (localClient.Equals(default(Client)))
{
return;
}
Clients.Remove(localClient);
if (MainResource != null)
{
MainResource.InvokePlayerDisconnected(localClient);
}
lock (Clients)
else
{
Clients.Remove(localClient);
Logging.Info($"Player {localClient.Player.Username} disconnected!");
}
}