Advertisement
Bkmz

Untitled

Apr 10th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from copy import deepcopy, copy
  3. from pdb import set_trace as st
  4.  
  5. from os import fork, pipe, read, write, close, fdopen
  6. from sys import exit
  7.  
  8. from socket import *
  9. from select import poll, POLLIN, POLLPRI, POLLHUP, POLLERR, POLLOUT
  10.  
  11. from select import error as select_error
  12.  
  13. import Queue
  14.  
  15. from time import sleep
  16.  
  17. import signal
  18.  
  19. from game import Game, NetException
  20.  
  21. import sys
  22. reload(sys)
  23. sys.setdefaultencoding('utf8')
  24.  
  25. def input_edit(str):
  26.     return str.strip()
  27.  
  28. def sig_handler(signal, frame):
  29.     global message_queues, server, net_loop
  30.  
  31.     server.close()
  32.  
  33. signal.signal(signal.SIGINT, sig_handler)
  34.  
  35. HOST = None
  36. PORT = 4500
  37. # Keep up with the queues of outgoing messages
  38. message_queues = {}
  39.  
  40. # flag to main network loop
  41. net_loop = 1
  42.  
  43. index = 0
  44.  
  45. # initiate game obj
  46. Game = Game()
  47.  
  48. # poll flags
  49. READ_ONLY = POLLIN | POLLPRI | POLLHUP | POLLERR
  50. READ_WRITE = READ_ONLY | POLLOUT
  51.  
  52. for res in  getaddrinfo(HOST, PORT, AF_INET, SOCK_STREAM):
  53.     af, socktype, proto, canonname, sa = res
  54.  
  55.     # create server socket and binding it to address
  56.     server = socket(af, socktype, proto)
  57.     server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # TIME_WAIT
  58.     server.bind(sa)
  59.     server.listen(0)
  60.     server.setblocking(0)
  61.  
  62.     # Map file descriptors to socket objects
  63.     fd_to_socket = { server.fileno(): server,
  64.                    }
  65.  
  66. poller = poll()
  67. poller.register(server, READ_ONLY)
  68.  
  69. while net_loop:
  70.     try:
  71.         events = poller.poll(250)
  72.     except select_error: # usually cause by Ctrl+C SIGINT Signal
  73.         print "Exiting main network loop"
  74.         break
  75.  
  76.     # check data to bw written
  77.     for key,vl in fd_to_socket.iteritems():
  78.         poller.modify(vl, READ_WRITE)
  79.  
  80.     for fd, flag in events:
  81.         s  = fd_to_socket[fd]
  82.  
  83.         # some checks
  84.         if flag & (POLLIN | POLLPRI):
  85.             if s is server:                         # new connection
  86.                 connection, client_addr = s.accept()
  87.                 print "Connected ", client_addr
  88.                 connection.setblocking(0)
  89.                 fd_to_socket[ connection.fileno() ] = connection
  90.                 poller.register(connection, READ_ONLY)
  91.                 message_queues[connection] = Queue.Queue()
  92.  
  93.                 Game.new_connection(connection)
  94.  
  95.             else:                                   # reading new data
  96.                 data = s.recv(1024)
  97.                 if data:
  98.                     data = input_edit(data)
  99.                     Game.new_data(s, data)
  100.                     print "From %s received: %s" % (s.getpeername(), data)
  101.                     poller.modify(s, READ_WRITE)
  102.                 else:                               # empty string, client disconnecting
  103.                     print "Closing connection %s after reading no data"
  104.                     poller.unregister(s)
  105.                     s.close()
  106.  
  107.                     del message_queues[s]
  108.                     del fd_to_socket[fd]
  109.         elif flag & POLLHUP:                        # client dirty hangup connection
  110.             print "Clossing connection from %s after HANGUP" % s.getpeername()
  111.             poller.unregister(s)
  112.             s.close()
  113.  
  114.             # del message_queues
  115.             del message_queues[s]
  116.             del fd_to_socket[fd]
  117.  
  118.         elif flag & POLLOUT:                        # ready send data to client
  119.             try:
  120.                 # st()
  121.                 # next_msg = message_queues[s].get_nowait()
  122.                 next_msg = Game.send_to_user(s)
  123.  
  124.             except Queue.Empty:
  125.                 # no writable data, so stop monitoring this connection for write
  126.                 poller.modify(s, READ_ONLY)
  127.                 # print "Nothing to send to %s" % str(s.getpeername())
  128.             except NetException:
  129.                 poller.modify(s, READ_ONLY)
  130.  
  131.             else:
  132.                 print "Send '%s' to %s" % (next_msg, s.getpeername())
  133.                 s.send(next_msg)
  134.  
  135.         elif flag & POLLERR:                        # closing connection on some error
  136.             print "Clossing connection from %s after Exception" % s.getpeername()
  137.             poller.unregister(s)
  138.             s.close()
  139.  
  140.             del message_queues[s]
  141.             del fd_to_socket[fd]
  142.  
  143.  
  144.     # check data to bw written
  145.     if index % 2 == 0:
  146.         for key,vl in fd_to_socket.iteritems():
  147.             poller.modify(vl, READ_WRITE)
  148.  
  149.  
  150.     # if index == 10:
  151.     #     break
  152.  
  153.     index += 1
  154.  
  155. server.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement