-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cs
148 lines (116 loc) · 5.05 KB
/
Server.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.DeviceSupport;
using Crestron.SimplSharpPro.CrestronThread;
using Crestron.SimplSharp.CrestronSockets;
using System.Text.RegularExpressions;
namespace Crestron_TCP_Buttons {
public delegate void ClientConnected(Client client);
public delegate void ClientDataReceived(Client client, String data);
public class Server {
public TCPServer tcp;
private Thread thread;
private Dictionary<uint, Client> clients;
public CrestronCollection<Button> buttons;
public event ClientConnected ClientConnectedEvent;
public event ClientDataReceived ClientDataReceiveEvent;
public Server() {
clients = new Dictionary<uint, Client>();
}
public void Start() {
tcp = new TCPServer("0.0.0.0", 9023, 0xFFFF, EthernetAdapterType.EthernetUnknownAdapter, 32);
tcp.SocketStatusChange += new TCPServerSocketStatusChangeEventHandler(SocketStatusChange);
thread = new Thread(new ThreadCallbackFunction((object o) => { ServerThread(); return null; }), null);
}
public void Stop() {
if(thread != null) thread.Abort();
if(tcp != null) tcp.DisconnectAll();
tcp = null;
}
public void Broadcast(string msg) {
lock(clients) {
var buf = Encoding.ASCII.GetBytes(msg);
foreach(var client in clients.Values) {
client.Send(msg);
}
}
}
private void ServerThread() {
uint clientId;
while(true) {
var listenStatus = tcp.WaitForConnection(out clientId);
if(listenStatus == SocketErrorCodes.SOCKET_OK) {
lock(clients) {
var newClient = new Client(this, tcp, clientId);
newClient.DataReceived += new EventHandler<DataEventArgs>(DataReceived);
clients.Add(clientId, newClient);
ErrorLog.Notice("# ButtonServer: New connection from {0}", tcp.GetAddressServerAcceptedConnectionFromForSpecificClient(clientId));
ClientConnectedEvent.Invoke(newClient);
}
} else {
ErrorLog.Error("Failed to listen for new connections: {0}", listenStatus.ToString());
Thread.Sleep(1000);
}
}
}
/*void buttonState(Client client) {
foreach(Button button in buttons) {
client.Send("BUTTON<" + button.Name + ">=(" + button.State + ")\n");
}
}*/
void DataReceived(object sender, DataEventArgs e) {
ClientDataReceiveEvent.Invoke(e.client, e.buf);
}
// Clean up disconnected clients
private void SocketStatusChange(TCPServer myTCPServer, uint clientIndex, SocketStatus serverSocketStatus) {
if(serverSocketStatus != SocketStatus.SOCKET_STATUS_CONNECTED) {
lock(clients) {
if(clients.ContainsKey(clientIndex))
clients.Remove(clientIndex);
}
}
}
}
public class DataEventArgs : EventArgs {
public string buf;
public Client client;
}
public class Client {
private uint id;
private string rx;
private Server server;
public event EventHandler<DataEventArgs> DataReceived;
public Client(Server srv, TCPServer tcp, uint clientId) {
server = srv;
id = clientId;
server.tcp.ReceiveDataAsync(new TCPServerReceiveCallback(ReceiveCallback));
}
public void Send(string msg) {
var buf = Encoding.ASCII.GetBytes(msg);
if(server.tcp.ClientConnected(id))
server.tcp.SendData(id, buf, buf.Length);
}
private void ReceiveCallback(TCPServer tcp, uint clientId, int len) {
// Only proceed if we have data & client is connected
if(len <= 0 || !tcp.ClientConnected(clientId)) return;
// Append incoming data to string "buffer"
rx += Encoding.ASCII.GetString(tcp.IncomingDataBuffer, 0, len);
// Await more data if still connected
if(tcp.ClientConnected(id))
tcp.ReceiveDataAsync(clientId, new TCPServerReceiveCallback(ReceiveCallback));
// Separate messages
var msgs = rx.Split('\n');
for(var i = 0; i < msgs.Length - 1; i++)
try {
if(DataReceived != null) {
DataReceived.Invoke(this, new DataEventArgs { buf = msgs[i], client = this });
}
} catch { }
rx = msgs.Last();
}
}
}