Fixed busy waits

This commit is contained in:
Kyle Cannoles 2016-04-27 00:56:39 -10:00
parent e23f4b2846
commit 2eb3a1bd04
2 changed files with 176 additions and 206 deletions

View File

@ -34,24 +34,6 @@ namespace Server_Application_Console
this.serverID = serverID;
}
/*
public static void setConnectExpected(bool expected)
{
if(expected)
{
connectExpected.Set();
}
else
{
connectExpected.Reset();
}
}
public static bool getConnectExpected()
{
return connectExpected.IsSet;
}
*/
public static uint getCurrentID()
{
return currentID;
@ -85,7 +67,7 @@ namespace Server_Application_Console
while (running)
{
//checks if a new client is waiting to connect
if (listener.Pending() && currentID == serverID)
if (currentID == serverID)
{
//connects to client
TcpClient client = await listener.AcceptTcpClientAsync();
@ -131,26 +113,22 @@ namespace Server_Application_Console
while (thisClient.Connected && running)
{
//checks if data is available on the clients stream
if (inStream.DataAvailable)
message = reader.ReadLine();
//sends data to all clients in chat
foreach (KeyValuePair<TcpClient, string> c in clientList)
{
message = reader.ReadLine();
//sends data to all clients in chat
foreach (KeyValuePair<TcpClient, string> c in clientList)
{
string clientName;
string clientName;
//creates StreamWriter for current outgoing client
outStream = c.Key.GetStream();
writer = new StreamWriter(outStream);
//appends sender name to beginning of message
clientList.TryGetValue(thisClient, out clientName);
//writes sender and message to outgoing stream
writer.WriteLine(clientName + ": " + message.ToString());
Console.WriteLine(clientName + ": " + message.ToString());
writer.Flush();
}
//creates StreamWriter for current outgoing client
outStream = c.Key.GetStream();
writer = new StreamWriter(outStream);
//appends sender name to beginning of message
clientList.TryGetValue(thisClient, out clientName);
//writes sender and message to outgoing stream
writer.WriteLine(clientName + ": " + message.ToString());
Console.WriteLine(clientName + ": " + message.ToString());
writer.Flush();
}
}
if (writer != null) { writer.Close(); }

View File

@ -44,30 +44,26 @@ namespace Server_Application_Console
while (running)
{
//checks if a new client is waiting to connect
//if (listener.Pending())
// {
//connects to client
//currently connects to any client, possibly add security later
TcpClient client = await listener.AcceptTcpClientAsync();
//TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
BinaryReader reader = new BinaryReader(stream);
byte type = reader.ReadByte();
//connects to client
//currently connects to any client, possibly add security later
TcpClient client = await listener.AcceptTcpClientAsync();
NetworkStream stream = client.GetStream();
BinaryReader reader = new BinaryReader(stream);
byte type = reader.ReadByte();
//client type sends requests to server
if (type == 0)
{
Thread t = new Thread(transmitter);
t.Start(client);
}
//client type waits for requests from server
else if (type == 1)
{
uint thisClientID = reader.ReadUInt32();
//adds client to a dictionary for use
clientList.Add(thisClientID, client);
}
//}
//client type sends requests to server
if (type == 0)
{
Thread t = new Thread(transmitter);
t.Start(client);
}
//client type waits for requests from server
else if (type == 1)
{
uint thisClientID = reader.ReadUInt32();
//adds client to a dictionary for use
clientList.Add(thisClientID, client);
}
}
}
@ -85,164 +81,160 @@ namespace Server_Application_Console
//control while client connected and server running
while (thisClient.Connected && running)
{
//checks if data is available on the clients stream
if (stream.DataAvailable)
//gets the type of request sent by the client
byte reqType = reader.ReadByte();
switch (reqType)
{
//gets the type of request sent by the client
byte reqType = reader.ReadByte();
switch (reqType)
{
//request for chat server
case 1:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
//request for chat server
case 1:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates chat server with serverCounter as its ID in new thread
Thread tC = new Thread(startChatServer);
tC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for resource server
case 2:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates resource server with serverCounter as its ID in new thread
Thread tR = new Thread(startResourceServer);
tR.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for real time collaboration server
case 3:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates real time collaboration server with serverCounter as its ID in new thread
Thread tRTC = new Thread(startRTCServer);
tRTC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request to connect a client to a server
case 4:
TcpClient connect;
//reads connection info from stream
byte serType = reader.ReadByte();
uint serverID = reader.ReadUInt32();
uint clientID = reader.ReadUInt32();
//checks if specified client connected, and gets corresponding TcpClient
if (clientList.TryGetValue(clientID, out connect))
{
//creates reader and writer on requested clients stream
NetworkStream cStream = connect.GetStream();
BinaryWriter cWriter = new BinaryWriter(cStream);
//indicates whether client successfully connected
bool success = false;
//checks the type of server connection request for
switch (serType)
{
//increments server counter
serverCounter++;
//creates chat server with serverCounter as its ID in new thread
Thread tC = new Thread(startChatServer);
tC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
//connection request for chat server
case 1:
//locks to ensure ChatServer's currentID is not changed
lock (ChatServer.IDLock)
{
//tells chat servers listener that incoming request is for server with specified ID
ChatServer.setCurrentID(serverID);
ChatServer.setExpectedClient(clientID);
//tells chat server that a connection is expected
ChatServer.connectExpected.Set();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
success = ChatServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
ChatServer.connectExpected.Reset();
ChatServer.connected.Reset();
}
break;
//connection request for resource server
case 2:
//structure same as chat server, see comments for case 1
lock (ResourceServer.IDLock)
{
ResourceServer.setCurrentID(serverID);
ResourceServer.setExpectedClient(clientID);
ResourceServer.connectExpected.Set();
cWriter.Write(serType);
cWriter.Write(serverID);
success = ResourceServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
ResourceServer.connectExpected.Reset();
ResourceServer.connected.Reset();
}
break;
//connection request for real time collaboration server
case 3:
//structure same as chat server, see comments for case 1
lock (RTCServer.IDLock)
{
RTCServer.setCurrentID(serverID);
RTCServer.setExpectedClient(clientID);
RTCServer.connectExpected.Set();
cWriter.Write(serType);
cWriter.Write(serverID);
success = RTCServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
RTCServer.connectExpected.Reset();
RTCServer.connected.Reset();
}
break;
}
break;
//request for resource server
case 2:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
//checks if requested client still connected
if (connect.Connected && success)
{
//increments server counter
serverCounter++;
//creates resource server with serverCounter as its ID in new thread
Thread tR = new Thread(startResourceServer);
tR.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for real time collaboration server
case 3:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates real time collaboration server with serverCounter as its ID in new thread
Thread tRTC = new Thread(startRTCServer);
tRTC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request to connect a client to a server
case 4:
TcpClient connect;
//reads connection info from stream
byte serType = reader.ReadByte();
uint serverID = reader.ReadUInt32();
uint clientID = reader.ReadUInt32();
//checks if specified client connected, and gets corresponding TcpClient
if (clientList.TryGetValue(clientID, out connect))
{
//creates reader and writer on requested clients stream
NetworkStream cStream = connect.GetStream();
BinaryWriter cWriter = new BinaryWriter(cStream);
//indicates whether client successfully connected
bool success = false;
//checks the type of server connection request for
switch (serType)
{
//connection request for chat server
case 1:
//locks to ensure ChatServer's currentID is not changed
lock (ChatServer.IDLock)
{
//tells chat servers listener that incoming request is for server with specified ID
ChatServer.setCurrentID(serverID);
ChatServer.setExpectedClient(clientID);
//tells chat server that a connection is expected
ChatServer.connectExpected.Set();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
success = ChatServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
ChatServer.connectExpected.Reset();
ChatServer.connected.Reset();
}
break;
//connection request for resource server
case 2:
//structure same as chat server, see comments for case 1
lock (ResourceServer.IDLock)
{
ResourceServer.setCurrentID(serverID);
ResourceServer.setExpectedClient(clientID);
ResourceServer.connectExpected.Set();
cWriter.Write(serType);
cWriter.Write(serverID);
success = ResourceServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
ResourceServer.connectExpected.Reset();
ResourceServer.connected.Reset();
}
break;
//connection request for real time collaboration server
case 3:
//structure same as chat server, see comments for case 1
lock (RTCServer.IDLock)
{
RTCServer.setCurrentID(serverID);
RTCServer.setExpectedClient(clientID);
RTCServer.connectExpected.Set();
cWriter.Write(serType);
cWriter.Write(serverID);
success = RTCServer.connected.Wait(TimeSpan.FromSeconds(5));
//ensures values reset regardless of outcome
RTCServer.connectExpected.Reset();
RTCServer.connected.Reset();
}
break;
}
//checks if requested client still connected
if (connect.Connected && success)
{
//tells requester client successfully connected if it is
writer.Write(true);
}
else
{
//else indicates connection unsuccessful
writer.Write(false);
}
}
//requested client not connected
else
{
//tells client connection request was unsuccessful
writer.Write(false);
}
break;
//request for status of indicated client
case 5:
TcpClient temp;
//checks if indicated client is in list of connected clients
if (clientList.TryGetValue(reader.ReadUInt32(), out temp))
{
//tells requester specified client is connected
//tells requester client successfully connected if it is
writer.Write(true);
}
else
{
//tells requester specified client is not connected
//else indicates connection unsuccessful
writer.Write(false);
}
break;
}
}
//requested client not connected
else
{
//tells client connection request was unsuccessful
writer.Write(false);
}
break;
//request for status of indicated client
case 5:
TcpClient temp;
//checks if indicated client is in list of connected clients
if (clientList.TryGetValue(reader.ReadUInt32(), out temp))
{
//tells requester specified client is connected
writer.Write(true);
}
else
{
//tells requester specified client is not connected
writer.Write(false);
}
break;
}
}