Initiate server (+Basic connection)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="SERVER_IP" value="localhost"/>
|
||||
<add key="PORT" value="25565"/>
|
||||
<add key="PSWD" value="password"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
+32
-1
@@ -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<string, string> 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";
|
||||
|
||||
|
||||
@@ -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<string, string> ReadAllSettings()
|
||||
{
|
||||
Dictionary<string, string> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<EmbeddedResource Include="Icon.bmp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LiteNetLib" Version="0.9.5.2" />
|
||||
<PackageReference Include="MonoGame.Extended" Version="3.8.0" />
|
||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
|
||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="PORT" value="25565"/>
|
||||
<add key="PSWD" value="password"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public class Lib
|
||||
{
|
||||
public static Dictionary<string, string> ReadAllSettings()
|
||||
{
|
||||
Dictionary<string, string> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LiteNetLib" Version="0.9.5.2" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user