mirror of
https://github.com/islehorse/HISP.git
synced 2025-06-20 01:35:12 +12:00
Use Async Io instead of threads
This commit is contained in:
parent
392a3dfcaa
commit
edb27809bd
17 changed files with 128 additions and 104 deletions
|
@ -21,7 +21,17 @@ namespace HISP.Server
|
|||
ConnectionString = "server=" + ConfigReader.DatabaseIP + ";user=" + ConfigReader.DatabaseUsername + ";password=" + ConfigReader.DatabasePassword + ";database=" + ConfigReader.DatabaseName;
|
||||
using (MySqlConnection db = new MySqlConnection(ConnectionString))
|
||||
{
|
||||
db.Open();
|
||||
|
||||
try
|
||||
{
|
||||
db.Open();
|
||||
}
|
||||
catch (MySqlException e)
|
||||
{
|
||||
Logger.ErrorPrint("Failed to connect to Database: "+e.Message);
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
string UserTable = "CREATE TABLE IF NOT EXISTS Users(Id INT, Username TEXT(16), PassHash TEXT(128), Salt TEXT(128), Gender TEXT(16), Admin TEXT(3), Moderator TEXT(3))";
|
||||
string ExtTable = "CREATE TABLE IF NOT EXISTS UserExt(Id INT, X INT, Y INT, LastLogin INT, Money INT, QuestPoints INT, BankBalance DOUBLE, BankInterest DOUBLE, ProfilePage Text(4000),IpAddress TEXT(1028),PrivateNotes Text(65535), CharId INT, ChatViolations INT,Subscriber TEXT(3), SubscribedUntil INT, Experience INT, Tiredness INT, Hunger INT, Thirst INT, FreeMinutes INT, TotalLogins INT)";
|
||||
string MailTable = "CREATE TABLE IF NOT EXISTS Mailbox(RandomId INT, IdTo INT, IdFrom INT, Subject TEXT(100), Message Text(65535), TimeSent INT, BeenRead TEXT(3))";
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.IO;
|
|||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using HISP.Player;
|
||||
using HISP.Game;
|
||||
using HISP.Game.Horse;
|
||||
|
@ -12,6 +13,15 @@ namespace HISP.Server
|
|||
{
|
||||
public class GameClient
|
||||
{
|
||||
private static List<GameClient> connectedClients = new List<GameClient>();
|
||||
public static GameClient[] ConnectedClients // Done to prevent Enumerator Changed errors.
|
||||
{
|
||||
get
|
||||
{
|
||||
return connectedClients.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public Socket ClientSocket;
|
||||
public string RemoteIp;
|
||||
|
||||
|
@ -33,8 +43,6 @@ namespace HISP.Server
|
|||
}
|
||||
}
|
||||
public User LoggedinUser;
|
||||
|
||||
private Thread recvPackets;
|
||||
|
||||
private Timer inactivityTimer;
|
||||
|
||||
|
@ -233,8 +241,11 @@ namespace HISP.Server
|
|||
SendPacket(chatPacket);
|
||||
if (LoggedIn)
|
||||
LoggedinUser.Idle = true;
|
||||
warnTimer.Dispose();
|
||||
warnTimer = null;
|
||||
if(warnTimer != null)
|
||||
{
|
||||
warnTimer.Dispose();
|
||||
warnTimer = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -246,7 +257,7 @@ namespace HISP.Server
|
|||
public void Login(int id)
|
||||
{
|
||||
// Check for duplicate
|
||||
foreach(GameClient Client in GameServer.ConnectedClients)
|
||||
foreach(GameClient Client in GameClient.ConnectedClients)
|
||||
{
|
||||
if(Client.LoggedIn)
|
||||
{
|
||||
|
@ -317,16 +328,17 @@ namespace HISP.Server
|
|||
warnTimer.Dispose();
|
||||
if(kickTimer != null)
|
||||
kickTimer.Dispose();
|
||||
|
||||
|
||||
// Call OnDisconnect
|
||||
connectedClients.Remove(this);
|
||||
GameServer.OnDisconnect(this);
|
||||
LoggedIn = false;
|
||||
// LoggedinUser = null;
|
||||
// Close Sockets
|
||||
|
||||
// Close Socket
|
||||
ClientSocket.Close();
|
||||
ClientSocket.Dispose();
|
||||
|
||||
return isDisconnecting; // Stop the thread.
|
||||
return isDisconnecting; // Stop the task.
|
||||
}
|
||||
|
||||
private void parsePackets(byte[] Packet)
|
||||
|
@ -492,12 +504,25 @@ namespace HISP.Server
|
|||
warnTimer = new Timer(new TimerCallback(warnTimerTick), null, warnInterval, warnInterval);
|
||||
minuteTimer = new Timer(new TimerCallback(minuteTimerTick), null, oneMinute, oneMinute);
|
||||
|
||||
recvPackets = new Thread(() =>
|
||||
{
|
||||
receivePackets();
|
||||
});
|
||||
recvPackets.Start();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -26,13 +26,6 @@ namespace HISP.Server
|
|||
|
||||
public static Socket ServerSocket;
|
||||
|
||||
public static GameClient[] ConnectedClients // Done to prevent Enumerator Changed errors.
|
||||
{
|
||||
get {
|
||||
return connectedClients.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static int IdleTimeout;
|
||||
public static int IdleWarning;
|
||||
|
||||
|
@ -52,7 +45,6 @@ namespace HISP.Server
|
|||
private static int gameTickSpeed = 480; // Changing this to ANYTHING else will cause desync with the client.
|
||||
private static int totalMinutesElapsed = 0;
|
||||
private static int oneMinute = 1000 * 60;
|
||||
private static List<GameClient> connectedClients = new List<GameClient>();
|
||||
private static Timer gameTimer; // Controls in-game time.
|
||||
private static Timer minuteTimer; // ticks every real world minute.
|
||||
private static int lastServerTime = 0;
|
||||
|
@ -193,7 +185,7 @@ namespace HISP.Server
|
|||
}
|
||||
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client == null)
|
||||
continue;
|
||||
|
@ -3629,7 +3621,7 @@ namespace HISP.Server
|
|||
|
||||
UpdateArea(sender);
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
|
@ -3690,7 +3682,7 @@ namespace HISP.Server
|
|||
|
||||
// Send login message
|
||||
byte[] loginMessageBytes = PacketBuilder.CreateChat(Messages.FormatLoginMessage(sender.LoggedinUser.Username), PacketBuilder.CHAT_BOTTOM_LEFT);
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
if (!client.LoggedinUser.MuteLogins && !client.LoggedinUser.MuteAll)
|
||||
if (client.LoggedinUser.Id != sender.LoggedinUser.Id)
|
||||
|
@ -3702,7 +3694,7 @@ namespace HISP.Server
|
|||
byte[] yourPlayerInfo = PacketBuilder.CreatePlayerInfoUpdateOrCreate(sender.LoggedinUser.X, sender.LoggedinUser.Y, sender.LoggedinUser.Facing, sender.LoggedinUser.CharacterId, sender.LoggedinUser.Username);
|
||||
byte[] yourPlayerInfoOffscreen = PacketBuilder.CreatePlayerInfoUpdateOrCreate(1000 + 4, 1000 + 1, sender.LoggedinUser.Facing, sender.LoggedinUser.CharacterId, sender.LoggedinUser.Username);
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
|
@ -5687,7 +5679,6 @@ namespace HISP.Server
|
|||
Chat.ChatChannel channel = (Chat.ChatChannel)packet[1];
|
||||
string message = packetStr.Substring(2, packetStr.Length - 4);
|
||||
|
||||
|
||||
Logger.DebugPrint(sender.LoggedinUser.Username + " Attempting to say '" + message + "' in channel: " + channel.ToString());
|
||||
|
||||
string nameTo = null;
|
||||
|
@ -5697,14 +5688,11 @@ namespace HISP.Server
|
|||
message = Chat.GetDmMessage(message);
|
||||
}
|
||||
|
||||
if (message == "")
|
||||
return;
|
||||
|
||||
if(message.StartsWith("/"))
|
||||
if (message.StartsWith("/"))
|
||||
{
|
||||
string channelString = message.Split(' ')[0].ToLower();
|
||||
string newMessage = string.Join(' ', message.Split(' ').Skip(1));
|
||||
message = newMessage;
|
||||
message = newMessage.Trim();
|
||||
switch(channelString)
|
||||
{
|
||||
case "/$":
|
||||
|
@ -5748,11 +5736,20 @@ namespace HISP.Server
|
|||
nameTo = channelString.Substring(1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (message == "") // this is how pinto does it, im serious.
|
||||
{
|
||||
channel = Chat.ChatChannel.Dm;
|
||||
nameTo = channelString.Substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
message = message.Trim();
|
||||
|
||||
if(channel == Chat.ChatChannel.All && message.Length > 150)
|
||||
if (message == "")
|
||||
return;
|
||||
|
||||
if (channel == Chat.ChatChannel.All && message.Length > 150)
|
||||
{
|
||||
byte[] tooLong = PacketBuilder.CreateChat(Messages.GlobalChatTooLong, PacketBuilder.CHAT_BOTTOM_RIGHT);
|
||||
sender.SendPacket(tooLong);
|
||||
|
@ -6573,7 +6570,7 @@ namespace HISP.Server
|
|||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
Logger.ErrorPrint(sender.LoggedinUser.Username + " Sent an invalid object interaction packet.");
|
||||
Logger.ErrorPrint(sender.LoggedinUser.Username + " Sent an invalid object interaction packet. (drop)");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -6592,6 +6589,8 @@ namespace HISP.Server
|
|||
byte[] chatPacket = PacketBuilder.CreateChat(Messages.DroppedAnItemMessage, PacketBuilder.CHAT_BOTTOM_RIGHT);
|
||||
sender.SendPacket(chatPacket);
|
||||
UpdateInventory(sender);
|
||||
|
||||
UpdateAreaForAll(sender.LoggedinUser.X, sender.LoggedinUser.Y, false, sender.LoggedinUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -7283,10 +7282,6 @@ namespace HISP.Server
|
|||
|
||||
public static void OnDisconnect(GameClient sender)
|
||||
{
|
||||
|
||||
connectedClients.Remove(sender);
|
||||
|
||||
|
||||
if (sender.LoggedIn)
|
||||
{
|
||||
Database.SetPlayerLastLogin(Convert.ToInt32(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()), sender.LoggedinUser.Id); // Set last login date
|
||||
|
@ -7321,7 +7316,7 @@ namespace HISP.Server
|
|||
|
||||
// Send disconnect message
|
||||
byte[] logoutMessageBytes = PacketBuilder.CreateChat(Messages.FormatLogoutMessage(sender.LoggedinUser.Username), PacketBuilder.CHAT_BOTTOM_LEFT);
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
if (!client.LoggedinUser.MuteLogins && !client.LoggedinUser.MuteAll)
|
||||
if (client.LoggedinUser.Id != sender.LoggedinUser.Id)
|
||||
|
@ -7329,13 +7324,12 @@ namespace HISP.Server
|
|||
|
||||
// Tell clients of diconnect (remove from chat)
|
||||
byte[] playerRemovePacket = PacketBuilder.CreatePlayerLeavePacket(sender.LoggedinUser.Username);
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
if (client.LoggedinUser.Id != sender.LoggedinUser.Id)
|
||||
client.SendPacket(playerRemovePacket);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -7359,7 +7353,7 @@ namespace HISP.Server
|
|||
public static User[] GetUsersInTown(World.Town town, bool includeStealth = false, bool includeMuted = false)
|
||||
{
|
||||
List<User> usersInTown = new List<User>();
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (!includeStealth && client.LoggedinUser.Stealth)
|
||||
|
@ -7376,7 +7370,7 @@ namespace HISP.Server
|
|||
public static User[] GetUsersInIsle(World.Isle isle, bool includeStealth = false, bool includeMuted = false)
|
||||
{
|
||||
List<User> usersInIsle = new List<User>();
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (!includeStealth && client.LoggedinUser.Stealth)
|
||||
|
@ -7395,7 +7389,7 @@ namespace HISP.Server
|
|||
{
|
||||
List<User> userList = new List<User>();
|
||||
|
||||
foreach (GameClient client in connectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
|
@ -7416,7 +7410,7 @@ namespace HISP.Server
|
|||
public static User[] GetUsersAt(int x, int y, bool includeStealth = false, bool includeMuted = false)
|
||||
{
|
||||
List<User> usersHere = new List<User>();
|
||||
foreach(GameClient client in ConnectedClients)
|
||||
foreach(GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if(client.LoggedIn)
|
||||
{
|
||||
|
@ -7432,7 +7426,7 @@ namespace HISP.Server
|
|||
}
|
||||
public static User GetUserByName(string username)
|
||||
{
|
||||
foreach(GameClient client in ConnectedClients)
|
||||
foreach(GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if(client.LoggedIn)
|
||||
{
|
||||
|
@ -7445,7 +7439,7 @@ namespace HISP.Server
|
|||
|
||||
public static User GetUserById(int id)
|
||||
{
|
||||
foreach(GameClient client in ConnectedClients)
|
||||
foreach(GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
if (client.LoggedinUser.Id == id)
|
||||
|
@ -7463,7 +7457,7 @@ namespace HISP.Server
|
|||
int endY = y + 3;
|
||||
List<User> usersNearby = new List<User>();
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (startX <= client.LoggedinUser.X && endX >= client.LoggedinUser.X && startY <= client.LoggedinUser.Y && endY >= client.LoggedinUser.Y)
|
||||
|
@ -7490,7 +7484,7 @@ namespace HISP.Server
|
|||
|
||||
List<User> usersOnScreen = new List<User>();
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (!includeStealth && client.LoggedinUser.Stealth)
|
||||
|
@ -7512,7 +7506,7 @@ namespace HISP.Server
|
|||
int endY = y + 19;
|
||||
List<User> usersNearby = new List<User>();
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (!includeStealth && client.LoggedinUser.Stealth)
|
||||
|
@ -7528,7 +7522,7 @@ namespace HISP.Server
|
|||
public static int GetNumberOfPlayers(bool includeStealth=false)
|
||||
{
|
||||
int count = 0;
|
||||
foreach(GameClient client in ConnectedClients)
|
||||
foreach(GameClient client in GameClient.ConnectedClients)
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
if (!includeStealth && client.LoggedinUser.Stealth)
|
||||
|
@ -7544,7 +7538,7 @@ namespace HISP.Server
|
|||
{
|
||||
List<Point> allLocations = new List<Point>();
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
|
@ -7566,7 +7560,7 @@ namespace HISP.Server
|
|||
{
|
||||
List<Point> allLocations = new List<Point>();
|
||||
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
{
|
||||
|
@ -7586,7 +7580,7 @@ namespace HISP.Server
|
|||
public static int GetNumberOfPlayersListeningToAdsChat()
|
||||
{
|
||||
int count = 0;
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
if (!client.LoggedinUser.MuteAds)
|
||||
|
@ -7598,7 +7592,7 @@ namespace HISP.Server
|
|||
public static int GetNumberOfModsOnline()
|
||||
{
|
||||
int count = 0;
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
if(client.LoggedinUser.Moderator)
|
||||
|
@ -7623,7 +7617,7 @@ namespace HISP.Server
|
|||
public static int GetNumberOfAdminsOnline()
|
||||
{
|
||||
int count = 0;
|
||||
foreach (GameClient client in ConnectedClients)
|
||||
foreach (GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
if (client.LoggedinUser.Administrator)
|
||||
|
@ -7740,7 +7734,7 @@ namespace HISP.Server
|
|||
}
|
||||
public static void UpdateAreaForAll(int x, int y, bool ignoreMetaPrio=false, User exceptMe=null)
|
||||
{
|
||||
foreach(GameClient client in ConnectedClients)
|
||||
foreach(GameClient client in GameClient.ConnectedClients)
|
||||
{
|
||||
if (client.LoggedIn)
|
||||
if (client.LoggedinUser.X == x && client.LoggedinUser.Y == y)
|
||||
|
@ -7856,7 +7850,7 @@ namespace HISP.Server
|
|||
public static void RemoveAllItemsOfIdInTheGame(int id)
|
||||
{
|
||||
// Remove from all online players
|
||||
foreach (GameClient connectedClient in GameServer.ConnectedClients)
|
||||
foreach (GameClient connectedClient in GameClient.ConnectedClients)
|
||||
{
|
||||
if (connectedClient.LoggedIn)
|
||||
if (connectedClient.LoggedinUser.Inventory.HasItemId(id))
|
||||
|
@ -8042,22 +8036,15 @@ namespace HISP.Server
|
|||
IPAddress hostIP = IPAddress.Parse(ConfigReader.BindIP);
|
||||
IPEndPoint ep = new IPEndPoint(hostIP, ConfigReader.Port);
|
||||
ServerSocket.Bind(ep);
|
||||
Logger.InfoPrint("Binding to ip: " + ConfigReader.BindIP + " On port: " + ConfigReader.Port.ToString());
|
||||
ServerSocket.Listen(10000);
|
||||
ServerSocket.Listen(0x7fffffff);
|
||||
gameTimer = new Timer(new TimerCallback(onGameTick), null, gameTickSpeed, gameTickSpeed);
|
||||
minuteTimer = new Timer(new TimerCallback(onMinuteTick), null, oneMinute, oneMinute);
|
||||
while (true)
|
||||
{
|
||||
Logger.InfoPrint("Waiting for new connections...");
|
||||
Logger.InfoPrint("Binding to ip: " + ConfigReader.BindIP + " On port: " + ConfigReader.Port.ToString());
|
||||
|
||||
Socket cientSocket = ServerSocket.Accept();
|
||||
GameClient client = new GameClient(cientSocket);
|
||||
connectedClients.Add(client);
|
||||
}
|
||||
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
|
||||
e.Completed += GameClient.CreateClient;
|
||||
GameClient.AcceptConnections(e, ServerSocket);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -340,10 +340,12 @@ namespace HISP.Server
|
|||
public static byte[] CreateDrawingUpdatePacket(string Drawing)
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
|
||||
ms.WriteByte(PACKET_SWFMODULE);
|
||||
byte[] drawingBytes = Encoding.UTF8.GetBytes(Drawing);
|
||||
ms.Write(drawingBytes, 0x00, drawingBytes.Length);
|
||||
ms.WriteByte(PACKET_TERMINATOR);
|
||||
|
||||
ms.Seek(0x00, SeekOrigin.Begin);
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ namespace HISP.Server
|
|||
}
|
||||
public static string GetCommitHash(int TotalBytes)
|
||||
{
|
||||
return Resources.GitCommit.Substring(0, TotalBytes * 2);
|
||||
return Resources.GitCommit.Substring(0, TotalBytes);
|
||||
}
|
||||
public static string GetBuildString()
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue