-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·133 lines (125 loc) · 4.17 KB
/
server.py
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
#!/usr/bin/env python
import sys, socket, select, random
try:
import cPickle as pickle
except:
import pickle
MAXCL=5
MAXS=4096
pW=76
pH=48
sW=1024
sH=768
if len(sys.argv)!=2:
sys.exit(sys.argv[0][2:]+' port')
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', int(sys.argv[1])))
sock.listen(MAXCL)
sock.setblocking(0)
conn={}
epoll=select.epoll()
epoll.register(sock.fileno(), select.EPOLLIN)
random.seed()
nrP=0
players=[]
try:
while True:
ev=epoll.poll(1)
for n, e in ev:
if n==sock.fileno():
newsock, addr=sock.accept()
newsock.setblocking(0)
conn[newsock.fileno()]=newsock
epoll.register(newsock.fileno(), select.EPOLLIN)
nrP=nrP+1
rX=random.randint(pW/2, sW-pW/2)
rY=random.randint(pH/2, sH-pH/2)
rRt=random.randrange(0, 35, 10)
# a lil hardcoding here, will fix
if nrP == 1:
rX = 500
rY = 500
rRt = 10.0
l=[0, newsock.fileno(), rX, rY, float(rRt), 'img'+str(nrP)+'.png']
s = pickle.dumps(l)
newsock.sendall(s)
if len(players)==0:
l[5]='img1.png'
players.append(l)
else:
for i in range(0, len(players)):
print 'i= '+str(i)
if not players[i]:
l[5]='img'+str(i)+'.png'
players[i]=l
#print l
break
else:
players.append(l)
#s=pickle.dumps(players)
#newsock.sendall(s)
#tip0, ID1, x2, y3, rot4e, path5
p=''
p=pickle.dumps(l)
for k in conn.keys():
if k!=n:
try:
conn[k].sendall(p)
except:
pass
elif e & select.EPOLLIN:
try:
s=conn[n].recv(MAXS)
except:
pass
if s:
#broadcast
if s[:4]!='QUIT':
for k in conn.keys():
if k!=n:
try:
conn[k].sendall(s)
except:
pass
else:
print 'exit: '+s
l=int(s[-1:])
for i in range(len(players)):
if players[i][1]==l:
nrP=nrP-1
players.remove(players[i])
l=[-1, l]
p=''
p=pickle.dumps(l)
for k in conn.keys():
if k!=n:
try:
conn[k].sendall(p)
except:
pass
epoll.unregister(n)
conn[n].close()
del conn[n]
# TODO
# if e & select.EPOLLHUP:
# for i in range(len(players)):
# if players[i][1]==
# nrP=nrP-1
# print nrP
# l=[-1, n]
# p=''
# p=pickle.dumps(l)
# for k in conn.keys():
# if k!=n:
# try:
# conn[k].sendall(p)
# except:
# pass
# epoll.unregister(n)
# conn[n].close()
# del conn[n]
finally:
epoll.unregister(sock.fileno())
epoll.close()
sock.close()