diff --git a/.gitignore b/.gitignore index d06ec0d..102567b 100755 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ HorseIsleServer/HorseIsleServer/bin/* HorseIsleServer/HorseIsleServer/obj/* HorseIsleServer/.vs/* -HorseIsleServer/HorseIsleServer/Resources/GitCommit +*GitCommit *.log *.suo \ No newline at end of file diff --git a/HorseIsleServer/HorseIsleServer/Program.cs b/HorseIsleServer/HorseIsleServer/Program.cs index 000dc1d..b7bf3a4 100644 --- a/HorseIsleServer/HorseIsleServer/Program.cs +++ b/HorseIsleServer/HorseIsleServer/Program.cs @@ -41,7 +41,7 @@ namespace HISP GameServer.StartServer(); - while (true) { }; + while (true) { }; } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) diff --git a/HorseIsleServer/HorseIsleServer/Server/GameClient.cs b/HorseIsleServer/HorseIsleServer/Server/GameClient.cs index 2e9128f..57d3b47 100644 --- a/HorseIsleServer/HorseIsleServer/Server/GameClient.cs +++ b/HorseIsleServer/HorseIsleServer/Server/GameClient.cs @@ -54,11 +54,11 @@ namespace HISP.Server private int totalMinutesElapsed = 0; private int oneMinute = 60 * 1000; - private int warnInterval = GameServer.IdleWarning * 60 * 1000; - private int kickInterval = GameServer.IdleTimeout * 60 * 1000; - + private int warnInterval = GameServer.IdleWarning * 60 * 1000; // Time before showing a idle warning + private int kickInterval = GameServer.IdleTimeout * 60 * 1000; // Time before kicking for inactivity + private List currentPacket = new List(); - private byte[] workBuffer = new byte[1028]; + private byte[] workBuffer = new byte[0x8000]; public GameClient(Socket clientSocket) { @@ -76,11 +76,13 @@ namespace HISP.Server connectedClients.Add(this); - SocketAsyncEventArgs e = new SocketAsyncEventArgs(); - e.Completed += receivePackets; - e.SetBuffer(workBuffer, 0, workBuffer.Length); - ClientSocket.ReceiveAsync(e); - +/* + SocketAsyncEventArgs evt = new SocketAsyncEventArgs(); + evt.Completed += receivePackets; + evt.SetBuffer(workBuffer); + clientSocket.ReceiveAsync(evt); +*/ + receivePackets(); } public static void CreateClient(object sender, SocketAsyncEventArgs e) @@ -89,19 +91,52 @@ namespace HISP.Server e.AcceptSocket = null; GameServer.ServerSocket.AcceptAsync(e); - GameClient client = new GameClient(eSocket); + new GameClient(eSocket); } +/* + public void Disconnect() + { + this.isDisconnecting = true; + // Cant outright stop threads anymore in .NET core, + // Lets just let the thread stop gracefully. + + // Stop Timers + if (inactivityTimer != null) + inactivityTimer.Dispose(); + if (warnTimer != null) + warnTimer.Dispose(); + if (kickTimer != null) + kickTimer.Dispose(); + + // Call OnDisconnect + connectedClients.Remove(this); + GameServer.OnDisconnect(this); + LoggedIn = false; + + // Close Socket + ClientSocket.Close(); + ClientSocket.Dispose(); + + } + + private void receivePackets(object sender, SocketAsyncEventArgs e) { // HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator if (!ClientSocket.Connected) + { Disconnect(); + return; + } if(e.SocketError != SocketError.Success) + { Disconnect(); + return; + } if (!isDisconnecting) { @@ -121,11 +156,60 @@ namespace HISP.Server } } - Array.Clear(e.Buffer); ClientSocket.ReceiveAsync(e); return; } + } + +*/ + public void Disconnect() + { + + // Cant outright stop threads anymore in .NET core, + // Lets just let the thread stop gracefully. + + this.isDisconnecting = true; + } + private bool receivePackets() + { + // HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator + Logger.DebugPrint("Reciving Packets..."); + + while (ClientSocket.Connected && !isDisconnecting) + { + if (isDisconnecting) + break; + + try + { + int availble = ClientSocket.Available; + if (availble >= 1) + { + byte[] buffer = new byte[availble]; + ClientSocket.Receive(buffer); + + for (int i = 0; i < availble; i++) + { + currentPacket.Add(buffer[i]); + if (buffer[i] == PacketBuilder.PACKET_TERMINATOR) + { + parsePackets(currentPacket.ToArray()); + currentPacket.Clear(); + } + } + + } + } + catch (SocketException) + { + Disconnect(); + break; + } + + } + + // Stop Timers if (inactivityTimer != null) inactivityTimer.Dispose(); @@ -143,7 +227,7 @@ namespace HISP.Server ClientSocket.Close(); ClientSocket.Dispose(); - return; + return isDisconnecting; // Stop the task. } private void minuteTimerTick(object state) @@ -467,16 +551,7 @@ namespace HISP.Server } } - public void Disconnect() - { - - // Cant outright stop threads anymore in .NET core, - // Lets just let the thread stop gracefully. - - this.isDisconnecting = true; - } - - public void Kick(string Reason) + public void Kick(string Reason) { byte[] kickPacket = PacketBuilder.CreateKickMessage(Reason); SendPacket(kickPacket); diff --git a/HorseIsleServer/HorseIsleServer/Server/PacketBuilder.cs b/HorseIsleServer/HorseIsleServer/Server/PacketBuilder.cs index 5121a37..ffe0b88 100644 --- a/HorseIsleServer/HorseIsleServer/Server/PacketBuilder.cs +++ b/HorseIsleServer/HorseIsleServer/Server/PacketBuilder.cs @@ -393,37 +393,30 @@ namespace HISP.Server } public static byte[] CreatePlaysoundPacket(string sound) { - MemoryStream ms = new MemoryStream(); - ms.WriteByte(PACKET_PLAYSOUND); + byte[] soundBytes = Encoding.UTF8.GetBytes(sound); + byte[] packet = new byte[(1 * 2) + soundBytes.Length]; - byte[] strBytes = Encoding.UTF8.GetBytes(sound); - ms.Write(strBytes, 0x00, strBytes.Length); + packet[0] = PACKET_PLAYSOUND; - ms.WriteByte(PACKET_TERMINATOR); + Array.Copy(soundBytes, 0, packet, 1, soundBytes.Length); - ms.Seek(0x00, SeekOrigin.Begin); - byte[] Packet = ms.ToArray(); - ms.Dispose(); + packet[packet.Length - 1] = PACKET_TERMINATOR; - return Packet; + return packet; } public static byte[] CreatePlayerLeavePacket(string username) { - MemoryStream ms = new MemoryStream(); + byte[] userBytes = Encoding.UTF8.GetBytes(username); + byte[] packet = new byte[(1 * 3) + userBytes.Length]; - ms.WriteByte(PACKET_PLAYERINFO); - ms.WriteByte(PLAYERINFO_LEAVE); + packet[0] = PACKET_PLAYERINFO; + packet[1] = PLAYERINFO_LEAVE; - byte[] strBytes = Encoding.UTF8.GetBytes(username); - ms.Write(strBytes, 0x00, strBytes.Length); + Array.Copy(userBytes, 0, packet, 2, userBytes.Length); - ms.WriteByte(PACKET_TERMINATOR); + packet[packet.Length - 1] = PACKET_TERMINATOR; - ms.Seek(0x00, SeekOrigin.Begin); - byte[] Packet = ms.ToArray(); - ms.Dispose(); - - return Packet; + return packet; } public static byte[] CreatePlayerInfoUpdateOrCreate(int x, int y, int facing, int charId, string username) {