-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathuds.py
More file actions
53 lines (39 loc) · 1.81 KB
/
uds.py
File metadata and controls
53 lines (39 loc) · 1.81 KB
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
'''
@author: hvishwanath | harish.shastry@gmail.com
'''
import msgpackrpc.transport
from tornado.netutil import bind_unix_socket
from tornado import tcpserver
from tornado.iostream import IOStream
# Much of the implementation will be same as that of tcp module
# Changes required for unix domain socket support are done in this module
# Rest will be automatically used from tcp
# Create namespace equals
BaseSocket = msgpackrpc.transport.tcp.BaseSocket
ClientSocket = msgpackrpc.transport.tcp.ClientSocket
ClientTransport = msgpackrpc.transport.tcp.ClientTransport
ServerSocket = msgpackrpc.transport.tcp.ServerSocket
ServerTransport = msgpackrpc.transport.tcp.ServerTransport
class UDSServer(tcpserver.TCPServer):
"""Define a Unix domain socket server.
Instead of binding to TCP/IP socket, bind to UDS socket and listen"""
def __init__(self, io_loop=None, ssl_options=None):
tcpserver.TCPServer.__init__(self, io_loop=io_loop, ssl_options=ssl_options)
def listen(self, port, address=""):
"""Bind to a unix domain socket and add to self.
Note that port in our case actually contains the uds file name"""
# Create a Unix domain socket and bind
socket = bind_unix_socket(port)
# Add to self
self.add_socket(socket)
class MessagePackServer(UDSServer):
"""The MessagePackServer inherits from UDSServer
instead of tornado's TCP Server"""
def __init__(self, transport, io_loop=None, encodings=None):
self._transport = transport
self._encodings = encodings
UDSServer.__init__(self, io_loop=io_loop)
def handle_stream(self, stream, address):
ServerSocket(stream, self._transport, self._encodings)
#Monkey patch the MessagePackServer
msgpackrpc.transport.tcp.MessagePackServer = MessagePackServer