-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.cs
153 lines (123 loc) · 3.91 KB
/
Session.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
using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using AutomationServerClient.Stream;
using AutomationServerClient.Messages;
namespace AutomationServerClient
{
public abstract class Session : OnMessageReadyListener
{
public delegate void SessionConnectedDelegate();
public event SessionConnectedDelegate OnConnected;
public event SessionConnectedDelegate OnConnectionFailed;
public event SessionConnectedDelegate OnAuthenticationFailed;
public event SessionConnectedDelegate OnDisconnected;
private static string server = "SOME IP ADDRESS";
private static int port = 9909;
private Thread sessionThread;
protected Socket socket;
protected string API_KEY;
protected string APP_KEY;
protected string deviceId;
private volatile bool running;
private StreamManager manager;
private OnDataReceivedListener dataReceivedListener;
public abstract void OnMessageReady(byte[] message);
public Session(string API_KEY, string APP_KEY, string deviceId)
{
this.API_KEY = API_KEY;
this.APP_KEY = APP_KEY;
this.deviceId = deviceId;
sessionThread = new Thread(run);
manager = new BytesStreamManager();
manager.setMessageReadyListener(this);
dataReceivedListener = manager;
running = false;
}
public void Connect()
{
sessionThread.Start();
}
public abstract void Authenticate();
private void run()
{
int length = 0;
byte[] authB = new byte[1];
byte[] buffer = new byte[256];
try
{
IPAddress address = Dns.GetHostAddresses(server)[0];
IPEndPoint ipe = new IPEndPoint(address, port);
socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
Console.WriteLine("Connected");
running = true;
Authenticate();
socket.Receive(authB);
if (authB[0] != 1){
OnAuthenticationFailed?.Invoke();
Console.WriteLine("Failed");
return;
}
OnConnected?.Invoke();
}
catch (Exception ex)
{
running = false;
Console.WriteLine(ex.Message);
OnConnectionFailed?.Invoke();
return;
}
try
{
while (running)
{
length = socket.Receive(buffer);
if (length < 0)
break;
dataReceivedListener?.OnDataReceived(buffer, length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
close();
}
public void close()
{
try
{
socket.Close();
}
catch (Exception) { }
finally
{
running = false;
OnDisconnected?.Invoke();
}
}
protected virtual void sendMessage(byte[] msg)
{
try
{
socket.Send(manager.formatStream(msg));
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
running = false;
}
}
public void sendMessage(Message message)
{
Console.WriteLine("sending...");
sendMessage(message.toByteArray());
}
public bool IsRunning()
{
return running;
}
}
}