mirror of
https://github.com/islehorse/HISP.git
synced 2025-04-07 21:55:42 +12:00
async packet receive
This commit is contained in:
parent
3c5a8e0c80
commit
8609ccc509
9 changed files with 152 additions and 156 deletions
|
@ -267,28 +267,26 @@ namespace HISP.Game
|
||||||
|
|
||||||
public static bool InSpecialTile(int x, int y)
|
public static bool InSpecialTile(int x, int y)
|
||||||
{
|
{
|
||||||
try
|
foreach (SpecialTile specialTile in SpecialTiles)
|
||||||
{
|
{
|
||||||
GetSpecialTile(x, y);
|
if (specialTile.X == x && specialTile.Y == y)
|
||||||
return true;
|
{
|
||||||
}
|
return true;
|
||||||
catch (KeyNotFoundException)
|
}
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool InIsle(int x, int y)
|
public static bool InIsle(int x, int y)
|
||||||
{
|
{
|
||||||
try
|
foreach (Isle isle in Isles)
|
||||||
{
|
{
|
||||||
GetIsle(x, y);
|
if (isle.StartX <= x && isle.EndX >= x && isle.StartY <= y && isle.EndY >= y)
|
||||||
return true;
|
{
|
||||||
}
|
return true;
|
||||||
catch(KeyNotFoundException)
|
}
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
public static Zone GetZoneByName(string name)
|
public static Zone GetZoneByName(string name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
edb27809bdaf2408b8100306b19261164685622d
|
3c5a8e0c80a656a0701ea767685ab620fc08db9a
|
||||||
|
|
|
@ -49,4 +49,10 @@ all_users_subscribed=false
|
||||||
intrest_rate=3333
|
intrest_rate=3333
|
||||||
|
|
||||||
# Should print extra debug logs
|
# Should print extra debug logs
|
||||||
debug=false
|
# 0 - no logs
|
||||||
|
# 1 - errors only
|
||||||
|
# 2 - errors, warnings
|
||||||
|
# 3 - errors, warnings, hackers
|
||||||
|
# 4 - errors, warnings, hackers, info,
|
||||||
|
# 5 - debug, errors, warnings, info, hackers
|
||||||
|
log_level=4
|
|
@ -1,5 +1,4 @@
|
||||||
using HISP.Properties;
|
using HISP.Properties;
|
||||||
using System;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using HISP.Server;
|
using HISP.Server;
|
||||||
namespace HISP.Security
|
namespace HISP.Security
|
||||||
|
@ -9,8 +8,7 @@ namespace HISP.Security
|
||||||
public static byte[] GetPolicy()
|
public static byte[] GetPolicy()
|
||||||
{
|
{
|
||||||
if (!File.Exists(ConfigReader.CrossDomainPolicyFile)) {
|
if (!File.Exists(ConfigReader.CrossDomainPolicyFile)) {
|
||||||
if (ConfigReader.Debug)
|
Logger.InfoPrint("Cross-Domain-Policy file not found, using default");
|
||||||
Console.WriteLine("[DEBUG] Cross-Domain-Policy file not found, using default");
|
|
||||||
File.WriteAllText(ConfigReader.CrossDomainPolicyFile, Resources.DefaultCrossDomain);
|
File.WriteAllText(ConfigReader.CrossDomainPolicyFile, Resources.DefaultCrossDomain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace HISP.Server
|
||||||
public static string GameDataFile;
|
public static string GameDataFile;
|
||||||
public static string CrossDomainPolicyFile;
|
public static string CrossDomainPolicyFile;
|
||||||
|
|
||||||
public static bool Debug = false;
|
public static int LogLevel = 0;
|
||||||
public static bool AllUsersSubbed = false;
|
public static bool AllUsersSubbed = false;
|
||||||
public static bool BadWords = true;
|
public static bool BadWords = true;
|
||||||
public static bool DoCorrections = true;
|
public static bool DoCorrections = true;
|
||||||
|
@ -109,8 +109,8 @@ namespace HISP.Server
|
||||||
case "intrest_rate":
|
case "intrest_rate":
|
||||||
IntrestRate = int.Parse(data);
|
IntrestRate = int.Parse(data);
|
||||||
break;
|
break;
|
||||||
case "debug":
|
case "log_level":
|
||||||
Debug = data == "true";
|
LogLevel = int.Parse(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ namespace HISP.Server
|
||||||
|
|
||||||
public Socket ClientSocket;
|
public Socket ClientSocket;
|
||||||
public string RemoteIp;
|
public string RemoteIp;
|
||||||
|
|
||||||
private bool loggedIn = false;
|
private bool loggedIn = false;
|
||||||
public bool LoggedIn
|
public bool LoggedIn
|
||||||
{
|
{
|
||||||
|
@ -58,9 +57,96 @@ namespace HISP.Server
|
||||||
private int warnInterval = GameServer.IdleWarning * 60 * 1000;
|
private int warnInterval = GameServer.IdleWarning * 60 * 1000;
|
||||||
private int kickInterval = GameServer.IdleTimeout * 60 * 1000;
|
private int kickInterval = GameServer.IdleTimeout * 60 * 1000;
|
||||||
|
|
||||||
|
private List<byte> currentPacket = new List<byte>();
|
||||||
|
private byte[] workBuffer = new byte[1028];
|
||||||
private bool dcLock = false;
|
private bool dcLock = false;
|
||||||
|
|
||||||
|
public GameClient(Socket clientSocket)
|
||||||
|
{
|
||||||
|
ClientSocket = clientSocket;
|
||||||
|
RemoteIp = clientSocket.RemoteEndPoint.ToString();
|
||||||
|
|
||||||
|
if (RemoteIp.Contains(":"))
|
||||||
|
RemoteIp = RemoteIp.Substring(0, RemoteIp.IndexOf(":"));
|
||||||
|
|
||||||
|
Logger.DebugPrint("Client connected @ " + RemoteIp);
|
||||||
|
|
||||||
|
kickTimer = new Timer(new TimerCallback(kickTimerTick), null, kickInterval, kickInterval);
|
||||||
|
warnTimer = new Timer(new TimerCallback(warnTimerTick), null, warnInterval, warnInterval);
|
||||||
|
minuteTimer = new Timer(new TimerCallback(minuteTimerTick), null, oneMinute, oneMinute);
|
||||||
|
|
||||||
|
connectedClients.Add(this);
|
||||||
|
|
||||||
|
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
|
||||||
|
e.Completed += receivePackets;
|
||||||
|
e.SetBuffer(workBuffer, 0, workBuffer.Length);
|
||||||
|
ClientSocket.ReceiveAsync(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CreateClient(object sender, SocketAsyncEventArgs e)
|
||||||
|
{
|
||||||
|
Socket eSocket = e.AcceptSocket;
|
||||||
|
e.AcceptSocket = null;
|
||||||
|
GameServer.ServerSocket.AcceptAsync(e);
|
||||||
|
|
||||||
|
GameClient client = new GameClient(eSocket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receivePackets(object sender, SocketAsyncEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
// HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator
|
||||||
|
|
||||||
|
if (e.SocketError == SocketError.Success && !isDisconnecting)
|
||||||
|
{
|
||||||
|
|
||||||
|
int availble = e.BytesTransferred;
|
||||||
|
if (availble >= 1)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; i < availble; i++)
|
||||||
|
{
|
||||||
|
currentPacket.Add(e.Buffer[i]);
|
||||||
|
if (e.Buffer[i] == PacketBuilder.PACKET_TERMINATOR)
|
||||||
|
{
|
||||||
|
parsePackets(currentPacket.ToArray());
|
||||||
|
currentPacket.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Array.Clear(e.Buffer);
|
||||||
|
ClientSocket.ReceiveAsync(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (dcLock) { }; // Refuse to shut down until dcLock is cleared. (prevents TOCTOU issues.)
|
||||||
|
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
return; // Stop the task.
|
||||||
|
}
|
||||||
|
|
||||||
private void minuteTimerTick(object state)
|
private void minuteTimerTick(object state)
|
||||||
{
|
{
|
||||||
dcLock = true;
|
dcLock = true;
|
||||||
|
@ -251,15 +337,15 @@ namespace HISP.Server
|
||||||
public void Login(int id)
|
public void Login(int id)
|
||||||
{
|
{
|
||||||
// Check for duplicate
|
// Check for duplicate
|
||||||
foreach(GameClient Client in GameClient.ConnectedClients)
|
foreach (GameClient Client in GameClient.ConnectedClients)
|
||||||
{
|
{
|
||||||
if(Client.LoggedIn)
|
if (Client.LoggedIn)
|
||||||
{
|
{
|
||||||
if (Client.LoggedinUser.Id == id)
|
if (Client.LoggedinUser.Id == id)
|
||||||
Client.Kick(Messages.KickReasonDuplicateLogin);
|
Client.Kick(Messages.KickReasonDuplicateLogin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LoggedinUser = new User(this,id);
|
LoggedinUser = new User(this, id);
|
||||||
LoggedIn = true;
|
LoggedIn = true;
|
||||||
|
|
||||||
Database.SetIpAddress(id, RemoteIp);
|
Database.SetIpAddress(id, RemoteIp);
|
||||||
|
@ -267,73 +353,6 @@ namespace HISP.Server
|
||||||
|
|
||||||
inactivityTimer = new Timer(new TimerCallback(keepAliveTimerTick), null, keepAliveInterval, keepAliveInterval);
|
inactivityTimer = new Timer(new TimerCallback(keepAliveTimerTick), null, keepAliveInterval, keepAliveInterval);
|
||||||
}
|
}
|
||||||
private bool receivePackets()
|
|
||||||
{
|
|
||||||
// HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator
|
|
||||||
|
|
||||||
|
|
||||||
while(ClientSocket.Connected && !isDisconnecting)
|
|
||||||
{
|
|
||||||
if(isDisconnecting)
|
|
||||||
break;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (MemoryStream ms = new MemoryStream())
|
|
||||||
{
|
|
||||||
if (ClientSocket.Available >= 1)
|
|
||||||
{
|
|
||||||
byte[] buffer = new byte[ClientSocket.Available];
|
|
||||||
ClientSocket.Receive(buffer);
|
|
||||||
|
|
||||||
foreach (Byte b in buffer)
|
|
||||||
{
|
|
||||||
if (isDisconnecting)
|
|
||||||
break;
|
|
||||||
|
|
||||||
ms.WriteByte(b);
|
|
||||||
if (b == 0x00)
|
|
||||||
{
|
|
||||||
ms.Seek(0x00, SeekOrigin.Begin);
|
|
||||||
byte[] fullPacket = ms.ToArray();
|
|
||||||
parsePackets(fullPacket);
|
|
||||||
ms.Seek(0x00, SeekOrigin.Begin);
|
|
||||||
ms.SetLength(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(SocketException)
|
|
||||||
{
|
|
||||||
Disconnect();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
while (dcLock) { }; // Refuse to shut down until dcLock is cleared. (prevents TOCTOU issues.)
|
|
||||||
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
return isDisconnecting; // Stop the task.
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parsePackets(byte[] Packet)
|
private void parsePackets(byte[] Packet)
|
||||||
{
|
{
|
||||||
|
@ -484,38 +503,6 @@ namespace HISP.Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameClient(Socket clientSocket)
|
|
||||||
{
|
|
||||||
ClientSocket = clientSocket;
|
|
||||||
RemoteIp = clientSocket.RemoteEndPoint.ToString();
|
|
||||||
|
|
||||||
if(RemoteIp.Contains(":"))
|
|
||||||
RemoteIp = RemoteIp.Substring(0, RemoteIp.IndexOf(":"));
|
|
||||||
|
|
||||||
Logger.DebugPrint("Client connected @ " + RemoteIp);
|
|
||||||
|
|
||||||
kickTimer = new Timer(new TimerCallback(kickTimerTick), null, kickInterval, kickInterval);
|
|
||||||
warnTimer = new Timer(new TimerCallback(warnTimerTick), null, warnInterval, warnInterval);
|
|
||||||
minuteTimer = new Timer(new TimerCallback(minuteTimerTick), null, oneMinute, oneMinute);
|
|
||||||
|
|
||||||
connectedClients.Add(this);
|
|
||||||
|
|
||||||
receivePackets();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AcceptConnections(SocketAsyncEventArgs e, Socket socket)
|
|
||||||
{
|
|
||||||
e.AcceptSocket = null;
|
|
||||||
socket.AcceptAsync(e);
|
|
||||||
}
|
|
||||||
public static void CreateClient(object sender, SocketAsyncEventArgs e)
|
|
||||||
{
|
|
||||||
Socket eSocket = e.AcceptSocket;
|
|
||||||
AcceptConnections(e, GameServer.ServerSocket);
|
|
||||||
|
|
||||||
GameClient client = new GameClient(eSocket);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8050,7 +8050,7 @@ namespace HISP.Server
|
||||||
|
|
||||||
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
|
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
|
||||||
e.Completed += GameClient.CreateClient;
|
e.Completed += GameClient.CreateClient;
|
||||||
GameClient.AcceptConnections(e, ServerSocket);
|
ServerSocket.AcceptAsync(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,29 +4,35 @@ namespace HISP.Server
|
||||||
{
|
{
|
||||||
public class Logger
|
public class Logger
|
||||||
{
|
{
|
||||||
public static void HackerPrint(string text) // When someone is obviously cheating.
|
public static void ErrorPrint(string text)
|
||||||
{
|
{
|
||||||
ConsoleColor prevColor = Console.ForegroundColor;
|
if (ConfigReader.LogLevel >= 1)
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.WriteLine("[ERROR] " + text);
|
||||||
Console.WriteLine("[HACK] " + text);
|
|
||||||
Console.ForegroundColor = prevColor;
|
|
||||||
}
|
|
||||||
public static void DebugPrint(string text)
|
|
||||||
{
|
|
||||||
if (ConfigReader.Debug)
|
|
||||||
Console.WriteLine("[DEBUG] " + text);
|
|
||||||
}
|
}
|
||||||
public static void WarnPrint(string text)
|
public static void WarnPrint(string text)
|
||||||
{
|
{
|
||||||
Console.WriteLine("[WARN] " + text);
|
if (ConfigReader.LogLevel >= 2)
|
||||||
|
Console.WriteLine("[WARN] " + text);
|
||||||
}
|
}
|
||||||
public static void ErrorPrint(string text)
|
public static void HackerPrint(string text) // When someone is obviously cheating.
|
||||||
{
|
{
|
||||||
Console.WriteLine("[ERROR] " + text);
|
if (ConfigReader.LogLevel >= 3)
|
||||||
|
{
|
||||||
|
ConsoleColor prevColor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine("[HACK] " + text);
|
||||||
|
Console.ForegroundColor = prevColor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static void InfoPrint(string text)
|
public static void InfoPrint(string text)
|
||||||
{
|
{
|
||||||
Console.WriteLine("[INFO] " + text);
|
if (ConfigReader.LogLevel >= 4)
|
||||||
|
Console.WriteLine("[INFO] " + text);
|
||||||
|
}
|
||||||
|
public static void DebugPrint(string text)
|
||||||
|
{
|
||||||
|
if (ConfigReader.LogLevel >= 5)
|
||||||
|
Console.WriteLine("[DEBUG] " + text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -339,22 +339,17 @@ namespace HISP.Server
|
||||||
}
|
}
|
||||||
public static byte[] CreateDrawingUpdatePacket(string Drawing)
|
public static byte[] CreateDrawingUpdatePacket(string Drawing)
|
||||||
{
|
{
|
||||||
MemoryStream ms = new MemoryStream();
|
|
||||||
|
|
||||||
ms.WriteByte(PACKET_SWFMODULE);
|
|
||||||
byte[] drawingBytes = Encoding.UTF8.GetBytes(Drawing);
|
byte[] drawingBytes = Encoding.UTF8.GetBytes(Drawing);
|
||||||
ms.Write(drawingBytes, 0x00, drawingBytes.Length);
|
byte[] packet = new byte[(1 * 2) + drawingBytes.Length];
|
||||||
ms.WriteByte(PACKET_TERMINATOR);
|
|
||||||
|
|
||||||
ms.Seek(0x00, SeekOrigin.Begin);
|
packet[0] = PACKET_SWFMODULE;
|
||||||
return ms.ToArray();
|
Array.Copy(drawingBytes, 0, packet, 1, drawingBytes.Length);
|
||||||
|
packet[packet.Length] = PACKET_TERMINATOR;
|
||||||
|
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
public static byte[] CreateBrickPoetMovePacket(Brickpoet.PoetryPeice peice)
|
public static byte[] CreateBrickPoetMovePacket(Brickpoet.PoetryPeice peice)
|
||||||
{
|
{
|
||||||
|
|
||||||
MemoryStream ms = new MemoryStream();
|
|
||||||
ms.WriteByte(PACKET_SWFMODULE);
|
|
||||||
ms.WriteByte(BRICKPOET_MOVE);
|
|
||||||
string packetStr = "|";
|
string packetStr = "|";
|
||||||
packetStr += peice.Id + "|";
|
packetStr += peice.Id + "|";
|
||||||
packetStr += peice.X + "|";
|
packetStr += peice.X + "|";
|
||||||
|
@ -362,10 +357,16 @@ namespace HISP.Server
|
||||||
packetStr += "^";
|
packetStr += "^";
|
||||||
|
|
||||||
byte[] infoBytes = Encoding.UTF8.GetBytes(packetStr);
|
byte[] infoBytes = Encoding.UTF8.GetBytes(packetStr);
|
||||||
ms.Write(infoBytes, 0x00, infoBytes.Length);
|
byte[] packet = new byte[(1 * 3) + infoBytes.Length];
|
||||||
ms.WriteByte(PACKET_TERMINATOR);
|
|
||||||
ms.Seek(0x00, SeekOrigin.Begin);
|
packet[0] = PACKET_SWFMODULE;
|
||||||
return ms.ToArray();
|
packet[1] = BRICKPOET_MOVE;
|
||||||
|
|
||||||
|
Array.Copy(infoBytes, 0, packet, 2, infoBytes.Length);
|
||||||
|
|
||||||
|
packet[packet.Length] = PACKET_TERMINATOR;
|
||||||
|
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
public static byte[] CreateBrickPoetListPacket(Brickpoet.PoetryPeice[] room)
|
public static byte[] CreateBrickPoetListPacket(Brickpoet.PoetryPeice[] room)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue