Coder Social home page Coder Social logo

jczic / xasyncsockets Goto Github PK

View Code? Open in Web Editor NEW
38.0 7.0 3.0 302 KB

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

Home Page: https://xasyncsockets.hc2.fr

License: MIT License

Python 100.00%
python asynchronous sockets tcp udp concurrent-processes hc2 client-server micropython fifo-queue

xasyncsockets's Introduction

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 name Description
XAsyncSocketsPool Managed pool of 'XAsyncSocket' objects
XClosedReason Enumerator of 'XAsyncSocket' closing reasons
XAsyncSocket Abstract class of managed asynchronous sockets
XAsyncTCPServer TCP server implementation of 'XAsyncSocket'
XAsyncTCPClient TCP client implementation of 'XAsyncSocket'
XAsyncUDPDatagram UDP sender/recever implementation of 'XAsyncSocket'
XBufferSlot Managed buffer
XBufferSlots Managed buffers collection
XFiFo Dedicated FiFo queue

XAsyncSockets exceptions :

Class name Description
XAsyncSocketsPoolException Exception class for 'XAsyncSocketsPool'
XAsyncSocketException Exception class for 'XAsyncSocket'
XAsyncTCPServerException Exception class for 'XAsyncTCPServer'
XAsyncTCPClientException Exception class for 'XAsyncTCPClient'
XAsyncUDPDatagramException Exception class for 'XAsyncUDPDatagram'
XFiFoException Exception class for 'XFiFo'

XAsyncSocketsPool class details :

Method Arguments
Constructor None
GetAllAsyncSockets None
GetAsyncSocketByID id (int)
AsyncWaitEvents threadsCount=0 (int)
StopWaitEvents None
Property Details
WaitEventsProcessing Return True if "WaitEvents" is in processing

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

XClosedReason class details :

Static variable Value
Error 0x00
ClosedByHost 0x01
ClosedByPeer 0x02
Timeout 0x03

XAsyncSocket class details :

Method Arguments
GetAsyncSocketsPool None
GetSocketObj None
Close None
Property Details
SocketID Get the opened socket unique ID
OnClosed Get or set an event of type f(closedReason)
State Get or set a custom object

XAsyncTCPServer class details :

Method Arguments
Create (static) asyncSocketsPool, srvAddr (tuple of ip and port), srvBacklog=256 (int), bufSlots=None
Property Details
SrvAddr Tuple of ip and port
OnClientAccepted Get or set an event of type f(xAsyncTCPServer, xAsyncTCPClient)

XAsyncTCPClient class details :

Method Arguments
Create (static) asyncSocketsPool, srvAddr (tuple of ip and port), connectTimeout=5 (int), recvBufLen=4096 (int), sendBufLen=4096(int), connectAsync=True (bool)
AsyncRecvLine lineEncoding='UTF-8', onLineRecv=None (function), onLineRecvArg=None (object), timeoutSec=None (int)
AsyncRecvData size=None (int), onDataRecv=None (function), onDataRecvArg=None (object), timeoutSec=None (int)
AsyncSendData data (bytes or buffer protocol), onDataSent=None (function), onDataSentArg=None (object)
AsyncSendSendingBuffer size=None (int), onDataSent=None (function), onDataSentArg=None (object)
StartSSL keyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE, ca_certs=None
StartSSLContext sslContext, 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)
Property Details
SrvAddr Tuple of ip and port
CliAddr Tuple of ip and port
IsSSL Return True if SSL is used
SendingBuffer Get the existing buffer (memoryview) used to send data
OnFailsToConnect Get or set an event of type f(xAsyncTCPClient)
OnConnected Get or set an event of type f(xAsyncTCPClient)

XAsyncUDPDatagram class details :

Method Arguments
Create (static) asyncSocketsPool, localAddr=None (tuple of ip and port), recvBufLen=4096 (int), broadcast=False (bool)
AsyncSendDatagram datagram (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)
Property Details
LocalAddr Tuple of ip and port
OnRecv Get or set an event of type f(xAsyncUDPDatagram, remoteAddr, datagram)
OnFailsToSend Get or set an event of type f(xAsyncUDPDatagram, datagram, remoteAddr)

XBufferSlot class details :

Method Arguments
Constructor size (int), keepAlloc=True (bool)
Property Details
Available Get or set the availability of the slot
Size Get the buffer size of the slot
Buffer Get the buffer of the slot

XBufferSlots class details :

Method Arguments
Constructor slotsCount (int), slotsSize (int), keepAlloc=True (bool)
GetAvailableSlot None
Property Details
SlotsCount Get the number of slots
SlotsSize Get the buffer size of each slots
Slots Get the list of slots

XFiFo class details :

Method Arguments
Constructor None
Put obj (object)
Get None
Clear None
Property Details
Empty Return True if the FiFo is empty

By JC`zic for HC² ;')

Keep it simple, stupid 👍

xasyncsockets's People

Contributors

jczic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

xasyncsockets's Issues

tcpCli how to get a reply?

I modified it(tcpCli.py):

countClosed = 0

pool    = XAsyncSocketsPool()
srvAddr = ('192.168.0.190', 8090)

for i in range(1) :
    cli = XAsyncTCPClient.Create(pool, srvAddr,5,4096,4096,False)
    if cli :
        print("Client %s created" % (i+1))
        cli.OnFailsToConnect = _onTCPClientFailsToConnect
        cli.OnConnected      = _onTCPClientConnected
        cli.OnClosed         = _onTCPClientClosed
    else :
        print("Error to create client %s..." % (i+1))

pool.AsyncWaitEvents(threadsCount=1)
time.sleep(7)
pool.StopWaitEvents()

Program output:

Client 1 created
On TCP Client Connected
On TCP Client Data Sent (test)

When I send a message to it, the program outputs:
1) On TCP Client Closed (error)
How should I configure to receive information circularly, and whether there is automatic reconnection function
Looking forward to your reply, thank you

Use of select.select vs select.poll

This code is using uselect.select() to block for events. However, I want to run the code under the Unix port of micropython (and esp32). That port doesn't provide the select method, and assumes everyone wants to use the superior poll object interface. The docs also have pretty strong language about select.select(): This function is provided by some MicroPython ports for compatibility and is not efficient. Usage of Poll is recommended instead. see here

It doesn't look hard to rework to use poll() but I'm surprised it wasn't done already and I'm thinking this might be a deliberate choice?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.