Merge remote-tracking branch 'origin/develop' into newDoctor
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using Util;
|
||||
using System.Linq;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
@@ -96,16 +97,17 @@ namespace Server
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN:
|
||||
handleLogin(payloadbytes);
|
||||
if (handleLogin(payloadbytes))
|
||||
communication.NewLogin(this);
|
||||
break;
|
||||
case DataParser.LOGIN_DOCTOR:
|
||||
if (communication.doctor != null)
|
||||
if (communication.Doctor != null)
|
||||
return;
|
||||
|
||||
if (handleLogin(payloadbytes))
|
||||
{
|
||||
communication.doctor = this;
|
||||
Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this);
|
||||
communication.Doctor = this;
|
||||
Console.WriteLine("Set doctor to " + communication.Doctor + " , this is " + this);
|
||||
}
|
||||
break;
|
||||
case DataParser.START_SESSION:
|
||||
@@ -135,7 +137,7 @@ namespace Server
|
||||
else if (DataParser.isRawData(message))
|
||||
{
|
||||
// print the raw data
|
||||
Console.WriteLine(BitConverter.ToString(payloadbytes));
|
||||
//Console.WriteLine(BitConverter.ToString(payloadbytes));
|
||||
// TODO change, checking for length is not that safe
|
||||
if (payloadbytes.Length == 8)
|
||||
{
|
||||
@@ -167,7 +169,6 @@ namespace Server
|
||||
this.username = username;
|
||||
sendMessage(DataParser.getLoginResponse("OK"));
|
||||
//sendMessage(DataParser.getStartSessionJson());
|
||||
communication.NewLogin(this);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@@ -184,6 +185,7 @@ namespace Server
|
||||
|
||||
public void sendMessage(byte[] message)
|
||||
{
|
||||
Debug.WriteLine("serverclient " + Encoding.ASCII.GetString(message.Skip(5).ToArray()));
|
||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
@@ -12,7 +13,25 @@ namespace Server
|
||||
{
|
||||
private TcpListener listener;
|
||||
private List<Client> clients;
|
||||
public Client doctor;
|
||||
private Client mDoctor;
|
||||
public Client Doctor
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.mDoctor;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.mDoctor = value;
|
||||
this.clients.ForEach((client) =>
|
||||
{
|
||||
Debug.WriteLine("foreach called for " + client.username);
|
||||
byte[] dinges = DataParser.getNewConnectionJson(client.username);
|
||||
Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges.Skip(5).ToArray()));
|
||||
this.mDoctor.sendMessage(dinges);
|
||||
});
|
||||
}
|
||||
}
|
||||
public Communication(TcpListener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
@@ -33,7 +52,7 @@ namespace Server
|
||||
|
||||
var tcpClient = listener.EndAcceptTcpClient(ar);
|
||||
Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}");
|
||||
clients.Add(new Client(this, tcpClient));
|
||||
new Client(this, tcpClient);
|
||||
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
@@ -45,15 +64,20 @@ namespace Server
|
||||
|
||||
public void NewLogin(Client client)
|
||||
{
|
||||
if (doctor == null)
|
||||
this.clients.Add(client);
|
||||
Debug.WriteLine("amount of clients is now " + this.clients.Count);
|
||||
var dinges = DataParser.getNewConnectionJson(client.username);
|
||||
Debug.WriteLine("new login" + Encoding.ASCII.GetString(dinges));
|
||||
Doctor?.sendMessage(dinges);
|
||||
}
|
||||
|
||||
public void LogOff(Client client)
|
||||
{
|
||||
if (this.Doctor == client)
|
||||
{
|
||||
doctor = client;
|
||||
this.Doctor = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
doctor.sendMessage(DataParser.getNewConnectionJson(client.username));
|
||||
}
|
||||
|
||||
this.clients.Remove(client);
|
||||
}
|
||||
|
||||
public void StartSessionUser(string user)
|
||||
|
||||
Reference in New Issue
Block a user