diff --git a/client/App.config b/client/App.config new file mode 100644 index 0000000..c1829f3 --- /dev/null +++ b/client/App.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/client/Game1.cs b/client/Game1.cs index 15e8ac5..c8084ae 100644 --- a/client/Game1.cs +++ b/client/Game1.cs @@ -1,10 +1,19 @@ -using Microsoft.Xna.Framework; +using LiteNetLib; +using LiteNetLib.Utils; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; namespace client { public class Game1 : Game { + // Network + EventBasedNetListener listener = new(); + NetManager client; + NetPeer server; + // Game instances private Player pl1; private Player pl2; @@ -23,10 +32,32 @@ namespace client _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; + + Dictionary settings = Lib.ReadAllSettings(); + if (!settings.ContainsKey("SERVER_IP") || (settings.ContainsKey("SERVER_IP") && Lib.ReadSetting("SERVER_IP") == null)) + { + Console.Error.WriteLine("Server IP missing in config file!"); + } else if (!settings.ContainsKey("PORT") || (settings.ContainsKey("PORT") && Lib.ReadSetting("PORT") == null)) + { + Console.Error.WriteLine("Port missing in config file!"); + } else if (!settings.ContainsKey("PSWD") || (settings.ContainsKey("PSWD") && Lib.ReadSetting("PSWD") == null)) + { + Console.Error.WriteLine("Server password missing in config file!"); + } } protected override void Initialize() { + // Network + client = new NetManager(listener); + client.Start(); + server = client.Connect(Lib.ReadSetting("SERVER_IP"), int.Parse(Lib.ReadSetting("PORT")), Lib.ReadSetting("PSWD")); + if (server == null) + { + // TODO: Replace by a error message :) + Exit(); + } + // Window Window.Title = "Pong"; diff --git a/client/Lib/Lib.cs b/client/Lib/Lib.cs new file mode 100644 index 0000000..9f9e6f8 --- /dev/null +++ b/client/Lib/Lib.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace client +{ + public class Lib + { + public static Dictionary ReadAllSettings() + { + Dictionary settings = new(); + try + { + var appSettings = ConfigurationManager.AppSettings; + + if (appSettings.Count == 0) + { + Console.Error.WriteLine("AppSettings is empty."); + return settings; + } + else + { + foreach (var key in appSettings.AllKeys) + { + settings.Add(key, appSettings[key]); + } + } + return settings; + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error reading app settings"); + return settings; + } + } + + public static string? ReadSetting(string key) + { + try + { + var appSettings = ConfigurationManager.AppSettings; + string? result = appSettings[key] ?? null; + return result; + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error reading app settings"); + return null; + } + } + + public static void AddUpdateAppSettings(string key, string value) + { + try + { + var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + var settings = configFile.AppSettings.Settings; + if (settings[key] == null) + { + settings.Add(key, value); + } + else + { + settings[key].Value = value; + } + configFile.Save(ConfigurationSaveMode.Modified); + ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error writing app settings"); + } + } + } +} diff --git a/client/client.csproj b/client/client.csproj index 32e5c5c..591848b 100644 --- a/client/client.csproj +++ b/client/client.csproj @@ -19,6 +19,7 @@ + diff --git a/server/.gitkeep b/server/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/server/App.config b/server/App.config new file mode 100644 index 0000000..dc9608c --- /dev/null +++ b/server/App.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/server/Lib/Lib.cs b/server/Lib/Lib.cs new file mode 100644 index 0000000..befa4be --- /dev/null +++ b/server/Lib/Lib.cs @@ -0,0 +1,73 @@ +using System.Configuration; + +namespace Server +{ + public class Lib + { + public static Dictionary ReadAllSettings() + { + Dictionary settings = new(); + try + { + var appSettings = ConfigurationManager.AppSettings; + + if (appSettings.Count == 0) + { + Console.Error.WriteLine("AppSettings is empty."); + return settings; + } + else + { + foreach (var key in appSettings.AllKeys) + { + settings.Add(key, appSettings[key]); + } + } + return settings; + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error reading app settings"); + return settings; + } + } + + public static string? ReadSetting(string key) + { + try + { + var appSettings = ConfigurationManager.AppSettings; + string? result = appSettings[key] ?? null; + return result; + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error reading app settings"); + return null; + } + } + + public static void AddUpdateAppSettings(string key, string value) + { + try + { + var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + var settings = configFile.AppSettings.Settings; + if (settings[key] == null) + { + settings.Add(key, value); + } + else + { + settings[key].Value = value; + } + configFile.Save(ConfigurationSaveMode.Modified); + ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); + } + catch (ConfigurationErrorsException) + { + Console.Error.WriteLine("Error writing app settings"); + } + } + } +} diff --git a/server/Program.cs b/server/Program.cs new file mode 100644 index 0000000..386dbe0 --- /dev/null +++ b/server/Program.cs @@ -0,0 +1,60 @@ +using LiteNetLib; +using LiteNetLib.Utils; + +namespace Server +{ + public class Program + { + static EventBasedNetListener listener = new(); + static NetManager? server; + + static string? port; + static string? pswd; + + public static void Main(string[] args) + { + server = new NetManager(listener); + port = Lib.ReadSetting("PORT"); + pswd = Lib.ReadSetting("PSWD"); + + if (port != null && pswd != null) + { + Console.WriteLine($"Server started on port {port}"); + server.Start(int.Parse(port)); + } + else + { + Console.Error.WriteLine("Missing port or password in config file !\nClosing server..."); + return; + } + + + listener.ConnectionRequestEvent += request => + { + Console.WriteLine($"New connection request from \"{request.RemoteEndPoint}\""); + request.AcceptIfKey(pswd); + }; + + listener.PeerConnectedEvent += peer => + { + Console.WriteLine("Connection accepted for: {0}", peer.EndPoint); + /* NetDataWriter writer = new(); + writer.Put("Hello client!"); + peer.Send(writer, DeliveryMethod.ReliableOrdered); */ + }; + + listener.PeerDisconnectedEvent += (peer, infos) => + { + Console.WriteLine($"{peer.EndPoint} has left.\nError code: {infos.SocketErrorCode}\nReason: {infos.Reason}"); + }; + + while (!Console.KeyAvailable) + { + server.PollEvents(); + Thread.Sleep(15); + } + + server.Stop(); + } + } +} \ No newline at end of file diff --git a/server/server.csproj b/server/server.csproj new file mode 100644 index 0000000..b25b491 --- /dev/null +++ b/server/server.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/server/server.sln b/server/server.sln new file mode 100644 index 0000000..48ee81d --- /dev/null +++ b/server/server.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server", "server.csproj", "{AB011134-CF45-4362-B3AC-5F11D9C39B80}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B988A71C-6FD9-4115-805D-94C7CB8E3893} + EndGlobalSection +EndGlobal