-
Notifications
You must be signed in to change notification settings - Fork 3
/
XAsyncSockets.py
1259 lines (1088 loc) · 48 KB
/
XAsyncSockets.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The MIT License (MIT)
Copyright © 2019 Jean-Christophe Bos & HC² (www.hc2.fr)
"""
from _thread import allocate_lock, start_new_thread, stack_size
from time import sleep
from select import select
import socket
import ssl
try :
from time import perf_counter
except :
from time import ticks_ms
def perf_counter() :
return ticks_ms() / 1000
# ============================================================================
# ===( XAsyncSocketsPool )====================================================
# ============================================================================
class XAsyncSocketsPoolException(Exception) :
pass
class XAsyncSocketsPool :
_CHECK_SEC_INTERVAL = 1.0
def __init__(self) :
self._processing = None
self._microWorkers = None
self._opLock = allocate_lock()
self._asyncSockets = { }
self._readList = [ ]
self._writeList = [ ]
self._handlingList = [ ]
self._udpSockEvt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for i in range(30) :
self._udpSockEvtAddr = ('127.0.0.1', 54321+i)
try :
self._udpSockEvt.bind(self._udpSockEvtAddr)
break
except :
pass
# ------------------------------------------------------------------------
def _addSocket(self, socket, asyncSocket) :
if socket and asyncSocket :
with self._opLock :
if socket not in self._asyncSockets :
self._asyncSockets[socket] = asyncSocket
return True
return False
# ------------------------------------------------------------------------
def _removeSocket(self, socket) :
if socket :
with self._opLock :
if socket in self._asyncSockets :
del self._asyncSockets[socket]
if socket in self._readList :
self._readList.remove(socket)
if socket in self._writeList :
self._writeList.remove(socket)
return True
return False
# ------------------------------------------------------------------------
def _socketListAdd(self, socket, socketsList) :
with self._opLock :
if socket not in socketsList :
socketsList.append(socket)
return True
return False
# ------------------------------------------------------------------------
def _socketListRemove(self, socket, socketsList) :
with self._opLock :
if socket in socketsList :
socketsList.remove(socket)
return True
return False
# ------------------------------------------------------------------------
def _sendUDPSockEvent(self) :
self._udpSockEvt.sendto(b'\xFF', self._udpSockEvtAddr)
# ------------------------------------------------------------------------
def _processWaitEvents(self) :
def jobExceptionalCondition(args) :
if args[0].OnExceptionalCondition() :
self._removeSocket(args[1])
self._socketListRemove(args[1], self._handlingList)
def jobReadyForWriting(args) :
if args[0].OnReadyForWriting() :
self._removeSocket(args[1])
self._socketListRemove(args[1], self._handlingList)
def jobReadyForReading(args) :
if args[0].OnReadyForReading() :
self._removeSocket(args[1])
self._socketListRemove(args[1], self._handlingList)
self._processing = True
self._socketListAdd(self._udpSockEvt, self._readList)
timeSec = perf_counter()
udpSockEvtBuf = bytearray(32)
while self._processing :
try :
try :
rd, wr, ex = select( self._readList,
self._writeList,
self._readList,
XAsyncSocketsPool._CHECK_SEC_INTERVAL )
except KeyboardInterrupt :
break
except :
sleep(XAsyncSocketsPool._CHECK_SEC_INTERVAL)
continue
if not self._processing :
break
for socketsList in ex, wr, rd :
for sock in socketsList :
if sock == self._udpSockEvt :
self._udpSockEvt.recv_into(udpSockEvtBuf)
else :
asyncSocket = self._asyncSockets.get(sock)
if asyncSocket and asyncSocket.GetSocketObj() == sock and sock.fileno() != -1 :
if self._socketListAdd(sock, self._handlingList) :
if socketsList is rd :
if self._microWorkers :
self._microWorkers.AddJob(jobReadyForReading, (asyncSocket, sock))
else :
jobReadyForReading((asyncSocket, sock))
elif socketsList is wr :
self._socketListRemove(sock, self._writeList)
if self._microWorkers :
self._microWorkers.AddJob(jobReadyForWriting, (asyncSocket, sock))
else :
jobReadyForWriting((asyncSocket, sock))
else :
self._removeSocket(sock)
if self._microWorkers :
self._microWorkers.AddJob(jobExceptionalCondition, (asyncSocket, sock))
else :
jobExceptionalCondition((asyncSocket, sock))
else :
self._removeSocket(sock)
sock.close()
sec = perf_counter()
if sec > timeSec + XAsyncSocketsPool._CHECK_SEC_INTERVAL :
timeSec = sec
for asyncSocket in list(self._asyncSockets.values()) :
if asyncSocket.ExpireTimeSec and \
timeSec > asyncSocket.ExpireTimeSec :
asyncSocket._close(XClosedReason.Timeout)
except :
pass
if self._microWorkers :
self._microWorkers.StopAll()
self._microWorkers = None
for asyncSocket in list(self._asyncSockets.values()) :
try :
asyncSocket.Close()
except :
pass
self._readList.clear()
self._writeList.clear()
self._processing = None
# ------------------------------------------------------------------------
def AddAsyncSocket(self, asyncSocket) :
try :
socket = asyncSocket.GetSocketObj()
except :
raise XAsyncSocketsPoolException('AddAsyncSocket : "asyncSocket" is incorrect.')
return self._addSocket(socket, asyncSocket)
# ------------------------------------------------------------------------
def RemoveAsyncSocket(self, asyncSocket) :
try :
socket = asyncSocket.GetSocketObj()
except :
raise XAsyncSocketsPoolException('RemoveAsyncSocket : "asyncSocket" is incorrect.')
return self._removeSocket(socket)
# ------------------------------------------------------------------------
def GetAllAsyncSockets(self) :
return list(self._asyncSockets.values())
# ------------------------------------------------------------------------
def GetAsyncSocketByID(self, id) :
return self._asyncSockets.get(id, None)
# ------------------------------------------------------------------------
def NotifyNextReadyForReading(self, asyncSocket, notify) :
try :
socket = asyncSocket.GetSocketObj()
except :
raise XAsyncSocketsPoolException('NotifyNextReadyForReading : "asyncSocket" is incorrect.')
if notify :
if self._socketListAdd(socket, self._readList) :
self._sendUDPSockEvent()
else :
self._socketListRemove(socket, self._readList)
# ------------------------------------------------------------------------
def NotifyNextReadyForWriting(self, asyncSocket, notify) :
try :
socket = asyncSocket.GetSocketObj()
except :
raise XAsyncSocketsPoolException('NotifyNextReadyForWriting : "asyncSocket" is incorrect.')
if notify :
if self._socketListAdd(socket, self._writeList) :
self._sendUDPSockEvent()
else :
self._socketListRemove(socket, self._writeList)
# ------------------------------------------------------------------------
def AsyncWaitEvents(self, threadsCount=0) :
if self.WaitEventsProcessing :
return
self._processing = False
if threadsCount > 0 :
try :
if threadsCount > 1 :
self._microWorkers = MicroWorkers(workersCount=threadsCount-1)
start_new_thread(self._processWaitEvents, ())
while self._processing != True :
sleep(0.010)
except :
if self._microWorkers :
self._microWorkers.StopAll()
self._microWorkers = None
self._processing = None
raise XAsyncSocketsPoolException('AsyncWaitEvents : Fatal error to create new threads...')
else :
self._processWaitEvents()
# ------------------------------------------------------------------------
def StopWaitEvents(self) :
if not self.WaitEventsProcessing :
return
self._processing = False
self._sendUDPSockEvent()
while self.WaitEventsProcessing :
sleep(0.010)
# ------------------------------------------------------------------------
@property
def WaitEventsProcessing(self) :
return (self._processing is not None)
# ============================================================================
# ===( XClosedReason )========================================================
# ============================================================================
class XClosedReason() :
Error = 0x00
ClosedByHost = 0x01
ClosedByPeer = 0x02
Timeout = 0x03
# ============================================================================
# ===( XAsyncSocket )=========================================================
# ============================================================================
class XAsyncSocketException(Exception) :
pass
class XAsyncSocket :
def __init__(self, asyncSocketsPool, socket, recvBufSlot=None, sendBufSlot=None) :
if type(self) is XAsyncSocket :
raise XAsyncSocketException('XAsyncSocket is an abstract class and must be implemented.')
self._asyncSocketsPool = asyncSocketsPool
self._socket = socket
self._recvBufSlot = recvBufSlot
self._sendBufSlot = sendBufSlot
self._expireTimeSec = None
self._state = None
self._onClosed = None
try :
socket.settimeout(0)
socket.setblocking(0)
if (recvBufSlot is not None and type(recvBufSlot) is not XBufferSlot) or \
(sendBufSlot is not None and type(sendBufSlot) is not XBufferSlot) :
raise Exception()
asyncSocketsPool.AddAsyncSocket(self)
except :
raise XAsyncSocketException('XAsyncSocket : Arguments are incorrects.')
# ------------------------------------------------------------------------
def _setExpireTimeout(self, timeoutSec) :
try :
if timeoutSec and timeoutSec > 0 :
self._expireTimeSec = perf_counter() + timeoutSec
except :
raise XAsyncSocketException('"timeoutSec" is incorrect to set expire timeout.')
# ------------------------------------------------------------------------
def _removeExpireTimeout(self) :
self._expireTimeSec = None
# ------------------------------------------------------------------------
def _close(self, closedReason=XClosedReason.Error, triggerOnClosed=True) :
if self._asyncSocketsPool.RemoveAsyncSocket(self) :
try :
self._socket.close()
except :
pass
if self._recvBufSlot is not None :
self._recvBufSlot.Available = True
self._recvBufSlot = None
if self._sendBufSlot is not None :
self._sendBufSlot.Available = True
self._sendBufSlot = None
if triggerOnClosed and self._onClosed :
try :
self._onClosed(self, closedReason)
except Exception as ex :
raise XAsyncSocketException('Error when handling the "OnClose" event : %s' % ex)
return True
return False
# ------------------------------------------------------------------------
def GetAsyncSocketsPool(self) :
return self._asyncSocketsPool
# ------------------------------------------------------------------------
def GetSocketObj(self) :
return self._socket
# ------------------------------------------------------------------------
def Close(self) :
return self._close(XClosedReason.ClosedByHost)
# ------------------------------------------------------------------------
def OnReadyForReading(self) :
return True
# ------------------------------------------------------------------------
def OnReadyForWriting(self) :
return True
# ------------------------------------------------------------------------
def OnExceptionalCondition(self) :
self._close()
return True
# ------------------------------------------------------------------------
@property
def SocketID(self) :
return self._socket.fileno() if self._socket else None
@property
def ExpireTimeSec(self) :
return self._expireTimeSec
@property
def OnClosed(self) :
return self._onClosed
@OnClosed.setter
def OnClosed(self, value) :
self._onClosed = value
@property
def State(self) :
return self._state
@State.setter
def State(self, value) :
self._state = value
# ============================================================================
# ===( XAsyncTCPServer )======================================================
# ============================================================================
class XAsyncTCPServerException(Exception) :
pass
class XAsyncTCPServer(XAsyncSocket) :
@staticmethod
def Create(asyncSocketsPool, srvAddr, srvBacklog=256, bufSlots=None) :
try :
srvSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except :
raise XAsyncTCPServerException('Create : Cannot open socket (no enought memory).')
try :
srvSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srvSocket.bind(srvAddr)
srvSocket.listen(srvBacklog)
except :
raise XAsyncTCPServerException('Create : Error to binding the TCP server on this address.')
if not bufSlots :
bufSlots = XBufferSlots(256, 4096, keepAlloc=True)
xAsyncTCPServer = XAsyncTCPServer( asyncSocketsPool,
srvSocket,
srvAddr,
bufSlots )
asyncSocketsPool.NotifyNextReadyForReading(xAsyncTCPServer, True)
return xAsyncTCPServer
# ------------------------------------------------------------------------
def __init__(self, asyncSocketsPool, srvSocket, srvAddr, bufSlots) :
try :
super().__init__(asyncSocketsPool, srvSocket)
self._srvAddr = srvAddr
self._bufSlots = bufSlots
self._onClientAccepted = None
except :
raise XAsyncTCPServerException('Error to creating XAsyncTCPServer, arguments are incorrects.')
# ------------------------------------------------------------------------
def OnReadyForReading(self) :
try :
cliSocket, cliAddr = self._socket.accept()
except :
return
recvBufSlot = self._bufSlots.GetAvailableSlot()
sendBufSlot = self._bufSlots.GetAvailableSlot()
if not recvBufSlot or not sendBufSlot or not self._onClientAccepted :
if recvBufSlot :
recvBufSlot.Available = True
if sendBufSlot :
sendBufSlot.Available = True
cliSocket.close()
return
asyncTCPCli = XAsyncTCPClient( self._asyncSocketsPool,
cliSocket,
self._srvAddr,
cliAddr,
recvBufSlot,
sendBufSlot )
try :
self._onClientAccepted(self, asyncTCPCli)
except Exception as ex :
asyncTCPCli._close()
raise XAsyncTCPServerException('Error when handling the "OnClientAccepted" event : %s' % ex)
# ------------------------------------------------------------------------
@property
def SrvAddr(self) :
return self._srvAddr
@property
def OnClientAccepted(self) :
return self._onClientAccepted
@OnClientAccepted.setter
def OnClientAccepted(self, value) :
self._onClientAccepted = value
# ============================================================================
# ===( XAsyncTCPClient )======================================================
# ============================================================================
class XAsyncTCPClientException(Exception) :
pass
class XAsyncTCPClient(XAsyncSocket) :
@staticmethod
def Create( asyncSocketsPool,
srvAddr,
connectTimeout = 5,
recvBufLen = 4096,
sendBufLen = 4096,
connectAsync = True ) :
try :
size = max(256, recvBufLen)
recvBufSlot = XBufferSlot(size=size, keepAlloc=True)
size = max(256, sendBufLen)
sendBufSlot = XBufferSlot(size=size, keepAlloc=True)
except :
raise XAsyncTCPClientException('Create : Out of memory?')
try :
cliSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except :
raise XAsyncTCPClientException('Create : Cannot open socket (no enought memory).')
asyncTCPCli = XAsyncTCPClient( asyncSocketsPool,
cliSocket,
srvAddr,
None,
recvBufSlot,
sendBufSlot )
ok = False
try :
if connectAsync and hasattr(cliSocket, 'connect_ex') :
errno = cliSocket.connect_ex(srvAddr)
if errno == 0 or errno == 36 :
asyncTCPCli._setExpireTimeout(connectTimeout)
ok = True
else :
try :
addr = socket.getaddrinfo( srvAddr[0],
srvAddr[1],
socket.AF_INET )
except :
addr = socket.getaddrinfo( srvAddr[0],
srvAddr[1] )
addr = addr[0][-1]
if connectAsync :
asyncTCPCli._setExpireTimeout(connectTimeout)
else :
cliSocket.settimeout(connectTimeout)
cliSocket.setblocking(1)
try :
cliSocket.connect(srvAddr)
except OSError as ex :
if not connectAsync or str(ex) != '119' :
raise ex
if not connectAsync :
cliSocket.settimeout(0)
cliSocket.setblocking(0)
ok = True
except :
pass
if ok :
asyncSocketsPool.NotifyNextReadyForWriting(asyncTCPCli, True)
return asyncTCPCli
asyncTCPCli._close()
return None
# ------------------------------------------------------------------------
def __init__(self, asyncSocketsPool, cliSocket, srvAddr, cliAddr, recvBufSlot, sendBufSlot) :
try :
super().__init__(asyncSocketsPool, cliSocket, recvBufSlot, sendBufSlot)
self._srvAddr = srvAddr
self._cliAddr = cliAddr if cliAddr else ('0.0.0.0', 0)
self._onFailsToConnect = None
self._onConnected = None
self._onDataRecv = None
self._onDataRecvArg = None
self._onDataSent = None
self._onDataSentArg = None
self._sizeToRecv = None
self._rdLinePos = None
self._rdLineEncoding = None
self._rdBufView = None
self._wrBufView = None
self._socketOpened = (cliAddr is not None)
except :
raise XAsyncTCPClientException('Error to creating XAsyncTCPClient, arguments are incorrects.')
# ------------------------------------------------------------------------
def Close(self) :
if self._wrBufView :
try :
self._socket.send(self._wrBufView)
except :
pass
try :
self._socket.shutdown(socket.SHUT_RDWR)
except :
pass
return self._close(XClosedReason.ClosedByHost)
# ------------------------------------------------------------------------
def OnReadyForReading(self) :
while True :
if self._rdLinePos is not None :
# In the context of reading a line,
while True :
try :
try :
b = self._socket.recv(1)
except ssl.SSLError as sslErr :
if sslErr.args[0] != ssl.SSL_ERROR_WANT_READ :
self._close()
return
except BlockingIOError as bioErr :
if bioErr.errno != 35 :
self._close()
return
except :
self._close()
return
except :
self._close()
return
if b :
if b == b'\n' :
lineLen = self._rdLinePos
self._rdLinePos = None
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._removeExpireTimeout()
if self._onDataRecv :
line = self._recvBufSlot.Buffer[:lineLen]
try :
line = bytes(line).decode(self._rdLineEncoding)
except :
line = None
try :
self._onDataRecv(self, line, self._onDataRecvArg)
except Exception as ex :
raise XAsyncTCPClientException('Error when handling the "OnDataRecv" event : %s' % ex)
if self.IsSSL and self._socket.pending() > 0 :
break
return
elif b != b'\r' :
try :
if self._rdLinePos < self._recvBufSlot.Size :
self._recvBufSlot.Buffer[self._rdLinePos] = ord(b)
self._rdLinePos += 1
else :
self._close()
return
except :
return
else :
self._close(XClosedReason.ClosedByPeer)
return
elif self._sizeToRecv :
# In the context of reading data,
recvBuf = self._rdBufView[-self._sizeToRecv:]
try :
try :
n = self._socket.recv_into(recvBuf)
except ssl.SSLError as sslErr :
if sslErr.args[0] != ssl.SSL_ERROR_WANT_READ :
self._close()
return
except BlockingIOError as bioErr :
if bioErr.errno != 35 :
self._close()
return
except :
self._close()
return
except :
try :
n = self._socket.readinto(recvBuf)
except :
self._close()
return
if not n :
self._close(XClosedReason.ClosedByPeer)
return
self._sizeToRecv -= n
if not self._sizeToRecv :
data = self._rdBufView
self._rdBufView = None
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._removeExpireTimeout()
if self._onDataRecv :
try :
self._onDataRecv(self, data, self._onDataRecvArg)
except Exception as ex :
raise XAsyncTCPClientException('Error when handling the "OnDataRecv" event : %s' % ex)
if not self.IsSSL or self._socket.pending() == 0 :
return
else :
self._close(XClosedReason.ClosedByHost)
return True
# ------------------------------------------------------------------------
def OnReadyForWriting(self) :
if not self._socketOpened :
if hasattr(self._socket, "getsockopt") :
if self._socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) :
self._close(XClosedReason.Error, triggerOnClosed=False)
if self._onFailsToConnect :
try :
self._onFailsToConnect(self)
except Exception as ex :
raise XAsyncTCPClientException('Error when handling the "OnFailsToConnect" event : %s' % ex)
return
self._cliAddr = self._socket.getsockname()
self._removeExpireTimeout()
self._socketOpened = True
if self._onConnected :
try :
self._onConnected(self)
except Exception as ex :
raise XAsyncTCPClientException('Error when handling the "OnConnected" event : %s' % ex)
return
if self._wrBufView :
try :
n = self._socket.send(self._wrBufView)
except Exception as ex :
if hasattr(ssl, 'SSLEOFError') and isinstance(ex, ssl.SSLEOFError) :
self._close()
return True
else :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, True)
return
self._wrBufView = self._wrBufView[n:]
if self._wrBufView :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, True)
elif self._onDataSent :
try :
self._onDataSent(self, self._onDataSentArg)
except Exception as ex :
raise XAsyncTCPClientException('Error when handling the "OnDataSent" event : %s' % ex)
# ------------------------------------------------------------------------
def AsyncRecvLine(self, lineEncoding='UTF-8', onLineRecv=None, onLineRecvArg=None, timeoutSec=None) :
if self._rdLinePos is not None or self._sizeToRecv :
raise XAsyncTCPClientException('AsyncRecvLine : Already waiting asynchronous receive.')
if self._socket :
self._setExpireTimeout(timeoutSec)
self._rdLinePos = 0
self._rdLineEncoding = lineEncoding
self._onDataRecv = onLineRecv
self._onDataRecvArg = onLineRecvArg
self._asyncSocketsPool.NotifyNextReadyForReading(self, True)
return True
return False
# ------------------------------------------------------------------------
def AsyncRecvData(self, size=None, onDataRecv=None, onDataRecvArg=None, timeoutSec=None) :
if self._rdLinePos is not None or self._sizeToRecv :
raise XAsyncTCPClientException('AsyncRecvData : Already waiting asynchronous receive.')
if self._socket :
if size is None :
size = self._recvBufSlot.Size
elif not isinstance(size, int) or size <= 0 :
raise XAsyncTCPClientException('AsyncRecvData : "size" is incorrect.')
if size <= self._recvBufSlot.Size :
self._rdBufView = memoryview(self._recvBufSlot.Buffer)[:size]
else :
try :
self._rdBufView = memoryview(bytearray(size))
except :
raise XAsyncTCPClientException('AsyncRecvData : No enought memory to receive %s bytes.' % size)
self._setExpireTimeout(timeoutSec)
self._sizeToRecv = size
self._onDataRecv = onDataRecv
self._onDataRecvArg = onDataRecvArg
self._asyncSocketsPool.NotifyNextReadyForReading(self, True)
return True
return False
# ------------------------------------------------------------------------
def AsyncSendData(self, data, onDataSent=None, onDataSentArg=None) :
if self._socket :
try :
if bytes([data[0]]) :
if self._wrBufView :
self._wrBufView = memoryview(bytes(self._wrBufView) + data)
else :
self._wrBufView = memoryview(data)
self._onDataSent = onDataSent
self._onDataSentArg = onDataSentArg
self._asyncSocketsPool.NotifyNextReadyForWriting(self, True)
return True
except :
pass
raise XAsyncTCPClientException('AsyncSendData : "data" is incorrect.')
return False
# ------------------------------------------------------------------------
def AsyncSendSendingBuffer(self, size=None, onDataSent=None, onDataSentArg=None) :
if self._wrBufView :
raise XAsyncTCPClientException('AsyncSendBufferSlot : Already waiting to send data.')
if self._socket :
if size is None :
size = self._sendBufSlot.Size
if size > 0 and size <= self._sendBufSlot.Size :
self._wrBufView = memoryview(self._sendBufSlot.Buffer)[:size]
self._onDataSent = onDataSent
self._onDataSentArg = onDataSentArg
self._asyncSocketsPool.NotifyNextReadyForWriting(self, True)
return True
return False
# ------------------------------------------------------------------------
def _doSSLHandshake(self) :
count = 0
while count < 10 :
try :
self._socket.do_handshake()
break
except ssl.SSLError as sslErr :
count += 1
if sslErr.args[0] == ssl.SSL_ERROR_WANT_READ :
select([self._socket], [], [], 1)
elif sslErr.args[0] == ssl.SSL_ERROR_WANT_WRITE :
select([], [self._socket], [], 1)
else :
raise XAsyncTCPClientException('SSL : Bad handshake : %s' % sslErr)
except Exception as ex :
raise XAsyncTCPClientException('SSL : Handshake error : %s' % ex)
else :
raise XAsyncTCPClientException('SSL : Handshake error')
# ------------------------------------------------------------------------
def StartSSL( self,
keyfile = None,
certfile = None,
server_side = False,
cert_reqs = 0,
ca_certs = None ) :
if not hasattr(ssl, 'SSLContext') :
raise XAsyncTCPClientException('StartSSL : This SSL implementation is not supported.')
if self.IsSSL :
raise XAsyncTCPClientException('StartSSL : SSL already started.')
try :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, False)
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._asyncSocketsPool.RemoveAsyncSocket(self)
self._socket = ssl.wrap_socket( self._socket,
keyfile = keyfile,
certfile = certfile,
server_side = server_side,
cert_reqs = cert_reqs,
ca_certs = ca_certs,
do_handshake_on_connect = False )
self._asyncSocketsPool.AddAsyncSocket(self)
except Exception as ex :
raise XAsyncTCPClientException('StartSSL : %s' % ex)
self._doSSLHandshake()
# ------------------------------------------------------------------------
def StartSSLContext(self, sslContext, serverSide=False) :
if not hasattr(ssl, 'SSLContext') :
raise XAsyncTCPClientException('StartSSLContext : This SSL implementation is not supported.')
if not isinstance(sslContext, ssl.SSLContext) :
raise XAsyncTCPClientException('StartSSLContext : "sslContext" is incorrect.')
if self.IsSSL :
raise XAsyncTCPClientException('StartSSLContext : SSL already started.')
try :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, False)
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._asyncSocketsPool.RemoveAsyncSocket(self)
self._socket = sslContext.wrap_socket( self._socket,
server_side = serverSide,
do_handshake_on_connect = False )
self._asyncSocketsPool.AddAsyncSocket(self)
except Exception as ex :
raise XAsyncTCPClientException('StartSSLContext : %s' % ex)
self._doSSLHandshake()
# ------------------------------------------------------------------------
@property
def SrvAddr(self) :
return self._srvAddr
@property
def CliAddr(self) :
return self._cliAddr
@property
def IsSSL(self) :
return ( hasattr(ssl, 'SSLContext') and \
hasattr(ssl, 'SSLSocket') and \
isinstance(self._socket, ssl.SSLSocket) )
@property
def SendingBuffer(self) :
return self._sendBufSlot.Buffer
@property
def OnFailsToConnect(self) :
return self._onFailsToConnect
@OnFailsToConnect.setter
def OnFailsToConnect(self, value) :
self._onFailsToConnect = value
@property
def OnConnected(self) :
return self._onConnected
@OnConnected.setter
def OnConnected(self, value) :
self._onConnected = value
# ============================================================================
# ===( XAsyncUDPDatagram )====================================================
# ============================================================================
class XAsyncUDPDatagramException(Exception) :
pass
class XAsyncUDPDatagram(XAsyncSocket) :
@staticmethod
def Create(asyncSocketsPool, localAddr=None, recvBufLen=4096, broadcast=False) :
try :
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except :
raise XAsyncUDPDatagramException('Create : Cannot open socket (no enought memory).')
if broadcast :
udpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
openRecv = (localAddr is not None)
if openRecv :
try :
udpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udpSocket.bind(localAddr)
except :
raise XAsyncUDPDatagramException('Create : Error to binding the UDP Datagram local address.')
try :
size = max(256, recvBufLen)
recvBufSlot = XBufferSlot(size=size, keepAlloc=False)
except :
raise XAsyncUDPDatagramException('Create : Out of memory?')
else :
recvBufSlot = None
xAsyncUDPDatagram = XAsyncUDPDatagram(asyncSocketsPool, udpSocket, recvBufSlot)
if openRecv :
asyncSocketsPool.NotifyNextReadyForReading(xAsyncUDPDatagram, True)
return xAsyncUDPDatagram
# ------------------------------------------------------------------------
def __init__(self, asyncSocketsPool, udpSocket, recvBufSlot) :
try :
super().__init__(asyncSocketsPool, udpSocket, recvBufSlot, None)
self._wrDgramFiFo = XFiFo()
self._onFailsToSend = None
self._onDataSent = None
self._onDataSentArg = None
self._onDataRecv = None
except :
raise XAsyncUDPDatagramException('Error to creating XAsyncUDPDatagram, arguments are incorrects.')
# ------------------------------------------------------------------------
def OnReadyForReading(self) :
try :
n, remoteAddr = self._socket.recvfrom_into(self._recvBufSlot.Buffer)
datagram = memoryview(self._recvBufSlot.Buffer)[:n]
except :
try :
buf, remoteAddr = self._socket.recvfrom(self._recvBufSlot.Size)
datagram = memoryview(buf)
except :
return True
if self._onDataRecv :
try :
self._onDataRecv(self, remoteAddr, datagram)
except Exception as ex :
raise XAsyncUDPDatagramException('Error when handling the "OnDataRecv" event : %s' % ex)
# ------------------------------------------------------------------------
def OnReadyForWriting(self) :
if not self._wrDgramFiFo.Empty :
datagram = None
remoteAddr = ('0.0.0.0', 0)
try :
datagram, remoteAddr = self._wrDgramFiFo.Get()
self._socket.sendto(datagram, remoteAddr)
except :
if self._onFailsToSend :
try :
self._onFailsToSend(self, datagram, remoteAddr)
except Exception as ex :
raise XAsyncUDPDatagramException('Error when handling the "OnFailsToSend" event : %s' % ex)
if not self._wrDgramFiFo.Empty :