tcpchan是一个TCP(de)多路复用器
tcpchan的Python项目详细描述
茶茶
TCPChan是一个TCP连接多路复用库,支持在单个TCP连接中使用多个通道。TCPChan可以通过消除连接设置的开销来提高短期连接的效率,特别是在高延迟链路(例如跨大陆链路)中。在
该库的核心部分与I/O库分离,因此可以携带自己的I/O库。为了方便起见,我们提供了一个基于Asyncio的协议实现,以便于与Asyncio应用程序集成。在
警告:TCPChan是为了好玩和教育目的而构建的,它没有经过充分测试,也没有被广泛部署。使用它的风险由你自己承担。在
指南
在制品
安装
通过Pip安装
pip install tcpchan
从GitHub安装最新版本
^{pr2}$
依赖性
- python>;=3.7
- fpack>;=1.0.0
用法
在制品
频道
继承tcpchan.core.chan.Channel
并实现data_received
回调。在
fromtcpchan.core.chanimportChannelclassCustomChannel(Channel):defdata_received(self,data):# Do stuff upon data reception
连接
在服务器/客户机中建立连接后,创建ServerConnection
或{
创建服务器连接
fromtcpchan.core.connimportServerConnectionconn=ServerConnection(lambda:CustomChannel())
创建客户端连接
fromtcpchan.core.connimportClientConnectionconn=ClientConnection(lambda:CustomChannel())
事件
fromtcpchan.coreimport(HandshakeSuccess,DataTransmit,ChannelCreated,ChannelClosed)
异步
TCPChan提供了一个基于异步的协议实现,因此可以轻松地将TCPChan集成到异步应用程序中。在
对于服务器端应用程序,TCPChanServerProtocol
可以使用,同样,对于客户端应用程序,TCPChanClientProtocol
也可以使用。在
importasynciofromtcpchan.aioimportTCPChanServerProtocolfromtcpchan.core.chanimportChannelclassCustomChannel(Channel):defdata_received(self,data):# Do stuff upon data reception...classMyProtocol(TCPChanServerProtocol):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)self._channels={}# Mapping for channelsdefhandshake_success(self):# Do something on handshake success...defhandshake_failed(self,reason):# Do something on handshake failure...defchannel_created(self,channel):# Do something on channel creationself._channels[channel.channel_id]=channeldefchannel_closed(self,channel_id):# Do something when a channel is closeddelself._channels[channel_id]loop=asyncio.get_event_loop()# To initialize `Protocol`, channel factory function is required.server=awaitloop.create_server(lambda:MyProtocol(lambda:CustomChannel()),host="localhost",port=9487,start_serving=True,)loop.run_forever()
许可证
BSD公司
- 项目
标签: