Revert Packet Parsing Method

C# ReceiveAsync is broken, and doenst work when flash sends stuff, for some reason
This commit is contained in:
Bluzume 2021-11-02 04:27:39 -04:00
parent 1653944b51
commit 9272d71ca7
4 changed files with 112 additions and 44 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
HorseIsleServer/HorseIsleServer/bin/* HorseIsleServer/HorseIsleServer/bin/*
HorseIsleServer/HorseIsleServer/obj/* HorseIsleServer/HorseIsleServer/obj/*
HorseIsleServer/.vs/* HorseIsleServer/.vs/*
HorseIsleServer/HorseIsleServer/Resources/GitCommit *GitCommit
*.log *.log
*.suo *.suo

View file

@ -41,7 +41,7 @@ namespace HISP
GameServer.StartServer(); GameServer.StartServer();
while (true) { }; while (true) { };
} }
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

View file

@ -54,11 +54,11 @@ namespace HISP.Server
private int totalMinutesElapsed = 0; private int totalMinutesElapsed = 0;
private int oneMinute = 60 * 1000; private int oneMinute = 60 * 1000;
private int warnInterval = GameServer.IdleWarning * 60 * 1000; private int warnInterval = GameServer.IdleWarning * 60 * 1000; // Time before showing a idle warning
private int kickInterval = GameServer.IdleTimeout * 60 * 1000; private int kickInterval = GameServer.IdleTimeout * 60 * 1000; // Time before kicking for inactivity
private List<byte> currentPacket = new List<byte>(); private List<byte> currentPacket = new List<byte>();
private byte[] workBuffer = new byte[1028]; private byte[] workBuffer = new byte[0x8000];
public GameClient(Socket clientSocket) public GameClient(Socket clientSocket)
{ {
@ -76,11 +76,13 @@ namespace HISP.Server
connectedClients.Add(this); connectedClients.Add(this);
SocketAsyncEventArgs e = new SocketAsyncEventArgs(); /*
e.Completed += receivePackets; SocketAsyncEventArgs evt = new SocketAsyncEventArgs();
e.SetBuffer(workBuffer, 0, workBuffer.Length); evt.Completed += receivePackets;
ClientSocket.ReceiveAsync(e); evt.SetBuffer(workBuffer);
clientSocket.ReceiveAsync(evt);
*/
receivePackets();
} }
public static void CreateClient(object sender, SocketAsyncEventArgs e) public static void CreateClient(object sender, SocketAsyncEventArgs e)
@ -89,19 +91,52 @@ namespace HISP.Server
e.AcceptSocket = null; e.AcceptSocket = null;
GameServer.ServerSocket.AcceptAsync(e); 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) private void receivePackets(object sender, SocketAsyncEventArgs e)
{ {
// HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator // HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator
if (!ClientSocket.Connected) if (!ClientSocket.Connected)
{
Disconnect(); Disconnect();
return;
}
if(e.SocketError != SocketError.Success) if(e.SocketError != SocketError.Success)
{
Disconnect(); Disconnect();
return;
}
if (!isDisconnecting) if (!isDisconnecting)
{ {
@ -121,11 +156,60 @@ namespace HISP.Server
} }
} }
Array.Clear(e.Buffer);
ClientSocket.ReceiveAsync(e); ClientSocket.ReceiveAsync(e);
return; 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 // Stop Timers
if (inactivityTimer != null) if (inactivityTimer != null)
inactivityTimer.Dispose(); inactivityTimer.Dispose();
@ -143,7 +227,7 @@ namespace HISP.Server
ClientSocket.Close(); ClientSocket.Close();
ClientSocket.Dispose(); ClientSocket.Dispose();
return; return isDisconnecting; // Stop the task.
} }
private void minuteTimerTick(object state) private void minuteTimerTick(object state)
@ -467,16 +551,7 @@ namespace HISP.Server
} }
} }
public void Disconnect() public void Kick(string Reason)
{
// Cant outright stop threads anymore in .NET core,
// Lets just let the thread stop gracefully.
this.isDisconnecting = true;
}
public void Kick(string Reason)
{ {
byte[] kickPacket = PacketBuilder.CreateKickMessage(Reason); byte[] kickPacket = PacketBuilder.CreateKickMessage(Reason);
SendPacket(kickPacket); SendPacket(kickPacket);

View file

@ -393,37 +393,30 @@ namespace HISP.Server
} }
public static byte[] CreatePlaysoundPacket(string sound) public static byte[] CreatePlaysoundPacket(string sound)
{ {
MemoryStream ms = new MemoryStream(); byte[] soundBytes = Encoding.UTF8.GetBytes(sound);
ms.WriteByte(PACKET_PLAYSOUND); byte[] packet = new byte[(1 * 2) + soundBytes.Length];
byte[] strBytes = Encoding.UTF8.GetBytes(sound); packet[0] = PACKET_PLAYSOUND;
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR); Array.Copy(soundBytes, 0, packet, 1, soundBytes.Length);
ms.Seek(0x00, SeekOrigin.Begin); packet[packet.Length - 1] = PACKET_TERMINATOR;
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet; return packet;
} }
public static byte[] CreatePlayerLeavePacket(string username) 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); packet[0] = PACKET_PLAYERINFO;
ms.WriteByte(PLAYERINFO_LEAVE); packet[1] = PLAYERINFO_LEAVE;
byte[] strBytes = Encoding.UTF8.GetBytes(username); Array.Copy(userBytes, 0, packet, 2, userBytes.Length);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR); packet[packet.Length - 1] = PACKET_TERMINATOR;
ms.Seek(0x00, SeekOrigin.Begin); return packet;
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
} }
public static byte[] CreatePlayerInfoUpdateOrCreate(int x, int y, int facing, int charId, string username) public static byte[] CreatePlayerInfoUpdateOrCreate(int x, int y, int facing, int charId, string username)
{ {