README.md

November 16, 2019 · View on GitHub

XAsyncSockets is an efficient Python/MicroPython library of managed asynchronous sockets.

HC²

Very easy to integrate and very light with one file only :

  • "XAsyncSockets.py"

XAsyncSockets features :

  • Managed asynchronous sockets in a pool (up to thousands!)
  • Works directly with I/O to receive and send very quickly
  • Supports very large number of simultaneous TCP connections
  • Supports concurrent synchronous processing operations if necessary (threaded)
  • Implementation of TCP servers
  • Implementation of TCP clients
  • Implementation of UDP datagrams (sender and/or receiver)
  • TCP client can event after a specified size of data or a text line received
  • Each connections and receivings can waiting during a specified time
  • The reasons of TCP client closures are returned
  • Really robust, very fast and easy to use
  • Compatible with MicroPython implementation (sockets layer, FiFo queue, perf counter)

XAsyncSockets classes :

Class nameDescription
XAsyncSocketsPoolManaged pool of 'XAsyncSocket' objects
XClosedReasonEnumerator of 'XAsyncSocket' closing reasons
XAsyncSocketAbstract class of managed asynchronous sockets
XAsyncTCPServerTCP server implementation of 'XAsyncSocket'
XAsyncTCPClientTCP client implementation of 'XAsyncSocket'
XAsyncUDPDatagramUDP sender/recever implementation of 'XAsyncSocket'
XBufferSlotManaged buffer
XBufferSlotsManaged buffers collection
XFiFoDedicated FiFo queue

XAsyncSockets exceptions :

Class nameDescription
XAsyncSocketsPoolExceptionException class for 'XAsyncSocketsPool'
XAsyncSocketExceptionException class for 'XAsyncSocket'
XAsyncTCPServerExceptionException class for 'XAsyncTCPServer'
XAsyncTCPClientExceptionException class for 'XAsyncTCPClient'
XAsyncUDPDatagramExceptionException class for 'XAsyncUDPDatagram'
XFiFoExceptionException class for 'XFiFo'

XAsyncSocketsPool class details :

MethodArguments
ConstructorNone
GetAllAsyncSocketsNone
GetAsyncSocketByIDid (int)
AsyncWaitEventsthreadsCount=0 (int)
StopWaitEventsNone
PropertyDetails
WaitEventsProcessingReturn True if "WaitEvents" is in processing

( Do not call directly the methods AddAsyncSocket, RemoveAsyncSocket, NotifyNextReadyForReading and NotifyNextReadyForWriting )

XClosedReason class details :

Static variableValue
Error0x00
ClosedByHost0x01
ClosedByPeer0x02
Timeout0x03

XAsyncSocket class details :

MethodArguments
GetAsyncSocketsPoolNone
GetSocketObjNone
CloseNone
PropertyDetails
SocketIDGet the opened socket unique ID
OnClosedGet or set an event of type f(closedReason)
StateGet or set a custom object

XAsyncTCPServer class details :

MethodArguments
Create (static)asyncSocketsPool, srvAddr (tuple of ip and port), srvBacklog=256 (int), bufSlots=None
PropertyDetails
SrvAddrTuple of ip and port
OnClientAcceptedGet or set an event of type f(xAsyncTCPServer, xAsyncTCPClient)

XAsyncTCPClient class details :

MethodArguments
Create (static)asyncSocketsPool, srvAddr (tuple of ip and port), connectTimeout=5 (int), recvBufLen=4096 (int), sendBufLen=4096(int), connectAsync=True (bool)
AsyncRecvLinelineEncoding='UTF-8', onLineRecv=None (function), onLineRecvArg=None (object), timeoutSec=None (int)
AsyncRecvDatasize=None (int), onDataRecv=None (function), onDataRecvArg=None (object), timeoutSec=None (int)
AsyncSendDatadata (bytes or buffer protocol), onDataSent=None (function), onDataSentArg=None (object)
AsyncSendSendingBuffersize=None (int), onDataSent=None (function), onDataSentArg=None (object)
StartSSLkeyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE, ca_certs=None
StartSSLContextsslContext, serverSide=False
  • onLineRecv is a callback event of type f(xAsyncTCPClient, line, arg)
  • onDataRecv is a callback event of type f(xAsyncTCPClient, data, arg)
  • onDataSent is a callback event of type f(xAsyncTCPClient, arg)
  • StartSSL and StartSSLContext doesn't works on MicroPython (in asynchronous non-blocking sockets mode)
  • It is widely recommended to use StartSSLContext rather than StartSSL (old version)
PropertyDetails
SrvAddrTuple of ip and port
CliAddrTuple of ip and port
IsSSLReturn True if SSL is used
SendingBufferGet the existing buffer (memoryview) used to send data
OnFailsToConnectGet or set an event of type f(xAsyncTCPClient)
OnConnectedGet or set an event of type f(xAsyncTCPClient)

XAsyncUDPDatagram class details :

MethodArguments
Create (static)asyncSocketsPool, localAddr=None (tuple of ip and port), recvBufLen=4096 (int), broadcast=False (bool)
AsyncSendDatagramdatagram (bytes or buffer protocol), remoteAddr (tuple of ip and port), onDataSent=None (function), onDataSentArg=None (object)
  • onDataSent is a callback event of type f(xAsyncUDPDatagram, arg)
PropertyDetails
LocalAddrTuple of ip and port
OnRecvGet or set an event of type f(xAsyncUDPDatagram, remoteAddr, datagram)
OnFailsToSendGet or set an event of type f(xAsyncUDPDatagram, datagram, remoteAddr)

XBufferSlot class details :

MethodArguments
Constructorsize (int), keepAlloc=True (bool)
PropertyDetails
AvailableGet or set the availability of the slot
SizeGet the buffer size of the slot
BufferGet the buffer of the slot

XBufferSlots class details :

MethodArguments
ConstructorslotsCount (int), slotsSize (int), keepAlloc=True (bool)
GetAvailableSlotNone
PropertyDetails
SlotsCountGet the number of slots
SlotsSizeGet the buffer size of each slots
SlotsGet the list of slots

XFiFo class details :

MethodArguments
ConstructorNone
Putobj (object)
GetNone
ClearNone
PropertyDetails
EmptyReturn True if the FiFo is empty

By JC`zic for HC² ;')

Keep it simple, stupid :+1: