Source code for synapse.lib.link

import socket
import asyncio
import logging
import collections

import cryptography.x509 as c_x509
import cryptography.hazmat.primitives.hashes as c_hashes

logger = logging.getLogger(__name__)

import synapse.exc as s_exc
import synapse.common as s_common

import synapse.lib.base as s_base
import synapse.lib.const as s_const
import synapse.lib.msgpack as s_msgpack

readsize = 10 * s_const.megabyte

[docs] async def connect(host, port, ssl=None, hostname=None, linkinfo=None): ''' Async connect and return a Link(). ''' info = {'host': host, 'port': port, 'ssl': ssl, 'hostname': hostname, 'tls': bool(ssl)} if linkinfo is not None: info.update(linkinfo) ssl = info.get('ssl') hostname = info.get('hostname') reader, writer = await asyncio.open_connection(host, port, ssl=ssl, server_hostname=hostname) return await Link.anit(reader, writer, info=info)
[docs] async def listen(host, port, onlink, ssl=None): ''' Listen on the given host/port and fire onlink(Link). Returns a server object that contains the listening sockets ''' async def onconn(reader, writer): info = {'tls': bool(ssl)} link = await Link.anit(reader, writer, info=info) link.schedCoro(onlink(link)) server = await asyncio.start_server(onconn, host=host, port=port, ssl=ssl) return server
[docs] async def unixlisten(path, onlink): ''' Start an PF_UNIX server listening on the given path. ''' info = {'path': path, 'unix': True} async def onconn(reader, writer): link = await Link.anit(reader, writer, info=info) link.schedCoro(onlink(link)) return await asyncio.start_unix_server(onconn, path=path)
[docs] async def unixconnect(path): ''' Connect to a PF_UNIX server listening on the given path. ''' reader, writer = await asyncio.open_unix_connection(path=path) info = {'path': path, 'unix': True} return await Link.anit(reader, writer, info=info)
[docs] async def linkfile(mode='wb'): ''' Connect a socketpair to a file-object and return (link, file). ''' sock0, sock1 = socket.socketpair() file1 = sock1.makefile(mode) sock1.close() reader, writer = await asyncio.open_connection(sock=sock0) link0 = await Link.anit(reader, writer, info={'unix': True}) return link0, file1
[docs] async def linksock(forceclose=False): ''' Connect a Link, socket pair. ''' sock0, sock1 = socket.socketpair() reader, writer = await asyncio.open_connection(sock=sock0) link0 = await Link.anit(reader, writer, info={'unix': True}, forceclose=forceclose) return link0, sock1
[docs] async def fromspawn(spawninfo): sock = spawninfo.get('sock') info = spawninfo.get('info', {}) info['spawn'] = True reader, writer = await asyncio.open_connection(sock=sock) return await Link.anit(reader, writer, info=info)