-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrudpconnexion.py
More file actions
68 lines (59 loc) · 2.27 KB
/
rudpconnexion.py
File metadata and controls
68 lines (59 loc) · 2.27 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
from time import time
from constantsrudp import *
from socket import *
from rudppacket import *
class rudpConnection():
def __init__(self, destAddr, isClient):
self.destAddr = destAddr
self.wait = SYN_ACK if isClient else SYN
self.pktId = 0
if not isClient:
self.accept = [SYN] #[SYN, DAT, FIN]
self.time = 0
self.data = ''
def checkTime(self, time):
if time - self.time > END_WAIT:
return False
return True
def printConnection(self):
print '[RUDP Connection]'
print '\tdestAddr:', self.destAddr
print '\tpktId :', self.pktId
print '\twait :', self.wait
try:
print '\taccept :', self.accep
print '\ttime :', self.time
print '\tdata :', self.data
except:
print 'NOT VALID'
class client(object):
def __init__(self, address, port):
self.socketip=address
self.socketport=port
self.skt = socket(AF_INET, SOCK_DGRAM) #UDP
self.skt.connect((address,port))
#self.skt.bind(('', srcPort)) #used for recv
def connect(self):
destIP = self.socketip
destPort = self.socketport
self.conn = rudpConnection(None, True)
self.conn.destAddr = (destIP, destPort)
self.skt.setblocking(0)
#self.skt.settimeout(RTO)
for i in xrange(MAX_RESND):
try:
self.skt.sendto(encode(rudpPacket(SYN, self.conn.pktId)), self.conn.destAddr)
print rudpPacket(SYN, self.conn.pktId) ## For debugging
while True:
recvData, addr = self.skt.recvfrom(MAX_DATA)
try:
recvPkt = decode(recvData)
sendPkt = rudpProcessSwitch[recvPkt['pktType']](recvPkt, self.conn)
return True
except WRONG_PKT, KeyError: continue
except timeout: continue
except Exception as e :
print e.message
print '[Handshaking] unexpected error occurs\n' ## For debugging
return False
raise MAX_RESND_FAIL()