-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrserver.py
executable file
·69 lines (62 loc) · 1.83 KB
/
rserver.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
#coding=utf-8
from logger import logger as rlogger
from twisted.inernet.protocol import DatagramProtocol
from twisted.inernet import reactor
import struct
client_pc={}
client_dev={}
"""
get the package id
"""
def get_syn_id(r_data):
if r_data!=None && len(r_data)>14:
head =struct.unpack("<H8sI",r_data[:14])
return (head[0],head[2])
else:
return (0,0)
"""
the protocol dowith the package
"""
class tudpProtocol(DatagramProtocol):
def __init__(self):
pass
def datagramReceived(self, data, addr):
rlogger.message("recieve data from %s :%d"%(addr))
syn,dev_id = get_syn_id(data)
if syn!=0:
list t_data
t_data=data[:]
if syn==0x7F00:
t_data[0]=0xFF
client_dev[dev_id]=addr
rlogger.message("add dev %d"%(dev_id))
des = client_pc.get(dev_id)
if des!=None:
res_data = ''.join([chr(i) for i t_data])
self.transport.write(res_data,des)
if syn==0x7FF:
t_data[0]=0x00
client_pc[dev_id]=addr
rlogger.message("add pc %d"%(dev_id)
des = client_dev.get(dev_id)
if des!=None:
res_data = ''.join([chr(i) for i t_data])
self.transport.write(res_data,des)
else:
rlogger.message("recieve wrong package")
def run_udp(port):
reactor.listenUDP(port,tudpProtocol(),interface="0.0.0.0")
reactor.run()
pass
class client:
'''
The class contain the client paires
'''
def __init__ (self,host,port):
self.host = host
self.port = port
def get_socket (self):
return (self.host,self.port)
if __name__ == '__main__':
rlogger.debug("server run")
run_udp(1047)