forked from jimkeir/EDProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.py
More file actions
73 lines (60 loc) · 2.03 KB
/
wrapper.py
File metadata and controls
73 lines (60 loc) · 2.03 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
72
73
import socket, select
import threading
import logging
class SocketWrapper(object):
def __init__(self, sock):
self._log = logging.getLogger("com.fussyware.edproxy")
self._sock = sock
self._sock.settimeout(60)
self._lock = threading.Lock()
self._closed = False
self._frame = False
def close(self):
if not self._closed:
self._closed = True
try:
self._sock.shutdown(socket.SHUT_RDWR)
except:
pass
try:
self._sock.close()
except:
pass
def write(self, buf):
try:
self._lock.acquire()
self._sock.sendall(buf)
finally:
self._lock.release()
def read(self, bytes_read = 4096):
if self._closed or bytes_read == 0:
self._log.debug("closed [%s], bytes [%d]" % (str(self._closed), bytes_read))
return ""
try:
rr = []
while not rr and not self._closed:
if self._frame:
rr, _, _ = select.select([self._sock], [], [], 0.250)
if not rr:
self._frame = False
self._log.debug("Framing is done so send blank")
return ""
else:
rr, _, _ = select.select([self._sock], [], [], 2)
except:
self.close()
if self._closed:
self._log.debug("Socket has been closed")
return ""
try:
buf = self._sock.recv(bytes_read)
if len(buf) < bytes_read:
self._frame = True
return buf
except socket.error as msg:
self.close()
self._log.debug("Socket error")
return ""
except socket.timeout as msg:
self._log.debug("Socket timeout")
return ""