Better connection handling
This commit is contained in:
@@ -75,7 +75,6 @@ namespace ErgometerServer
|
|||||||
{
|
{
|
||||||
name = input.DisplayName;
|
name = input.DisplayName;
|
||||||
loggedin = true;
|
loggedin = true;
|
||||||
Console.WriteLine(name + " (client) connected with password: " + input.Password);
|
|
||||||
FileHandler.CreateSession(session, name);
|
FileHandler.CreateSession(session, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,7 +94,6 @@ namespace ErgometerServer
|
|||||||
{
|
{
|
||||||
chat.Add(new ChatMessage(name, input.ChatMessage, false));
|
chat.Add(new ChatMessage(name, input.ChatMessage, false));
|
||||||
server.SendToDoctor(input);
|
server.SendToDoctor(input);
|
||||||
Console.WriteLine(name + ": " + input.ChatMessage);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
|
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
|
||||||
@@ -108,8 +106,17 @@ namespace ErgometerServer
|
|||||||
FileHandler.WriteChat(session, chat);
|
FileHandler.WriteChat(session, chat);
|
||||||
client.Close();
|
client.Close();
|
||||||
break;
|
break;
|
||||||
|
case NetCommand.CommandType.ERROR:
|
||||||
|
Console.WriteLine("An error occured, assuming client disconnected");
|
||||||
|
loggedin = false;
|
||||||
|
running = false;
|
||||||
|
Console.WriteLine(name + " logged out due to an error");
|
||||||
|
FileHandler.WriteMetingen(session, metingen);
|
||||||
|
FileHandler.WriteChat(session, chat);
|
||||||
|
client.Close();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FormatException("Unknown command");
|
throw new FormatException("Unknown command");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,12 @@ namespace ErgometerServer
|
|||||||
throw new FormatException("Unknown Command");
|
throw new FormatException("Unknown Command");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case NetCommand.CommandType.ERROR:
|
||||||
|
Console.WriteLine("An error occured, assuming docter disconnected");
|
||||||
|
running = false;
|
||||||
|
Console.WriteLine("Doctor logged out due to an error");
|
||||||
|
client.Close();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FormatException("Unknown Command");
|
throw new FormatException("Unknown Command");
|
||||||
|
|||||||
@@ -37,6 +37,10 @@
|
|||||||
<SpecificVersion>False</SpecificVersion>
|
<SpecificVersion>False</SpecificVersion>
|
||||||
<HintPath>..\..\..\ErgometerLibrary\ErgometerLibrary\ErgometerLibrary\bin\Debug\ErgometerLibrary.dll</HintPath>
|
<HintPath>..\..\..\ErgometerLibrary\ErgometerLibrary\ErgometerLibrary\bin\Debug\ErgometerLibrary.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
@@ -49,11 +53,13 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ClientThread.cs" />
|
<Compile Include="ClientThread.cs" />
|
||||||
<Compile Include="DoctorThread.cs" />
|
<Compile Include="DoctorThread.cs" />
|
||||||
|
<Compile Include="FileHandler.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Server.cs" />
|
<Compile Include="Server.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
|||||||
@@ -0,0 +1,165 @@
|
|||||||
|
using ErgometerLibrary;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace ErgometerServer
|
||||||
|
{
|
||||||
|
public class FileHandler
|
||||||
|
{
|
||||||
|
public static string DataFolder { get; } = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Ergometer");
|
||||||
|
public static string UsersFile { get; } = Path.Combine(DataFolder, "users.ergo");
|
||||||
|
|
||||||
|
public static void CheckStorage()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(DataFolder))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(DataFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!File.Exists(UsersFile))
|
||||||
|
{
|
||||||
|
using (Stream stream = File.Open(UsersFile, FileMode.Create))
|
||||||
|
{
|
||||||
|
BinaryWriter writer = new BinaryWriter(stream);
|
||||||
|
writer.Write(1);
|
||||||
|
writer.Write("Doctor0tVfW");
|
||||||
|
writer.Write("password");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//SESSION
|
||||||
|
public static int GenerateSession()
|
||||||
|
{
|
||||||
|
string[] existingSessions = Directory.GetDirectories(DataFolder);
|
||||||
|
|
||||||
|
Random rand = new Random();
|
||||||
|
int sessionID = rand.Next(0, int.MaxValue);
|
||||||
|
|
||||||
|
|
||||||
|
while (existingSessions.Contains(sessionID.ToString()))
|
||||||
|
{
|
||||||
|
sessionID = rand.Next(int.MinValue, int.MaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sessionID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CreateSession(int session, string naam)
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(GetSessionFolder(session));
|
||||||
|
|
||||||
|
using (File.Create(Path.Combine(GetSessionFile(session)))) ;
|
||||||
|
using (File.Create(Path.Combine(GetSessionMetingen(session)))) ;
|
||||||
|
using (File.Create(Path.Combine(GetSessionChat(session)))) ;
|
||||||
|
|
||||||
|
File.WriteAllText(GetSessionFile(session), naam + Environment.NewLine + Helper.Now);
|
||||||
|
Console.WriteLine("Created session at " + Helper.MillisecondsToTime(Helper.Now));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteMetingen(int session, List<Meting> metingen)
|
||||||
|
{
|
||||||
|
if (metingen.Count <= 15 && Directory.Exists(GetSessionFolder(session)))
|
||||||
|
{
|
||||||
|
Directory.Delete(GetSessionFolder(session), true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(metingen);
|
||||||
|
File.WriteAllText(GetSessionMetingen(session), json);
|
||||||
|
Console.WriteLine("Writing metingen: " + GetSessionMetingen(session));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Meting> ReadMetingen(int session)
|
||||||
|
{
|
||||||
|
object metingString = Newtonsoft.Json.JsonConvert.DeserializeObject(GetSessionMetingen(session));
|
||||||
|
Console.WriteLine("Reading metingen: " + GetSessionMetingen(session));
|
||||||
|
return (List<Meting>)metingString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] GetAllSessions()
|
||||||
|
{
|
||||||
|
string[] directories = Directory.GetDirectories(DataFolder);
|
||||||
|
int[] sessions = new int[directories.Length];
|
||||||
|
for (int i = 0; i < directories.Length; i++)
|
||||||
|
{
|
||||||
|
sessions[i] = int.Parse(directories[i]);
|
||||||
|
}
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteChat(int session, List<ChatMessage> chat)
|
||||||
|
{
|
||||||
|
if (chat.Count <= 15 && Directory.Exists(GetSessionFolder(session)))
|
||||||
|
{
|
||||||
|
Directory.Delete(GetSessionFolder(session), true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string write = "";
|
||||||
|
foreach (ChatMessage c in chat)
|
||||||
|
{
|
||||||
|
write += c.ToString() + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(GetSessionChat(session), write);
|
||||||
|
Console.WriteLine("Writing chat: " + GetSessionChat(session));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetSessionFolder(int session)
|
||||||
|
{
|
||||||
|
return Path.Combine(DataFolder, session.ToString());
|
||||||
|
}
|
||||||
|
private static string GetSessionFile(int session)
|
||||||
|
{
|
||||||
|
return Path.Combine(DataFolder, session.ToString(), "session.prop");
|
||||||
|
}
|
||||||
|
private static string GetSessionMetingen(int session)
|
||||||
|
{
|
||||||
|
return Path.Combine(DataFolder, session.ToString(), "metingen.ergo");
|
||||||
|
}
|
||||||
|
private static string GetSessionChat(int session)
|
||||||
|
{
|
||||||
|
return Path.Combine(DataFolder, session.ToString(), "chat.log");
|
||||||
|
}
|
||||||
|
|
||||||
|
//USER MANAGEMENT
|
||||||
|
public static Dictionary<string, string> LoadUsers()
|
||||||
|
{
|
||||||
|
Dictionary<string, string> users = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
using (Stream stream = File.Open(UsersFile, FileMode.Open))
|
||||||
|
{
|
||||||
|
BinaryReader reader = new BinaryReader(stream);
|
||||||
|
int count = reader.ReadInt32();
|
||||||
|
for (int n = 0; n < count; n++)
|
||||||
|
{
|
||||||
|
var key = reader.ReadString();
|
||||||
|
var value = reader.ReadString();
|
||||||
|
users.Add(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SaveUsers(Dictionary<string, string> users)
|
||||||
|
{
|
||||||
|
using (Stream stream = File.Open(UsersFile, FileMode.Open))
|
||||||
|
{
|
||||||
|
BinaryWriter writer = new BinaryWriter(stream);
|
||||||
|
writer.Write(users.Count);
|
||||||
|
foreach (var kvp in users)
|
||||||
|
{
|
||||||
|
writer.Write(kvp.Key);
|
||||||
|
writer.Write(kvp.Value);
|
||||||
|
}
|
||||||
|
writer.Flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -85,10 +85,6 @@ namespace ErgometerServer
|
|||||||
{
|
{
|
||||||
doctor.sendToDoctor(command);
|
doctor.sendToDoctor(command);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("No doctor connected to the server yet");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void BroadcastToClients(NetCommand command)
|
public void BroadcastToClients(NetCommand command)
|
||||||
@@ -131,8 +127,6 @@ namespace ErgometerServer
|
|||||||
string pass;
|
string pass;
|
||||||
bool isOk = users.TryGetValue(name, out pass);
|
bool isOk = users.TryGetValue(name, out pass);
|
||||||
|
|
||||||
Console.WriteLine($"Checking {name}, {password}: found {isOk} with response {pass}");
|
|
||||||
|
|
||||||
if (!isOk) return false;
|
if (!isOk) return false;
|
||||||
|
|
||||||
return pass == password;
|
return pass == password;
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
|
||||||
|
</packages>
|
||||||
Reference in New Issue
Block a user