fixed bugs

This commit is contained in:
princessbobsanta 2016-04-13 01:20:00 -10:00
commit 0e82467079
18 changed files with 394 additions and 546 deletions

View File

@ -48,6 +48,7 @@ namespace GameCreatorGroupProject
reader = new StreamReader(stream);
//tells server clients username
writer.WriteLine(MainClient.getUsername());
writer.Flush();
}
else
{

View File

@ -1,114 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GameCreatorGroupProject
{
class ChatServer : TCPServer
{
private List<TcpClient> clientList = new List<TcpClient>();
private Dictionary<IPAddress, string> collaborators;
private bool running = true;
//chat port
private readonly int port = 20113;
//starts a chat server
public override void startServer(Project project)
{
//gets collaborator list from project
collaborators = project.Collaborators;
//attempts to start listener on chat port
try
{
//possible issues with conflicts if multiple chat servers running, to be fixed later
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
}
//displays error box if unable to start server
catch (SocketException e)
{
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//main server control
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 = listener.AcceptTcpClient();
clientList.Add(client);
//spawns new thread for handling data transmission to other clients
Thread t = new Thread(transmitter);
t.Start(client);
}
}
}
//sends messages to all clients
private void transmitter(Object client)
{
using (TcpClient thisClient = (TcpClient)client)
{
NetworkStream inStream = ((TcpClient)client).GetStream();
StringBuilder message = new StringBuilder();
StreamReader reader = new StreamReader(inStream);
StreamWriter writer = null;
NetworkStream outStream;
while (thisClient.Connected && running)
{
//checks if data is available on the clients stream
if (inStream.DataAvailable)
{
//sends data to all clients in chat
foreach (TcpClient c in clientList)
{
message.Clear();
IPAddress userIP;
string clientName;
//gets client IP, used to determine client name (to be changed)
userIP = ((IPEndPoint)thisClient.Client.RemoteEndPoint).Address;
//creates StreamWriter for current outgoing client
outStream = c.GetStream();
writer = new StreamWriter(outStream);
//appends sender name to beginning of message
collaborators.TryGetValue(userIP, out clientName);
message.Append(clientName);
message.Append(": ");
//appends outgoing message
message.Append(reader.ReadLine());
//writes message to outgoing stream
writer.WriteLine(message.ToString());
writer.Flush();
}
}
}
if (writer != null) { writer.Close(); }
if (reader != null) { reader.Close(); }
lock (clientList)
{
clientList.Remove(thisClient);
}
}
}
public override void stopServer()
{
if (listener != null) { listener.Stop(); }
running = false;
}
}
}

View File

@ -41,6 +41,7 @@
this.itemStartServer = new System.Windows.Forms.ToolStripMenuItem();
this.itemConnect = new System.Windows.Forms.ToolStripMenuItem();
this.itemDisconnect = new System.Windows.Forms.ToolStripMenuItem();
this.sendMessageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.ModeControlTabs = new System.Windows.Forms.TabControl();
this.ResourcesTab = new System.Windows.Forms.TabPage();
@ -152,6 +153,9 @@
this.txtMessage = new System.Windows.Forms.TextBox();
this.txtChat = new System.Windows.Forms.TextBox();
this.folderPrjDir = new System.Windows.Forms.FolderBrowserDialog();
this.addUserToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.addUserDebugToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.addUserReleaseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStrip1.SuspendLayout();
this.ModeControlTabs.SuspendLayout();
this.ResourcesTab.SuspendLayout();
@ -242,12 +246,14 @@
this.toolChat.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.itemStartServer,
this.itemConnect,
this.itemDisconnect});
this.itemDisconnect,
this.sendMessageToolStripMenuItem,
this.addUserToolStripMenuItem});
this.toolChat.ForeColor = System.Drawing.SystemColors.ControlLight;
this.toolChat.Image = ((System.Drawing.Image)(resources.GetObject("toolChat.Image")));
this.toolChat.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolChat.Name = "toolChat";
this.toolChat.Size = new System.Drawing.Size(78, 22);
this.toolChat.Size = new System.Drawing.Size(77, 22);
this.toolChat.Text = "Team Chat";
//
// itemStartServer
@ -270,6 +276,13 @@
this.itemDisconnect.Size = new System.Drawing.Size(177, 22);
this.itemDisconnect.Text = "Disconnect...";
//
// sendMessageToolStripMenuItem
//
this.sendMessageToolStripMenuItem.Name = "sendMessageToolStripMenuItem";
this.sendMessageToolStripMenuItem.Size = new System.Drawing.Size(177, 22);
this.sendMessageToolStripMenuItem.Text = "Send Message";
this.sendMessageToolStripMenuItem.Click += new System.EventHandler(this.sendMessageToolStripMenuItem_Click);
//
// statusStrip1
//
this.statusStrip1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
@ -1503,7 +1516,7 @@
// btnSendMsg
//
this.btnSendMsg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnSendMsg.Location = new System.Drawing.Point(1016, 141);
this.btnSendMsg.Location = new System.Drawing.Point(1016, 133);
this.btnSendMsg.Name = "btnSendMsg";
this.btnSendMsg.Size = new System.Drawing.Size(139, 23);
this.btnSendMsg.TabIndex = 2;
@ -1514,7 +1527,7 @@
//
this.txtMessage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtMessage.Location = new System.Drawing.Point(3, 141);
this.txtMessage.Location = new System.Drawing.Point(3, 133);
this.txtMessage.Name = "txtMessage";
this.txtMessage.Size = new System.Drawing.Size(1006, 20);
this.txtMessage.TabIndex = 1;
@ -1529,7 +1542,7 @@
this.txtChat.Name = "txtChat";
this.txtChat.ReadOnly = true;
this.txtChat.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtChat.Size = new System.Drawing.Size(1152, 132);
this.txtChat.Size = new System.Drawing.Size(1152, 124);
this.txtChat.TabIndex = 0;
//
// folderPrjDir
@ -1537,6 +1550,30 @@
this.folderPrjDir.Description = "Specify a folder to store the project in:";
this.folderPrjDir.RootFolder = System.Environment.SpecialFolder.MyDocuments;
//
// addUserToolStripMenuItem
//
this.addUserToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.addUserDebugToolStripMenuItem,
this.addUserReleaseToolStripMenuItem});
this.addUserToolStripMenuItem.Name = "addUserToolStripMenuItem";
this.addUserToolStripMenuItem.Size = new System.Drawing.Size(177, 22);
this.addUserToolStripMenuItem.Text = "Add User";
this.addUserToolStripMenuItem.Click += new System.EventHandler(this.addUserToolStripMenuItem_Click);
//
// addUserDebugToolStripMenuItem
//
this.addUserDebugToolStripMenuItem.Name = "addUserDebugToolStripMenuItem";
this.addUserDebugToolStripMenuItem.Size = new System.Drawing.Size(164, 22);
this.addUserDebugToolStripMenuItem.Text = "Add User Debug";
this.addUserDebugToolStripMenuItem.Click += new System.EventHandler(this.addUserDebugToolStripMenuItem_Click);
//
// addUserReleaseToolStripMenuItem
//
this.addUserReleaseToolStripMenuItem.Name = "addUserReleaseToolStripMenuItem";
this.addUserReleaseToolStripMenuItem.Size = new System.Drawing.Size(164, 22);
this.addUserReleaseToolStripMenuItem.Text = "Add User Release";
this.addUserReleaseToolStripMenuItem.Click += new System.EventHandler(this.addUserReleaseToolStripMenuItem_Click);
//
// MainWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -1712,7 +1749,11 @@
private System.Windows.Forms.ToolStripMenuItem itemConnect;
private System.Windows.Forms.ToolStripMenuItem itemStartServer;
private System.Windows.Forms.ToolStripDropDownButton toolChat;
private System.Windows.Forms.FolderBrowserDialog folderPrjDir;
private System.Windows.Forms.FolderBrowserDialog folderPrjDir;
private System.Windows.Forms.ToolStripMenuItem sendMessageToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem addUserToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem addUserDebugToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem addUserReleaseToolStripMenuItem;
}
}

View File

@ -34,6 +34,8 @@ namespace GameCreatorGroupProject
MainClient online;
TCPClient chat;
private uint chatServerID;
// Declare a ResourceImporter to make it easier to load and save resources
ResourceImporter resImporter = new ResourceImporter();
@ -186,7 +188,7 @@ namespace GameCreatorGroupProject
openResourceDialog.ShowDialog();
// Restore filter to unfiltered state
openResourceDialog.Filter = "All Files (*.*)|*.*";
// Grab user-selected path
string projPath = openResourceDialog.FileName;
@ -205,8 +207,11 @@ namespace GameCreatorGroupProject
private void itemConnect_Click(object sender, EventArgs e)
{
uint chatServerID = online.requestChatServer();
chat = MainClient.clients.ElementAt(0);
chatServerID = online.requestChatServer();
MessageBox.Show("Connected to chat server: " + chatServerID.ToString());
//online.connectClient(1, chatServerID, 1);
//chat = MainClient.clients.ElementAt(0);
//chat.connectClient(ServerInfo.getServerIP());
}
private void MainWindow_Load(object sender, EventArgs e)
@ -235,7 +240,24 @@ namespace GameCreatorGroupProject
{
string msg = "Hello World!";
object temp = msg;
if (chat == null)
chat = online.getAvailable();
chat.send(ref temp);
}
private void addUserToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void addUserDebugToolStripMenuItem_Click(object sender, EventArgs e)
{
online.connectClient(1, chatServerID, 0);
}
private void addUserReleaseToolStripMenuItem_Click(object sender, EventArgs e)
{
online.connectClient(1, chatServerID, 1);
}
}
}

View File

@ -1,57 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Threading;
namespace Room
{
class GEngine
{
/*______________members_________________________________*/
private Graphics drawHandle;
private Thread renderThread;
/*______________functions_______________________________*/
public GEngine(Graphics g)
{
drawHandle = g;
}
public void init()
{
renderThread = new Thread(new ThreadStart(render));
renderThread.Start();
}
public void stop()
{
renderThread.Abort();
}
private void render()
{
int framesRendered = 0;
long startTime = Environment.TickCount;
while (true)
{
drawHandle.FillRectangle(new SolidBrush(Color.Yellow), 0, 0, 1200, 700);
//benchmarking
framesRendered++;
if((Environment.TickCount) >= startTime + 1000)
{
Console.WriteLine("GEngine: " + framesRendered + " fps");
framesRendered = 0;
startTime = Environment.TickCount;
}
}
}
}
}

View File

@ -51,13 +51,18 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChatClient.cs" />
<Compile Include="ChatServer.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form2.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form2.Designer.cs">
<DependentUpon>Form2.cs</DependentUpon>
</Compile>
<Compile Include="MainClient.cs" />
<Compile Include="notConnectedException.cs" />
<Compile Include="Object.cs" />
@ -70,10 +75,12 @@
<Compile Include="Segment.cs" />
<Compile Include="ServerInfo.cs" />
<Compile Include="TCPClient.cs" />
<Compile Include="TCPServer.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Form2.resx">
<DependentUpon>Form2.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>

View File

@ -11,9 +11,19 @@ namespace GameCreatorGroupProject
internal class MainClient
{
//TEMPORARY, each program should have unique clientID
#if DEBUG
private static readonly uint thisClientID = 0;
#else
private static readonly uint thisClientID = 1;
#endif
//have some way for user to provide username
private static string username = "";
#if DEBUG
private static string username = "Debug";
#else
private static string username = "Release";
#endif
public static List<TCPClient> clients = new List<TCPClient>();
@ -68,7 +78,7 @@ namespace GameCreatorGroupProject
}
//connects to specified chat server
public TCPClient connectClient(string serverIP)
public void connectClient(string serverIP)
{
TCPClient c = null;
dc = false;
@ -113,7 +123,6 @@ namespace GameCreatorGroupProject
{
//reads stream data
connectInfo = new Tuple<byte, uint>(reader.ReadByte(), reader.ReadUInt32());
c = null;
//creates requested client
//might need to add way to control clients (eg disconnect, etc), probably add to a list or somthing
switch (connectInfo.Item1)
@ -144,7 +153,6 @@ namespace GameCreatorGroupProject
disconnect();
MessageBox.Show("A network error has occured.", "Unable to connect to server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return c;
}

View File

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GameCreatorGroupProject
{
abstract class TCPServer
{
protected TcpListener listener = null;
//starts the server for use with the specified project
abstract public void startServer(Project project);
//stops the server
abstract public void stopServer();
}
}

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Room
{
class Game
{
private GEngine gEngine;
public void startGraphics(Graphics g)
{
gEngine = new GEngine(g);
gEngine.init();
}
public void stopGame()
{
gEngine.stop();
}
}
}

View File

@ -19,3 +19,6 @@ C:\Users\Jared\Documents\GitHub\CS-350-410-431-Group-Project\OpenTK-Test\JustThe
C:\Users\Jared\Documents\GitHub\CS-350-410-431-Group-Project\OpenTK-Test\JustTheBasics\obj\Debug\JustTheBasics.csprojResolveAssemblyReference.cache
C:\Users\Jared\Documents\GitHub\CS-350-410-431-Group-Project\OpenTK-Test\JustTheBasics\obj\Debug\JustTheBasics.exe
C:\Users\Jared\Documents\GitHub\CS-350-410-431-Group-Project\OpenTK-Test\JustTheBasics\obj\Debug\JustTheBasics.pdb
C:\Users\Jared\Documents\GitHub\mod13\OpenTK-Test\JustTheBasics\bin\Debug\JustTheBasics.exe.config
C:\Users\Jared\Documents\GitHub\mod13\OpenTK-Test\JustTheBasics\obj\Debug\JustTheBasics.exe
C:\Users\Jared\Documents\GitHub\mod13\OpenTK-Test\JustTheBasics\obj\Debug\JustTheBasics.pdb

View File

@ -14,6 +14,9 @@ namespace Server_application
{
class ChatServer : TCPServer
{
protected static TcpListener listener = null;
protected static bool listenerStarted = false;
private static uint currentID = 0;
public static readonly object IDLock = new object();
private Dictionary<TcpClient, string> clientList = new Dictionary<TcpClient, string>();
@ -54,16 +57,21 @@ namespace Server_application
public override void startServer()
{
//attempts to start listener on chat port
try
if (!listenerStarted)
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
}
//displays error box if unable to start server
catch (SocketException e)
{
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
try
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
listenerStarted = true;
}
//displays error box if unable to start server
catch (SocketException e)
{
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//server control loop
while (running)
{
@ -106,10 +114,10 @@ namespace Server_application
//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 = "";
string clientName;
//creates StreamWriter for current outgoing client
@ -117,7 +125,6 @@ namespace Server_application
writer = new StreamWriter(outStream);
//appends sender name to beginning of message
clientList.TryGetValue(thisClient, out clientName);
message = reader.ReadLine();
//writes sender and message to outgoing stream
writer.WriteLine(clientName + ": " + message.ToString());
writer.Flush();

View File

@ -1,274 +1,280 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Server_application
{
class MainServer : TCPServer
{
//dictionary of connected clients by clientID
private Dictionary<uint, TcpClient> clientList = new Dictionary<uint, TcpClient>();
private bool running = true;
private static readonly object serverCounterLock = new Object();
private uint serverCounter = 0;
//main server port
private readonly int port = 20112;
public override void startServer()
{
//attempts to start listener on chat port
try
{
//possible issues with conflicts if multiple chat servers running, to be fixed later
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
}
//displays error box if unable to start server
catch (SocketException e)
{
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//main server control
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 = listener.AcceptTcpClient();
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);
}
}
}
}
//communicates between clients
private void transmitter(Object client)
{
using (TcpClient thisClient = (TcpClient)client)
{
NetworkStream stream = thisClient.GetStream();
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(stream);
uint thisClientID = reader.ReadUInt32();
//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)
{
//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);
//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);
//tells chat server that a connection is expected
ChatServer.setConnectExpected();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
while (ChatServer.getConnectExpected() && connect.Connected) { }
}
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.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (ResourceServer.getConnectExpected() && connect.Connected) { }
}
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.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (RTCServer.getConnectExpected() && connect.Connected) { }
}
break;
}
//checks if requested client still connected
if (connect.Connected)
{
//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
writer.Write(true);
}
else
{
//tells requester specified client is not connected
writer.Write(false);
}
break;
}
}
}
//cleanup
if (writer != null) { writer.Close(); }
if (reader != null) { reader.Close(); }
//removes client from list of connected clients
lock (clientList)
{
TcpClient close;
if (clientList.TryGetValue(thisClientID, out close))
{
close.GetStream().Close();
close.Close();
clientList.Remove(thisClientID);
}
}
}
}
//starts a real time collaboration server with specidied serverID
private void startRTCServer(object serverID)
{
TCPServer server = new RTCServer((uint)serverID);
server.startServer();
}
//starts a resource server with specidied serverID
private void startResourceServer(object serverID)
{
TCPServer server = new ResourceServer((uint)serverID);
server.startServer();
}
//starts a chat server with specidied serverID
private void startChatServer(object serverID)
{
TCPServer server = new ChatServer((uint)serverID);
server.startServer();
}
//stops the main server
public override void stopServer()
{
if (listener != null) { listener.Stop(); }
running = false;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Server_application
{
class MainServer : TCPServer
{
protected static TcpListener listener = null;
protected static bool listenerStarted = false;
//dictionary of connected clients by clientID
private Dictionary<uint, TcpClient> clientList = new Dictionary<uint, TcpClient>();
private bool running = true;
private static readonly object serverCounterLock = new Object();
private uint serverCounter = 0;
//main server port
private readonly int port = 20112;
public override void startServer()
{
//attempts to start listener on chat port
if (!listenerStarted)
{
try
{
//possible issues with conflicts if multiple chat servers running, to be fixed later
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
}
//displays error box if unable to start server
catch (SocketException e)
{
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//main server control
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 = listener.AcceptTcpClient();
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);
}
}
}
}
//communicates between clients
private void transmitter(Object client)
{
using (TcpClient thisClient = (TcpClient)client)
{
NetworkStream stream = thisClient.GetStream();
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(stream);
uint thisClientID = reader.ReadUInt32();
//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)
{
//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);
//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);
//tells chat server that a connection is expected
ChatServer.setConnectExpected();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
while (ChatServer.getConnectExpected() && connect.Connected) { }
}
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.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (ResourceServer.getConnectExpected() && connect.Connected) { }
}
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.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (RTCServer.getConnectExpected() && connect.Connected) { }
}
break;
}
//checks if requested client still connected
if (connect.Connected)
{
//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
writer.Write(true);
}
else
{
//tells requester specified client is not connected
writer.Write(false);
}
break;
}
}
}
//cleanup
if (writer != null) { writer.Close(); }
if (reader != null) { reader.Close(); }
//removes client from list of connected clients
lock (clientList)
{
TcpClient close;
if (clientList.TryGetValue(thisClientID, out close))
{
close.GetStream().Close();
close.Close();
clientList.Remove(thisClientID);
}
}
}
}
//starts a real time collaboration server with specidied serverID
private void startRTCServer(object serverID)
{
TCPServer server = new RTCServer((uint)serverID);
server.startServer();
}
//starts a resource server with specidied serverID
private void startResourceServer(object serverID)
{
TCPServer server = new ResourceServer((uint)serverID);
server.startServer();
}
//starts a chat server with specidied serverID
private void startChatServer(object serverID)
{
TCPServer server = new ChatServer((uint)serverID);
server.startServer();
}
//stops the main server
public override void stopServer()
{
if (listener != null) { listener.Stop(); }
running = false;
}
}
}

View File

@ -13,8 +13,6 @@ namespace Server_application
{
abstract class TCPServer
{
protected TcpListener listener = null;
//starts the server for use with the specified project
abstract public void startServer();

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>