-
Notifications
You must be signed in to change notification settings - Fork 35
/
videosocket.py
62 lines (48 loc) · 1.73 KB
/
videosocket.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
import socket
class videosocket:
'''A special type of socket to handle the sending and receiveing of fixed
size frame strings over ususal sockets
Size of a packet or whatever is assumed to be less than 100MB
'''
def __init__(self , sock=None):
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock= sock
def connect(self,host,port):
self.sock.connect((host,port))
def vsend(self, framestring):
totalsent = 0
metasent = 0
length =len(framestring)
lengthstr=str(length).zfill(8)
while metasent < 8 :
sent = self.sock.send(lengthstr[metasent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
metasent += sent
while totalsent < length :
sent = self.sock.send(framestring[totalsent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
totalsent += sent
def vreceive(self):
totrec=0
metarec=0
msgArray = []
metaArray = []
while metarec < 8:
chunk = self.sock.recv(8 - metarec)
if chunk == '':
raise RuntimeError("Socket connection broken")
metaArray.append(chunk)
metarec += len(chunk)
lengthstr= ''.join(metaArray)
length=int(lengthstr)
while totrec<length :
chunk = self.sock.recv(length - totrec)
if chunk == '':
raise RuntimeError("Socket connection broken")
msgArray.append(chunk)
totrec += len(chunk)
return ''.join(msgArray)