Skip to main content

tcpchan is a TCP (de)multiplexer

Project description

TCPChan

PyPI version

TCPChan is a TCP connection multiplexing library that enables working with multiple channels in a single TCP connection. TCPChan can boost the efficiency of the short-lived connections by eliminating the overhead of connection setup, especially in high-latency links (e.g. cross-continental links).

The core part of the library is decoupled from I/O libraries so it's possible to bring your own I/O library. For convenience, an Asyncio-based protocol implementation is provided for easy integration with Asyncio applications.

Warning: TCPChan is built for fun and educational purpose, it is not fully tested, and neither is it widely deployed. Use it at your own risk.

Guide

WIP

Installation

Install via Pip

pip install tcpchan

Install latest version from GitHub

git clone --depth 1 https://github.com/frankurcrazy/tcpchan
cd tcpchan; python setup.py install

Dependencies

  1. python >= 3.7
  2. fpack >= 1.0.0

Usage

WIP

Channel

Inherit tcpchan.core.chan.Channel and implements data_received callback.

from tcpchan.core.chan import Channel

class CustomChannel(Channel):
    def data_received(self, data):
        # Do stuff upon data reception

Connection

Create ServerConnection or ClientConnection instance upon connection establishment in server/client. And pass the channel factory to the Connection.

Server connection creation
from tcpchan.core.conn import ServerConnection

conn = ServerConnection(lambda: CustomChannel())
Client connection creation
from tcpchan.core.conn import ClientConnection

conn = ClientConnection(lambda: CustomChannel())

Events

from tcpchan.core import (
    HandshakeSuccess, DataTransmit,
    ChannelCreated, ChannelClosed
)

Asyncio

TCPChan provides an Asyncio-based protocol implementation so that one can easily integrate TCPChan in their Asyncio applications.

For server-side application, TCPChanServerProtocol can be used, likewise, for client-side application, TCPChanClientProtocol can be used.

import asyncio
from tcpchan.aio import TCPChanServerProtocol
from tcpchan.core.chan import Channel


class CustomChannel(Channel):
    def data_received(self, data):
        # Do stuff upon data reception
        ...


class MyProtocol(TCPChanServerProtocol):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._channels = {}  # Mapping for channels

    def handshake_success(self):
        # Do something on handshake success
        ...

    def handshake_failed(self, reason):
        # Do something on handshake failure
        ...

    def channel_created(self, channel):
        # Do something on channel creation
        self._channels[channel.channel_id] = channel

    def channel_closed(self, channel_id):
        # Do something when a channel is closed
        del self._channels[channel_id]


loop = asyncio.get_event_loop()


# To initialize `Protocol`, channel factory function is required.
server = await loop.create_server(
    lambda: MyProtocol(lambda: CustomChannel()),
    host="localhost",
    port=9487,
    start_serving=True,
)
loop.run_forever()

LICENSE

BSD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tcpchan-0.1.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tcpchan-0.1.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file tcpchan-0.1.1.tar.gz.

File metadata

  • Download URL: tcpchan-0.1.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.4 Darwin/19.6.0

File hashes

Hashes for tcpchan-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3fdb044490a09e095db8bf9f3b1ff2351f39f7568914dd11fa70828d395c87ce
MD5 902b0818cc7ae1d4ce726acae5f164af
BLAKE2b-256 f396c340b4eb6941342451566c6a9470b689ce1fded11e83e65f41ae8f1d661a

See more details on using hashes here.

File details

Details for the file tcpchan-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tcpchan-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.4 Darwin/19.6.0

File hashes

Hashes for tcpchan-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30346d065a7fe0c3dd0339fd42c236a7af0b5286fc06eedd67ee7c9d5a184a67
MD5 8dd1ea19e962f53185ceb1b30558a53f
BLAKE2b-256 5bd454555e61dc5b57d1a001bd93598d303dbfad55a758ebcc0ab7ac54def4b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page