-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockwrap.py
More file actions
71 lines (58 loc) · 2 KB
/
sockwrap.py
File metadata and controls
71 lines (58 loc) · 2 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Light UDP socket wrapper.
"""
# Copyright (C) 2008 James Fargher
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
import select
import socket
def create_server_socket(address, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
sock.bind((address, port))
return sock
def create_client_socket():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
return sock
class SocketReadDispatch(object):
"""Dispatch packet read from socket"""
def __init__(self, dispatcher):
self.dispatcher = dispatcher
def dispatch(self, sock):
data, address = sock.recvfrom(4096)
self.dispatcher.dispatch(data, address)
class SocketWriteQueue(object):
"""Queue packets to be written to a socket"""
def __init__(self):
self.writequeue = []
def push(self, data, address):
self.writequeue.append((data, address))
def empty(self):
return len(self.writequeue) < 1
def write(self, sock):
try:
cmd = self.writequeue.pop(0)
sock.sendto(cmd[0], cmd[1])
except IndexError:
pass
class SocketServer(object):
"""Handle socket and dispatch packets"""
def __init__(self, dispatcher, queue, sock):
self.dispatcher = dispatcher
self.queue = queue
self.socks = (sock,)
def select(self):
result = select.select(self.socks, self.socks, (), 0)
sock_read, sock_write, sock_error = result
for sock in sock_read:
self.dispatcher.dispatch(sock)
for sock in sock_write:
self.queue.write(sock)
def update(self):
self.select()
while not self.queue.empty():
self.select()