2using System.Collections.Generic;
4using System.Net.Sockets;
35 private ControlCallbacks ws;
38 private IPAddress myIP;
40 private uint receiverID;
41 private List<SenderStreamParams> senderStreams;
42 private List<ReceiverStreamParams> receiverStreams;
43 private List<uint> allowedStreams;
49 senderStreams =
new List<SenderStreamParams>();
50 receiverStreams =
new List<ReceiverStreamParams>();
51 allowedStreams =
new List<uint>();
52 Control.debug =
debug;
68 Print(
"Initializing WebSocket Connection");
79 throw new Exception(
"Timeout error");
95 this.credentials = credentials;
96 JSONNode jsonRequest = credentials.
ToJSON();
97 jsonRequest[
"ID"] = id;
98 string request = jsonRequest.ToString();
99 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
100 this.credentials.Token = jsonResponse[
"token"].Value;
101 myIP = IPAddress.Parse(jsonResponse[
"IP"].Value).MapToIPv4();
102 int statusCode = jsonResponse[
"statusCode"].AsInt;
103 string message = jsonResponse[
"message"].Value;
107 return statusCode == 0 ? true :
false;
135 JSONNode jsonRequest = streamParams.
toJSON();
136 jsonRequest[
"function"] =
"sender";
137 jsonRequest[
"IP"] = myIP.ToString();
138 jsonRequest[
"port"] = 0;
139 jsonRequest[
"token"] = this.credentials.
Token;
140 jsonRequest[
"ID"] = id;
141 string request = jsonRequest.ToString();
142 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
143 int statusCode = jsonResponse[
"statusCode"].AsInt;
144 uint.TryParse(jsonResponse[
"streamID"].Value, out streamParams.
streamID);
145 int udpPort = jsonResponse[
"port"].AsInt;
146 string MTU = jsonResponse[
"MTU"].Value;
147 string message = jsonResponse[
"message"].Value;
151 if (streamParams.
proto.Equals(
"udp"))
155 Print(
"Attempting to open a UDP port at " + jsonResponse[
"port"].Value);
156 udp =
new CorelinkUDP(config.
dataIP, udpPort, ref allowedStreams, ref receiverStreams);
160 streamParams.port = udpPort;
161 senderStreams.Add(streamParams);
178 if (receiverID !=
null)
181 streamParams.receiverid = receiverID;
183 JSONNode jsonRequest = streamParams.
toJSON();
184 jsonRequest[
"IP"] = myIP.ToString();
185 jsonRequest[
"port"] = 0;
186 jsonRequest[
"token"] = this.credentials.
Token;
187 jsonRequest[
"ID"] = id;
188 string request = jsonRequest.ToString();
189 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
190 uint.TryParse(jsonResponse[
"streamID"].Value, out streamParams.
streamID);
195 foreach (JSONNode node
in jsonResponse[
"streamList"])
198 uint.TryParse(node[
"streamID"].Value, out streamID);
200 streamParams.streamIDs.Add(streamID);
201 if (!allowedStreams.Contains(streamID))
203 allowedStreams.Add(streamID);
207 streamParams.port = jsonResponse[
"port"].AsInt;
208 string proto = jsonResponse[
"proto"].Value;
209 string MTU = jsonResponse[
"MTU"].Value;
211 if (proto.Equals(
"udp"))
215 Print(
"Attempting to open a UDP port at " + jsonResponse[
"port"].Value);
216 udp =
new CorelinkUDP(config.
dataIP, streamParams.port, ref allowedStreams, ref receiverStreams);
221 receiverStreams.Add(streamParams);
222 return streamParams.streamIDs;
232 if (receiverID ==
null)
234 throw new CorelinkException(
"ERROR: Need to create a receiver stream before subscribing");
236 JSONNode jsonRequest = JSON.Parse(
"{}");
237 jsonRequest[
"function"] =
"subscribe";
238 jsonRequest[
"receiverID"] = receiverID;
239 for (
int i = 0; i < streamIDs.Count; i++)
241 jsonRequest[
"streamID"][i] = streamIDs[i];
243 jsonRequest[
"token"] = this.credentials.
Token;
244 jsonRequest[
"ID"] = id;
245 string request = jsonRequest.ToString();
246 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
248 List<uint> returnedStreamids =
new List<uint>();
249 for (
int i = 0; i < jsonResponse[
"streamList"].Count; i++)
252 uint.TryParse(jsonResponse[
"streamList"][i][
"streamID"].Value, out streamID);
254 returnedStreamids.Add(streamID);
255 allowedStreams.Add(streamID);
258 return returnedStreamids;
268 JSONNode jsonRequest = JSON.Parse(
"{}");
269 jsonRequest[
"function"] =
"unsubscribe";
270 jsonRequest[
"receiverID"] = receiverStreams[0].streamID;
271 jsonRequest[
"streamID"] =
new JSONArray();
272 foreach (
string w
in streamIDs)
274 jsonRequest[
"workspaces"].Add(w);
276 jsonRequest[
"token"] = credentials.
Token;
277 jsonRequest[
"ID"] = id;
278 string request = jsonRequest.ToString();
279 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
280 List<uint> retval =
new List<uint>();
281 foreach (JSONNode f
in jsonResponse[
"streamList"].AsArray)
284 uint.TryParse(f.Value, out ID);
299 if (receiverStreams.Count > 0)
301 filter.
streamIDs.Add(receiverStreams[0].streamID);
305 filter.streamIDs.Add(param.
streamID);
311 JSONNode jsonRequest = JSON.Parse(
"{}");
312 jsonRequest[
"function"] =
"disconnect";
313 jsonRequest[
"workspaces"] =
new JSONArray();
314 foreach (
string w
in filter.workspaces)
316 jsonRequest[
"workspaces"].Add(w);
318 jsonRequest[
"types"] =
new JSONArray();
319 foreach (
string t
in filter.types)
321 jsonRequest[
"types"].Add(t);
323 jsonRequest[
"streamIDs"] =
new JSONArray();
324 foreach (uint s
in filter.streamIDs)
326 jsonRequest[
"streamIDs"].Add(s);
328 if (receiverID !=
null)
330 jsonRequest[
"streamIDs"].Add(receiverID);
332 jsonRequest[
"token"] = this.credentials.
Token;
333 jsonRequest[
"ID"] = id;
334 string request = jsonRequest.ToString();
335 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
336 List<string> retval =
new List<string>();
337 foreach (JSONNode f
in jsonResponse[
"streamList"].AsArray)
361 public void send(uint streamID,
byte[] header,
byte[] data)
365 throw new Exception(
"udp connection not created");
367 udp.
send(streamID, header, data);
372 #region [Workspace Functions]
380 JSONNode jsonRequest = JSON.Parse(
"{}");
381 jsonRequest[
"function"] =
"addWorkspace";
382 jsonRequest[
"workspace"] = workspace;
383 jsonRequest[
"token"] = credentials.
Token;
384 jsonRequest[
"ID"] = id;
385 string request = jsonRequest.ToString();
386 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
387 int statusCode = jsonResponse[
"statusCode"].AsInt;
388 return statusCode == 0 ? true :
false;
398 JSONNode jsonRequest = JSON.Parse(
"{}");
399 jsonRequest[
"function"] =
"rmWorkspace";
400 jsonRequest[
"workspace"] = workspace;
401 jsonRequest[
"token"] = credentials.
Token;
402 jsonRequest[
"ID"] = id;
403 string request = jsonRequest.ToString();
404 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
405 int statusCode = jsonResponse[
"statusCode"].AsInt;
406 return statusCode == 0 ? true :
false;
416 JSONNode jsonRequest = JSON.Parse(
"{}");
417 jsonRequest[
"function"] =
"setDefaultWorkspace";
418 jsonRequest[
"workspace"] = workspace;
419 jsonRequest[
"token"] = credentials.
Token;
420 jsonRequest[
"ID"] = id;
421 string request = jsonRequest.ToString();
422 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
423 int statusCode = jsonResponse[
"statusCode"].AsInt;
424 return statusCode == 0 ? true :
false;
433 JSONNode jsonRequest = JSON.Parse(
"{}");
434 jsonRequest[
"function"] =
"getDefaultWorkspace";
435 jsonRequest[
"token"] = credentials.
Token;
436 jsonRequest[
"ID"] = id;
437 string request = jsonRequest.ToString();
438 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
439 return jsonResponse[
"workspace"].Value;
448 JSONNode jsonRequest = JSON.Parse(
"{}");
449 jsonRequest[
"function"] =
"listWorkspaces";
450 jsonRequest[
"token"] = this.credentials.
Token;
451 string request = jsonRequest.ToString();
452 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
453 List<string> retval =
new List<string>();
454 foreach (JSONNode f
in jsonResponse[
"workspaceList"].AsArray)
463 #region [Function Helpers]
471 JSONNode jsonRequest = JSON.Parse(
"{}");
472 jsonRequest[
"function"] =
"listFunctions";
473 jsonRequest[
"token"] = credentials.
Token;
474 jsonRequest[
"ID"] = id;
475 string request = jsonRequest.ToString();
476 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
477 List<string> retval =
new List<string>();
478 foreach (JSONNode f
in jsonResponse[
"functionList"].AsArray)
492 JSONNode jsonRequest = JSON.Parse(
"{}");
493 jsonRequest[
"function"] =
"listServerFunctions";
494 jsonRequest[
"token"] = credentials.
Token;
495 jsonRequest[
"ID"] = id;
496 string request = jsonRequest.ToString();
497 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
498 List<string> retval =
new List<string>();
499 foreach (JSONNode f
in jsonResponse[
"functionList"].AsArray)
513 JSONNode jsonRequest = JSON.Parse(
"{}");
514 jsonRequest[
"function"] =
"describeFunction";
515 jsonRequest[
"functionName"] =
function;
516 jsonRequest[
"token"] = credentials.
Token;
517 jsonRequest[
"ID"] = id;
518 string request = jsonRequest.ToString();
519 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
520 return jsonResponse[
"description"].Value;
531 JSONNode jsonRequest = JSON.Parse(
"{}");
532 jsonRequest[
"function"] =
"describeServerFunction";
533 jsonRequest[
"functionName"] =
function;
534 jsonRequest[
"token"] = credentials.
Token;
535 jsonRequest[
"ID"] = id;
536 string request = jsonRequest.ToString();
537 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
538 return jsonResponse[
"description"].Value;
543 #region [Stream Info]
552 JSONNode jsonRequest = JSON.Parse(
"{}");
553 jsonRequest[
"function"] =
"listStreams";
554 jsonRequest[
"workspaces"] =
new JSONArray();
555 foreach (
string w
in filter.workspaces)
557 jsonRequest[
"workspaces"].Add(w);
559 jsonRequest[
"types"] =
new JSONArray();
560 foreach (
string t
in filter.types)
562 jsonRequest[
"types"].Add(t);
564 jsonRequest[
"token"] = credentials.
Token;
565 jsonRequest[
"ID"] = id;
566 string request = jsonRequest.ToString();
567 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
568 return jsonResponse[
"senderList"].AsArray;
577 JSONNode jsonRequest = JSON.Parse(
"{}");
578 jsonRequest[
"function"] =
"streamInfo";
579 jsonRequest[
"workspaces"] =
new JSONArray();
580 jsonRequest[
"streamID"] = streamID;
581 jsonRequest[
"token"] = credentials.
Token;
582 jsonRequest[
"ID"] = id;
583 string request = jsonRequest.ToString();
584 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
591 #region [User Functions]
594 JSONNode jsonRequest = JSON.Parse(
"{}");
595 jsonRequest[
"function"] =
"streamInfo";
596 jsonRequest[
"token"] = credentials.
Token;
597 jsonRequest[
"ID"] = id;
598 string request = jsonRequest.ToString();
599 JSONNode jsonResponse = SendAndErrorCheck(request,
id++);
600 List<string> retval =
new List<string>();
601 foreach (JSONNode f
in jsonResponse[
"userList"].AsArray)
616 JSONNode SendAndErrorCheck(
string request,
int id)
618 Print(
"Request:\t" + request);
619 ws.sendMessage(request);
620 string response = ws.getResponse();
622 Print(
"Response: " + response);
623 JSONNode jsonResponse = JSON.Parse(response);
624 int statusCode = jsonResponse[
"statusCode"].AsInt;
625 int receivedID = jsonResponse[
"ID"].AsInt;
626 string message = jsonResponse[
"message"].Value;
627 if (receivedID !=
id)
631 if (statusCode != 0)
throw new CorelinkException(statusCode, message);
634 public static void Print(
string message)
636 if (
debug) Debug.Log(message);
The Config class is filled once at the beginning of the unity session and contains important server i...
Singleton class to communicate with Corelink.
string describeServerFunction(string function)
Asks the server for a specific server callback function's description.
List< uint > createReceiver(ReceiverStreamParams streamParams)
Asks Corelink to create a receiver stream. At minimum, this requires a workspace name and a list of t...
List< string > listWorkspaces()
Asks corelink for all available workspaces.
List< uint > subscribe(List< uint > streamIDs)
Tells Corelink to subscribe a certain receiver stream to one or more sender streams.
bool rmWorkspace(string workspace)
Asks the server to remove a workspace.
List< string > disconnect(StreamFilter filter)
uint createSender(SenderStreamParams streamParams)
Asks Corelink to create a sender stream. At minimum, this requires a workspace name and a type....
static bool debug
debug turns on or off verbose logging. Control.Print() statements will only print if debug mode is on
void send(uint streamID, byte[] header, byte[] data)
Sends a UDP message to Corelink for a specific sender stream id.
List< string > disconnect()
Disconnects streams of a specific stream filter type, or all current streams if no parameters are giv...
bool login(Credentials credentials)
Logs into the server after connecting after verifying login credentials like username,...
JSONNode streamInfo(uint streamID)
Asks the server for information about a sender stream.
void connect(Config config)
Connects with client at the specified Port and IP number. Throws an error if something goes wrong.
List< string > listFunctions()
Asks the server for all available client functions.
void exit()
Disconnects all available streamIDs and stops the WebSocket/UDP Sockets.
string getDefaultWorkspace()
Asks Corelink for the current default workspace.
static int timeoutIterations
In some callbacks, the code is waiting on a response from the server. This specifies the number of 10...
List< uint > unsubscribe(List< string > streamIDs)
Unsubscribes the program from various sender streams.
List< string > listServerFunctions()
Asks the server for all server callback functions. Currently there is Update, Subscriber,...
List< string > listUsers()
async void ConnectToServer()
bool addWorkspace(string workspace)
Asks the server to create a new workspace.
JSONArray listStreams(StreamFilter filter)
Asks the server for a list all streams.
string describeFunction(string function)
Asks the server for a specific client side function's description.
bool setDefaultWorkspace(string workspace)
Requests Corelink to set the default workspace.
static void Print(string message)
This class was used as the control connection which will be deprecated soon in favor of WebSockets.
Client to send and receive data to/from CoreLink via UDP.
void send(uint streamID, byte[] header, byte[] data)
SENDER FUNCTIONS.
void stopConnection()
CLEANUP FUNCTIONS.
void receiverPing(uint streamID)
Send an empty packet to the CoreLink server to assign this receiver stream id to a port behind a fire...
Contains user information to authenticate with Corelink. The username and password are sent to the se...
override JSONNode toJSON()
Parse to server function. IP, port, and token gets filled in by CoreLink.Control.
Contains all the necessary parameters for sender streams except IP and port. Inherits from StreamPara...
override JSONNode toJSON()
Parse to server function. IP, port, and token gets filled in by CoreLink.Control.
When creating a receiver stream, the request must query what workspace and type of stream to receive ...