Skip to content

asyncTcpServer

Woong Gyu La edited this page Jun 21, 2020 · 4 revisions

Read asyncore in prior to properly use the pyserver!

In order to create TCP server object, you need first declare handler class/classes, which is/are subclass of ITcpSocketCallback, ITcpServerCallback, IAcceptor. The handler class/classes must override below functions:

To handle each socket packet transport, a subclass of ITcpSocketCallback must be implemented:

# def on_newconnection(self, sock, err):
# def on_disconnect(self, sock):
# def on_received(self, sock, data):
# def on_sent(self, sock, status, data):

# Sample implementation is as below:

from pyserver.network import *

class EchoSocketHandler(ITcpSocketCallback):
    def __init__(self):
        pass

    # called when new client connected to the server
    def on_newconnection(self, sock, err):
        print('New connection made')

    # called when the client disconnected
    def on_disconnect(self, sock):
        print('Client disconnected')

    # called when new packet arrived from the client
    def on_received(self, sock, data):
        print('Received : ' + str(data))
        # print('Received : ' + str(data.decode())) # Python3
        sock.send(data)

    # called when packet is sent
    def on_sent(self, sock, status, data):
        print('Sent with status code (' + str(status) + ')')

To handle server activities, a subclass of ITcpServerCallback must be implemented:

# def on_started(self, server):
# def on_accepted(self, server, sock)
# def on_stopped(self, server):


# Sample implementation is as below:

from pyserver.network import *

class EchoServerHandler(ITcpServerCallback):
    def __init__(self):
        pass

    # called when server finished the initialization and started listening
    def on_started(self, server):
        print('The server is started')

    # called when new client accepted
    def on_accepted(self, server, sock):
        print('New socket is accepted')

    # called when the server is stopped listening and disconnect all the clients
    def on_stopped(self, server):
        print('The server is stopped')

To handle newly connected socket, a subclass of IAcceptor must be implemented:

# def on_accept(self, server, addr):
# def get_socket_callback(self):

# Sample implementation is as below:

from pyserver.network import *

class EchoAcceptorHandler(IAcceptor):
    def __init__(self):
        pass

    # called when new client connected
    # must return boolean : True to accept the connection otherwise reject
    def on_accept(self, server, addr):
        return True # accept always

    # called when the server accepted the client and ask for the handler
    # Must return ITcpSocketCallback object
    def get_socket_callback(self):
        return EchoSocketHandler()

After creating the handler class/classes, the TCP server can be simply created as below:

port = 8080
acceptor = EchoAcceptorHandler()
server_handler = EchoServerHandler() 
bind_addr = ''  # '' if any of interface can be used, otherwise give IP address of the interface to use
              # If bindAddr is omitted, then '' is used
no_delay = True # If True, Nagle's algorithm is not used, otherwise use Nagle's Algorithm

tcp_server = AsyncTcpServer(port, server_handler, acceptor, bind_addr, no_delay)

To send the data to the all client connected:

for socket in tcp_server.get_socket_list():
    socket.send('Hello There')
    # socket.send('Hello There'.encode()) # Python3

To stop the server and close all the connected clients:

tcp_server.close()

To disconnect all the clients and keep the server running:

tcp_server.shutdown_all()

Using single handler object to handle every socket request

To handle every connected sockets with a single handler object then subclass of IAcceptor can be implemented as below:

# def on_accept(self, server, addr):
# def get_socket_callback(self):

# Sample implementation is as below:

from pyserver.network import *

class EchoAcceptorHandler(IAcceptor):
    def __init__(self):
        self.handler=EchoSocketHandler()

    # called when new client connected
    # must return boolean : True to accept the connection otherwise reject
    def on_accept(self, server, addr):
        return True # accept always

    # called when the server accepted the client and ask for the handler
    # Must return ITcpSocketCallback object
    def get_socket_callback(self):
        return self.handler

This is it!

Also see asyncTcpClient to find out how to create a TCP client instance!

Clone this wiki locally