-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.py
92 lines (63 loc) · 2.62 KB
/
tcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import socket, untwisted
from twisted.internet import protocol, reactor, tcp
from untwisted import promise
def connect(host, port, timeout=30, bindAddress=None):
# Avoid TypeError: Error when calling the metaclass bases, a new-style class
# can't have only classic bases
class result(tcp.Connector, object):
class __metaclass__(type):
def __call__(ctx):
transport = promise.promise()
# Extend protocol.ClientFactory for .startedConnecting()
@untwisted.call
class factory(protocol.ClientFactory):
class protocol:
def __init__(ctx):
ctx.connectionLost = promise.promise()
ctx.dataReceived = promise.sequence()
# .dataReceived() must return falsy: SelectReactor._doReadOrWrite()
__call__ = ctx.dataReceived.__call__
@untwisted.partial(setattr, ctx.dataReceived, '__call__')
def _(*args, **kwds):
__call__(*args, **kwds)
makeConnection = transport
type.__call__(ctx, host, port, factory, timeout, bindAddress, reactor).connect()
return transport
class _makeTransport(tcp.Client):
class __metaclass__(type):
__call__ = lambda ctx: type.__call__(ctx, ctx.ctx.host, ctx.ctx.port, ctx.ctx.bindAddress, ctx.ctx, ctx.ctx.reactor)
__get__ = untwisted.ctxual
def close(ctx):
ctx.loseConnection()
return ctx.protocol.connectionLost
return result
def listen(port, interface=''):
transport = promise.sequence()
# Extend protocol.Factory for .doStart()
@untwisted.call
class factory(protocol.Factory):
class protocol:
def __init__(ctx):
ctx.connectionLost = promise.promise()
ctx.dataReceived = promise.sequence()
# .dataReceived() must return falsy: SelectReactor._doReadOrWrite()
__call__ = ctx.dataReceived.__call__
@untwisted.partial(setattr, ctx.dataReceived, '__call__')
def _(*args, **kwds):
__call__(*args, **kwds)
makeConnection = transport
@untwisted.call
class _(tcp.Port):
class __metaclass__(type):
def __call__(ctx):
try:
type.__call__(ctx, port, factory, interface=interface).startListening()
# tcp.Connector calls socket.getservbyname() but tcp.Port doesn't : (
except TypeError:
nstPort = socket.getservbyname(port, 'tcp')
type.__call__(ctx, nstPort, factory, interface=interface).startListening()
class transport(tcp.Server):
def close(ctx):
ctx.loseConnection()
return ctx.protocol.connectionLost
return transport.shift