/usr/share/pyshared/Codeville/RawServer.py is in codeville 0.8.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | # Written by Bram Cohen
# see LICENSE.txt for license information
from bisect import insort
import socket
from cStringIO import StringIO
from traceback import print_exc
from errno import EWOULDBLOCK, EINTR
try:
from select import poll, error, POLLIN, POLLOUT, POLLERR, POLLHUP
timemult = 1000
except ImportError:
from selectpoll import poll, error, POLLIN, POLLOUT, POLLERR, POLLHUP
timemult = 1
from threading import Thread, Event
from time import time, sleep
import sys
from random import randrange
from traceback import print_stack
all = POLLIN | POLLOUT
class SingleSocket:
def __init__(self, raw_server, sock, handler):
self.raw_server = raw_server
self.socket = sock
self.handler = handler
self.buffer = []
self.last_hit = time()
self.fileno = sock.fileno()
self.connected = False
def get_ip(self):
try:
return self.socket.getpeername()[0]
except socket.error:
return 'no connection'
def close(self, unregister=True):
# print 'RawServer close: ' + str(self.fileno)
# print_stack()
sock = self.socket
self.socket = None
self.buffer = []
if unregister:
self.raw_server.unregister(self.fileno)
sock.close()
def shutdown(self, val):
self.socket.shutdown(val)
def is_flushed(self):
return len(self.buffer) == 0
def write(self, s):
assert self.socket is not None
self.buffer.append(s)
if self.connected and len(self.buffer) == 1:
self.try_write()
return not len(self.buffer)
def try_write(self):
if self.connected:
try:
while self.buffer != []:
amount = self.socket.send(self.buffer[0])
if amount != len(self.buffer[0]):
if amount != 0:
self.buffer[0] = self.buffer[0][amount:]
break
del self.buffer[0]
except socket.error, e:
code, msg = e
if code != EWOULDBLOCK:
self.raw_server.dead_from_write.append(self)
return
if self.buffer == []:
self.raw_server.poll.register(self.socket, POLLIN)
else:
self.raw_server.poll.register(self.socket, all)
class RawServer:
def __init__(self, doneflag, timeout_check_interval, timeout):
self.timeout_check_interval = timeout_check_interval
self.timeout = timeout
self.poll = poll()
# {socket: SingleSocket}
self.single_sockets = {}
self.dead_from_write = []
self.doneflag = doneflag
self.funcs = []
self.externally_added = []
self.server = None
self.add_task(self.scan_for_timeouts, timeout_check_interval)
def add_task(self, func, delay):
insort(self.funcs, (time() + delay, func))
def external_add_task(self, func, delay = 0):
self.externally_added.append((func, delay))
def scan_for_timeouts(self):
self.add_task(self.scan_for_timeouts, self.timeout_check_interval)
t = time() - self.timeout
tokill = []
for s in self.single_sockets.values():
if s.last_hit < t:
tokill.append(s)
for k in tokill:
if k.socket is not None:
self._close_socket(k)
def bind(self, port, bind = '', reuse = False):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if reuse:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setblocking(0)
server.bind((bind, port))
server.listen(5)
self.poll.register(server, POLLIN)
self.server = server
def start_connection(self, dns, handler = None):
if handler is None:
handler = self.handler
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
try:
sock.connect_ex(dns)
except socket.error:
raise
except Exception, e:
raise socket.error(str(e))
self.poll.register(sock, POLLIN|POLLOUT|POLLERR)
s = SingleSocket(self, sock, handler)
self.single_sockets[sock.fileno()] = s
return s
def handle_events(self, events):
for sock, event in events:
if self.server is not None and sock == self.server.fileno():
if event & (POLLHUP | POLLERR) != 0:
self.poll.unregister(self.server)
self.server.close()
self.errorfunc('lost server socket')
else:
try:
newsock, addr = self.server.accept()
newsock.setblocking(0)
nss = SingleSocket(self, newsock, self.handler)
self.single_sockets[newsock.fileno()] = nss
self.poll.register(newsock, POLLIN)
self.handler.external_connection_made(nss)
except socket.error:
sleep(1)
else:
s = self.single_sockets.get(sock)
if s is None:
continue
if (event & (POLLHUP|POLLERR)) != 0:
if s.connected:
self._close_socket(s, msg='Remote end closed connection')
else:
self._close_socket(s, msg='Connection refused')
continue
s.connected = True
if (event & POLLIN) != 0:
try:
s.last_hit = time()
data = s.socket.recv(100000)
if data == '':
self._close_socket(s, msg='Remote end closed connection')
else:
s.handler.data_came_in(s, data)
except socket.error, e:
code, msg = e
if code != EWOULDBLOCK:
self._close_socket(s, msg=msg)
continue
if (event & POLLOUT) != 0 and s.socket is not None and not s.is_flushed():
s.try_write()
if s.is_flushed():
s.handler.connection_flushed(s)
def pop_external(self):
try:
while True:
(a, b) = self.externally_added.pop()
self.add_task(a, b)
except IndexError:
pass
def listen_forever(self, handler):
self.handler = handler
try:
while not self.doneflag.isSet():
try:
self.pop_external()
if len(self.funcs) == 0:
period = 2 ** 30
else:
period = self.funcs[0][0] - time()
if period < 0:
period = 0
events = self.poll.poll(period * timemult)
if self.doneflag.isSet():
return
while len(self.funcs) > 0 and self.funcs[0][0] <= time():
garbage, func = self.funcs[0]
del self.funcs[0]
try:
func()
except KeyboardInterrupt:
print_exc()
return
self._close_dead()
self.handle_events(events)
if self.doneflag.isSet():
return
self._close_dead()
except error:
if self.doneflag.isSet():
return
#except KeyboardInterrupt:
# print_exc()
# return
# except:
# data = StringIO()
# print_exc(file = data)
# self.errorfunc(data.getvalue())
finally:
# for ss in self.single_sockets.values():
# ss.close()
if self.server is not None:
self.poll.unregister(self.server)
self.server.close()
def unregister(self, fd):
del self.single_sockets[fd]
self.poll.unregister(fd)
return
def _close_dead(self):
while len(self.dead_from_write) > 0:
old = self.dead_from_write
self.dead_from_write = []
for s in old:
if s.socket is not None:
self._close_socket(s)
def _close_socket(self, s, msg=None):
sock = s.socket.fileno()
self.poll.unregister(sock)
del self.single_sockets[sock]
s.socket.close()
s.socket = None
s.handler.connection_lost(s, msg)
# everything below is for testing
class DummyHandler:
def __init__(self):
self.external_made = []
self.data_in = []
self.lost = []
def external_connection_made(self, s):
self.external_made.append(s)
def data_came_in(self, s, data):
self.data_in.append((s, data))
def connection_lost(self, s, msg):
self.lost.append(s)
def connection_flushed(self, s):
pass
def sl(rs, handler, port):
rs.bind(port)
Thread(target = rs.listen_forever, args = [handler]).start()
def loop(rs):
x = []
def r(rs = rs, x = x):
rs.add_task(x[0], .1)
x.append(r)
rs.add_task(r, .1)
beginport = 5000 + randrange(10000)
def test_starting_side_close():
try:
fa = Event()
fb = Event()
da = DummyHandler()
sa = RawServer(fa, 100, 100)
loop(sa)
sl(sa, da, beginport)
db = DummyHandler()
sb = RawServer(fb, 100, 100)
loop(sb)
sl(sb, db, beginport + 1)
sleep(.5)
ca = sa.start_connection(('127.0.0.1', beginport + 1))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert len(db.external_made) == 1
cb = db.external_made[0]
del db.external_made[:]
assert db.data_in == []
assert db.lost == []
ca.write('aaa')
cb.write('bbb')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'bbb')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'aaa')]
del db.data_in[:]
assert db.lost == []
ca.write('ccc')
cb.write('ddd')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'ddd')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'ccc')]
del db.data_in[:]
assert db.lost == []
ca.close()
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert db.external_made == []
assert db.data_in == []
assert db.lost == [cb]
del db.lost[:]
finally:
fa.set()
fb.set()
def test_receiving_side_close():
try:
da = DummyHandler()
fa = Event()
sa = RawServer(fa, 100, 100)
loop(sa)
sl(sa, da, beginport + 2)
db = DummyHandler()
fb = Event()
sb = RawServer(fb, 100, 100)
loop(sb)
sl(sb, db, beginport + 3)
sleep(.5)
ca = sa.start_connection(('127.0.0.1', beginport + 3))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert len(db.external_made) == 1
cb = db.external_made[0]
del db.external_made[:]
assert db.data_in == []
assert db.lost == []
ca.write('aaa')
cb.write('bbb')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'bbb')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'aaa')]
del db.data_in[:]
assert db.lost == []
ca.write('ccc')
cb.write('ddd')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'ddd')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'ccc')]
del db.data_in[:]
assert db.lost == []
cb.close()
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == [ca]
del da.lost[:]
assert db.external_made == []
assert db.data_in == []
assert db.lost == []
finally:
fa.set()
fb.set()
def test_connection_refused():
try:
da = DummyHandler()
fa = Event()
sa = RawServer(fa, 100, 100)
loop(sa)
sl(sa, da, beginport + 6)
sleep(.5)
ca = sa.start_connection(('127.0.0.1', beginport + 15))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == [ca]
del da.lost[:]
finally:
fa.set()
def test_both_close():
try:
da = DummyHandler()
fa = Event()
sa = RawServer(fa, 100, 100)
loop(sa)
sl(sa, da, beginport + 4)
sleep(1)
db = DummyHandler()
fb = Event()
sb = RawServer(fb, 100, 100)
loop(sb)
sl(sb, db, beginport + 5)
sleep(.5)
ca = sa.start_connection(('127.0.0.1', beginport + 5))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert len(db.external_made) == 1
cb = db.external_made[0]
del db.external_made[:]
assert db.data_in == []
assert db.lost == []
ca.write('aaa')
cb.write('bbb')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'bbb')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'aaa')]
del db.data_in[:]
assert db.lost == []
ca.write('ccc')
cb.write('ddd')
sleep(1)
assert da.external_made == []
assert da.data_in == [(ca, 'ddd')]
del da.data_in[:]
assert da.lost == []
assert db.external_made == []
assert db.data_in == [(cb, 'ccc')]
del db.data_in[:]
assert db.lost == []
sa.unregister(ca.fileno)
sb.unregister(cb.fileno)
ca.close(unregister=False)
cb.close(unregister=False)
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert db.external_made == []
assert db.data_in == []
assert db.lost == []
finally:
fa.set()
fb.set()
def test_normal():
l = []
f = Event()
s = RawServer(f, 100, 100)
loop(s)
sl(s, DummyHandler(), beginport + 7)
s.add_task(lambda l = l: l.append('b'), 2)
s.add_task(lambda l = l: l.append('a'), 1)
s.add_task(lambda l = l: l.append('d'), 4)
sleep(1.5)
s.add_task(lambda l = l: l.append('c'), 1.5)
sleep(3)
assert l == ['a', 'b', 'c', 'd']
f.set()
def test_catch_exception():
l = []
f = Event()
s = RawServer(f, 100, 100, False)
loop(s)
sl(s, DummyHandler(), beginport + 9)
s.add_task(lambda l = l: l.append('b'), 2)
s.add_task(lambda: 4/0, 1)
sleep(3)
assert l == ['b']
f.set()
def test_closes_if_not_hit():
try:
da = DummyHandler()
fa = Event()
sa = RawServer(fa, 2, 2)
loop(sa)
sl(sa, da, beginport + 14)
sleep(1)
db = DummyHandler()
fb = Event()
sb = RawServer(fb, 100, 100)
loop(sb)
sl(sb, db, beginport + 13)
sleep(.5)
sa.start_connection(('127.0.0.1', beginport + 13))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert len(db.external_made) == 1
del db.external_made[:]
assert db.data_in == []
assert db.lost == []
sleep(3.1)
assert len(da.lost) == 1
assert len(db.lost) == 1
finally:
fa.set()
fb.set()
def test_does_not_close_if_hit():
try:
fa = Event()
fb = Event()
da = DummyHandler()
sa = RawServer(fa, 2, 2)
loop(sa)
sl(sa, da, beginport + 12)
sleep(1)
db = DummyHandler()
sb = RawServer(fb, 100, 100)
loop(sb)
sl(sb, db, beginport + 13)
sleep(.5)
sa.start_connection(('127.0.0.1', beginport + 13))
sleep(1)
assert da.external_made == []
assert da.data_in == []
assert da.lost == []
assert len(db.external_made) == 1
cb = db.external_made[0]
del db.external_made[:]
assert db.data_in == []
assert db.lost == []
cb.write('bbb')
sleep(.5)
assert da.lost == []
assert db.lost == []
finally:
fa.set()
fb.set()
|