Initiate server (+Basic connection)
This commit is contained in:
@@ -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