-
Notifications
You must be signed in to change notification settings - Fork 0
/
T41Server.cs
52 lines (43 loc) · 1.56 KB
/
T41Server.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
using System.Net.Sockets;
using SocketHelper;
using T41.Serial;
namespace T41.Server;
// adds T41Serial connection to server
class T41Server : SocketServer {
// serial class
protected T41Serial t41Serial;
int connectRetryCount = 0;
public T41Server(SocketSettings theSettings, T41Serial t41serial) : base(theSettings) {
t41Serial = t41serial;
}
protected override void ProcessAccept(SocketAsyncEventArgs e) {
// do we have a serial connection?
if(t41Serial.Connected) {
Interlocked.Increment(ref m_numConnectedSockets);
// perform additional work in response to connection
RespondToAccept(e);
// Get the socket for the accepted client connection and put it into the
// ReadEventArg object user token
SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
readEventArgs.UserToken = e.AcceptSocket;
// As soon as the client is connected, post a receive to the connection
bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
if (!willRaiseEvent) {
ProcessReceive(readEventArgs);
}
} else {
if(connectRetryCount < 10) {
if(connectRetryCount++ == 0) {
}
Thread.Sleep(10000);
ProcessAccept(e);
} else {
Console.WriteLine("\nClient attempted to connect before connected to T41.");
CloseClientSocket(e);
}
}
}
protected virtual void RespondToAccept(SocketAsyncEventArgs e) {
Console.WriteLine("Client connection accepted. There are {0} clients connected to the server", m_numConnectedSockets);
}
}