FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Blynk.NET/Blynk.NET/BlynkConnection.cs at main · SupraBitKid/Blynk.NET · GitHub
SupraBitKid
/
Blynk.NET
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Blynk.NET
/
Blynk.NET
/
BlynkConnection.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
295 lines (256 loc) · 9.06 KB
Breadcrumbs
Blynk.NET
/
Blynk.NET
/
BlynkConnection.cs
Copy path
File metadata and controls
295 lines (256 loc) · 9.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Net
.
Security
;
using
System
.
Net
.
Sockets
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
using
Blynk
.
NET
.
Logging
;
using
Blynk
.
NET
.
Pins
;
namespace
Blynk
.
NET
{
public
class
BlynkConnection
:
IDisposable
{
public
BlynkConnection
(
string
host
,
int
port
,
string
authentication
)
{
//, bool withSSL ) {
try
{
BlynkLogManager
.
LogMethodBegin
(
"BlynkConnection.ctor"
)
;
this
.
host
=
host
;
this
.
port
=
port
;
this
.
authentication
=
authentication
;
this
.
tcpClient
=
new
TcpClient
(
AddressFamily
.
InterNetwork
)
;
this
.
tcpClient
.
NoDelay
=
true
;
this
.
backgroundCancellationTokenSource
=
new
CancellationTokenSource
(
)
;
//this.withSSL = withSSL;
this
.
PingIntervalInMilliseconds
=
5000
;
this
.
BackgroundReadIntervalInMilliseconds
=
100
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
"BlynkConnection.ctor"
)
;
}
}
private
string
host
;
private
int
port
;
private
string
authentication
;
private
TcpClient
tcpClient
;
private
NetworkStream
tcpStream
;
//private bool withSSL;
//private SslStream sslStream;
private
object
messageidLock
=
new
object
(
)
;
private
UInt16
messageID
;
private
CancellationTokenSource
backgroundCancellationTokenSource
;
public
PinNotification
<
DigitalPin
>
DigitalPinNotification
{
get
;
private
set
;
}
=
new
PinNotification
<
DigitalPin
>
(
)
;
public
PinNotification
<
AnalogPin
>
AnalogPinNotification
{
get
;
private
set
;
}
=
new
PinNotification
<
AnalogPin
>
(
)
;
public
PinNotification
<
VirtualPin
>
VirtualPinNotification
{
get
;
private
set
;
}
=
new
PinNotification
<
VirtualPin
>
(
)
;
public
Action
<
string
,
PinMode
>
PinModeNotification
{
get
;
set
;
}
public
Action
<
int
>
ResponseReceivedNotification
{
get
;
set
;
}
internal
UInt16
NextMessageId
{
get
{
lock
(
this
.
messageidLock
)
{
return
this
.
messageID
++
;
}
}
}
public
bool
Connected
{
get
{
return
this
.
tcpClient
?
.
Connected
??
false
;
}
}
public
CancellationToken
CancellationToken
{
get
{
return
this
.
backgroundCancellationTokenSource
.
Token
;
}
}
public
int
PingIntervalInMilliseconds
{
get
;
set
;
}
public
int
BackgroundReadIntervalInMilliseconds
{
get
;
set
;
}
public
bool
Connect
(
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
Connect
)
)
;
CancellationTokenSource
cancellationTokenSource
=
new
CancellationTokenSource
(
50000
)
;
// this.withSSL ? 10000 : 5000 ); // five seconds for connect & login
return
this
.
ConnectAsync
(
cancellationTokenSource
.
Token
)
.
GetAwaiter
(
)
.
GetResult
(
)
;
}
catch
(
Exception
ex
)
{
BlynkLogManager
.
LogException
(
"Exception while connecting"
,
ex
)
;
return
false
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
Connect
)
)
;
}
}
public
async
Task
<
bool
>
ConnectAsync
(
CancellationToken
cancellationToken
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
ConnectAsync
)
)
;
this
.
messageID
=
1
;
await
this
.
tcpClient
.
ConnectAsync
(
this
.
host
,
this
.
port
)
;
if
(
this
.
tcpClient
.
Connected
)
{
this
.
tcpClient
.
NoDelay
=
true
;
this
.
tcpStream
=
this
.
tcpClient
.
GetStream
(
)
;
//if( this.withSSL ) {
// this.sslStream = new SslStream( this.tcpStream );
// await this.sslStream.AuthenticateAsClientAsync( this.host );
//}
using
(
var
loginCommand
=
new
BlynkCommand
(
BlynkCommandType
.
BLYNK_CMD_LOGIN
,
this
.
NextMessageId
)
)
{
loginCommand
.
Append
(
this
.
authentication
,
this
.
authentication
.
Length
)
;
var
dummy2
=
Task
.
Run
(
(
)
=>
this
.
GetMessageOnSchedule
(
this
.
backgroundCancellationTokenSource
.
Token
)
)
;
await
this
.
SendAsync
(
loginCommand
.
ToArray
(
)
,
cancellationToken
)
;
}
var
dummy
=
Task
.
Run
(
(
)
=>
this
.
SendPingsOnSchedule
(
this
.
backgroundCancellationTokenSource
.
Token
)
)
;
return
true
;
}
return
false
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
ConnectAsync
)
)
;
}
}
public
void
Disconnect
(
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
Disconnect
)
)
;
this
.
backgroundCancellationTokenSource
.
Cancel
(
)
;
this
.
tcpStream
.
Close
(
)
;
this
.
tcpClient
.
Close
(
)
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
Disconnect
)
)
;
}
}
internal
async
Task
<
bool
>
SendResponseAsync
(
UInt16
originalMessageId
,
BlynkResponse
response
=
BlynkResponse
.
OK
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
SendResponseAsync
)
)
;
var
command
=
new
BlynkCommand
(
BlynkCommandType
.
BLYNK_CMD_RESPONSE
,
originalMessageId
,
false
)
;
command
.
Append
(
(
byte
)
0
)
.
Append
(
(
byte
)
response
)
;
return
await
this
.
SendAsync
(
command
.
ToArray
(
)
,
backgroundCancellationTokenSource
.
Token
)
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
SendResponseAsync
)
)
;
}
}
public
async
Task
<
bool
>
SendAsync
(
byte
[
]
byteBuffer
,
CancellationToken
cancellationToken
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
SendAsync
)
)
;
await
this
.
tcpStream
.
WriteAsync
(
byteBuffer
,
0
,
byteBuffer
.
Length
,
cancellationToken
)
;
await
this
.
tcpStream
.
FlushAsync
(
cancellationToken
)
;
return
true
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
SendAsync
)
)
;
}
}
internal
async
Task
<
bool
>
ReceiveAsync
(
CancellationToken
cancellationToken
)
{
byte
[
]
receiveBuffer
=
null
;
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
ReceiveAsync
)
)
;
if
(
this
.
tcpStream
.
DataAvailable
)
{
BlynkLogManager
.
LogInformation
(
"data available"
)
;
receiveBuffer
=
Utility
.
ArrayManager
<
byte
>
.
GetArray
(
this
.
tcpClient
.
Available
)
;
// new byte[ this.tcpClient.Available ];
int
count
=
await
this
.
tcpStream
.
ReadAsync
(
receiveBuffer
,
0
,
receiveBuffer
.
Length
,
cancellationToken
)
;
var
commands
=
BlynkMessageParser
.
GetBlynkMessages
(
receiveBuffer
,
count
)
;
return
await
this
.
ProcessMessagesAsync
(
commands
)
;
}
//else {
// Console.Write( "." );
//}
return
false
;
}
finally
{
if
(
receiveBuffer
!=
null
)
{
Utility
.
ArrayManager
<
byte
>
.
ReleaseArray
(
receiveBuffer
)
;
receiveBuffer
=
null
;
}
BlynkLogManager
.
LogMethodEnd
(
nameof
(
ReceiveAsync
)
)
;
}
}
internal
async
Task
GetMessageOnSchedule
(
CancellationToken
cancellationToken
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
GetMessageOnSchedule
)
)
;
while
(
!
cancellationToken
.
IsCancellationRequested
)
{
try
{
if
(
this
.
Connected
)
{
var
receivedMessages
=
await
this
.
ReceiveAsync
(
cancellationToken
)
;
if
(
receivedMessages
)
BlynkLogManager
.
LogInformation
(
"received"
)
;
await
Task
.
Delay
(
this
.
BackgroundReadIntervalInMilliseconds
,
cancellationToken
)
;
}
else
{
BlynkLogManager
.
LogInformation
(
"not connected"
)
;
await
Task
.
Delay
(
10
*
this
.
BackgroundReadIntervalInMilliseconds
,
cancellationToken
)
;
}
}
catch
(
TaskCanceledException
)
{
}
catch
(
Exception
ex
)
{
BlynkLogManager
.
LogException
(
"Background message receiver"
,
ex
)
;
}
}
BlynkLogManager
.
LogInformation
(
"canceled"
)
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
GetMessageOnSchedule
)
)
;
}
}
private
async
Task
SendPingsOnSchedule
(
CancellationToken
cancellationToken
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
SendPingsOnSchedule
)
)
;
while
(
!
cancellationToken
.
IsCancellationRequested
)
{
try
{
if
(
this
.
Connected
)
{
BlynkLogManager
.
LogInformation
(
"ping"
)
;
var
pingCommand
=
new
BlynkCommand
(
BlynkCommandType
.
BLYNK_CMD_PING
,
NextMessageId
,
false
)
;
pingCommand
.
Append
(
(
Int16
)
0
)
;
await
this
.
SendAsync
(
pingCommand
.
ToArray
(
)
,
cancellationToken
)
;
}
else
{
BlynkLogManager
.
LogInformation
(
"not connected"
)
;
}
await
Task
.
Delay
(
this
.
PingIntervalInMilliseconds
,
cancellationToken
)
;
}
catch
(
TaskCanceledException
)
{
}
catch
(
Exception
ex
)
{
BlynkLogManager
.
LogException
(
"Background ping sender"
,
ex
)
;
}
}
BlynkLogManager
.
LogInformation
(
"canceled"
)
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
SendPingsOnSchedule
)
)
;
}
}
private
async
Task
<
bool
>
ProcessMessagesAsync
(
IEnumerable
<
BlynkMessageParser
>
blynkMessages
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
ProcessMessagesAsync
)
)
;
bool
totalResult
=
false
;
foreach
(
var
blynkMessage
in
blynkMessages
)
{
bool
result
=
await
blynkMessage
.
ParseMessageAsync
(
this
)
;
totalResult
=
totalResult
&&
result
;
}
return
totalResult
;
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
ProcessMessagesAsync
)
)
;
}
}
#region IDisposable Support
private
bool
disposedValue
=
false
;
// To detect redundant calls
protected
virtual
void
Dispose
(
bool
disposing
)
{
try
{
BlynkLogManager
.
LogMethodBegin
(
nameof
(
Dispose
)
)
;
if
(
!
disposedValue
)
{
if
(
disposing
)
{
if
(
this
.
tcpClient
!=
null
)
{
this
.
tcpClient
.
Dispose
(
)
;
this
.
tcpClient
=
null
;
}
if
(
this
.
tcpStream
!=
null
)
{
this
.
tcpStream
.
Dispose
(
)
;
this
.
tcpStream
=
null
;
}
}
disposedValue
=
true
;
}
}
finally
{
BlynkLogManager
.
LogMethodEnd
(
nameof
(
Dispose
)
)
;
}
}
public
void
Dispose
(
)
{
Dispose
(
true
)
;
}
#endregion
}
}
Back
|
FazBrowse Home
|
New Git URL