-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionHandler.cs
182 lines (162 loc) · 5.56 KB
/
connectionHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace csharp_server
{
class _connectionHandler
{
// if mClients is organized as a dictionary
// we can just lookup clients by id.
private Dictionary<uint, _client> mClients;
// list of used names
private List<string> mNames;
// ids that have been discarded
private Stack<uint> mUsedIDs;
// the next assignable id
private uint mStartIDs;
// stack containing all threads used by _connectionHandler
// mainly for termination of threads and clean up
private Stack<Thread> mThreads;
// These are necessary for accepting incoming connections
private TcpListener mListener;
private int mPort;
private bool mActive;
public _connectionHandler(int pPort)
{
mClients = new Dictionary<UInt32, _client>();
mNames = new List<string>();
mUsedIDs = new Stack<uint>();
mStartIDs = 0;
mThreads = new Stack<Thread>();
mPort = pPort;
mActive = true;
IPAddress hostIP = (Dns.GetHostEntry("127.0.0.1")).AddressList[0];
IPEndPoint ep = new IPEndPoint(hostIP, mPort);
mListener = new TcpListener(ep);
mListener.Start();
Thread mClientLoop = new Thread(joinLoop);
mClientLoop.Start();
mThreads.Push(mClientLoop);
mClientLoop = null;
}
public void shutdown()
{
_client cl;
int sizeOfClients = mClients.Count;
// tell the server to stop looping for new connections
mActive = false;
// send a dummy client to the server
dummyClientDisconnect();
mListener.Stop();
while (mThreads.Count > 0)
{
Thread s = mThreads.Pop();
s.Abort();
}
for (uint i = 0; i < sizeOfClients; ++i)
{
if (!mUsedIDs.Contains(i) && mClients.TryGetValue(i, out cl))
{
// delete the client associated with the ID
cl.sendDisconnect();
cl.kill();
mClients.Remove(i);
}
}
}
// joinLoop is responsible for adding clients to the server and managing their unique IDs
// joinLoop will start a new thread for every client that joins the server.
private void joinLoop()
{
UInt32 ID;
while (mActive)
{
Socket nClient = mListener.AcceptSocket();
// if the serve has shut down
// don't add the new client
// it's a fake client
if (!mActive) return;
if (mUsedIDs.Count == 0)
{
ID = mStartIDs;
++mStartIDs;
}
else
{
ID = mUsedIDs.Pop();
}
mClients.Add(ID, new _client(nClient, "Anon_" + ID, ID));
mNames.Add("Anon_" + ID);
_writer.writeLine("A client has joined with ID: {0}", ID);
Thread cl = new Thread(clientPacketLoop);
cl.Start(ID);
mThreads.Push(cl);
cl = null;
nClient = null;
}
}
private void dummyClientDisconnect()
{
byte[] packet = new byte[1];
Socket dummy = _network.joinListen("127.0.0.1", mPort);
packet[0] = (byte)packets.Disconnect;
_network.sendData(dummy, packet);
_network.killSocket(dummy);
dummy = null;
}
private void clientPacketLoop(object param)
{
uint pID = (uint)param;
_client cl;
while (mClients.TryGetValue(pID, out cl))
{
byte packet = cl.getNextPacket();
try
{
switch (packet)
{
case (byte)packets.Disconnect:
removeClient(pID);
break;
case (byte)packets.Message:
_writer.writeLine("{0}: {1}", cl.getName(), cl.getMessage());
break;
case (byte)packets.Name:
string name = cl.getNewName();
if (!mNames.Contains(name))
{
mNames.Add(name);
cl.setName(name);
}
break;
}
}
catch (Exception mErr)
{
if (mErr.Message == "Disconnected")
{
removeClient(pID);
}
}
cl = null;
}
}
private void removeClient(uint pID)
{
_client cl;
if (mClients.TryGetValue(pID, out cl))
{
_writer.writeLine("Client {0} has disconnected.", pID);
mClients.Remove(pID);
mUsedIDs.Push(pID);
cl.kill();
mNames.Remove(cl.getName());
}
cl = null;
}
}
}