Permalink
Browse files

Better packetdatastream from @pcgod. Changed from using the protobuf …

…UDPTunnel message as that is not supposed to be used for voice data.
  • Loading branch information...
1 parent 0bf5cca commit d81ed02c483ce16a061982914b5034db702fd2e0 @smoak committed Oct 19, 2010
Showing with 196 additions and 234 deletions.
  1. +7 −5 MumbleConnection.py
  2. +1 −0 MumbleService.py
  3. +136 −191 PacketDataStream.py
  4. +52 −38 main.py
View
@@ -31,7 +31,8 @@
UDPMESSAGETYPE_UDPPING = 1
UDPMESSAGETYPE_UDPVOICESPEEX = 2
UDPMESSAGETYPE_UDPVOICECELTBETA = 3
-headerFormat=">HI"
+#headerFormat=">HI"
+headerFormat = b"!hi"
protocolVersion = (1 << 16) | (2 << 8) | (3 & 0xFF)
#supportedCodec = 0x8000000b
supportedCodec = -2147483637
@@ -84,8 +85,10 @@ def joinChannel(self, channelId):
log.error("Could not join channel")
def sendMessage(self, messageType, message):
- log.debug("Sending Message. Type: " + MessageType.StringLookupTable[messageType])
packet=struct.pack(headerFormat,messageType,message.ByteSize())+message.SerializeToString()
+ self.__sendRawPacket(packet)
+
+ def __sendRawPacket(self, packet):
self.sockLock.acquire()
while len(packet)>0:
sent=self.socket.send(packet)
@@ -98,9 +101,8 @@ def sendMessage(self, messageType, message):
def sendUdpTunnelMessage(self, data):
msgType = MessageType.UDPTunnel
- message = UDPTunnel()
- message.packet = data
- self.sendMessage(msgType, message)
+ packet=struct.pack(headerFormat,msgType,len(data))+data
+ self.__sendRawPacket(packet)
def findChannel(self, channel_id):
for c in self.channelList:
View
@@ -18,6 +18,7 @@ def getState(self):
return self.state
def serverSyncCompleted(self):
+ log.debug("Server synched")
self.serverSyncComplete = True
def setConnectionState(self, state):
View
@@ -1,191 +1,136 @@
-class PacketDataStream:
- def __init__(self, d):
- self.data = []
- self.ok = False
- self.offset = 0
- self.capacity = 0
- self.setBuffer(d)
-
- def append(self, d):
- length = len(d)
- if self.left() >= length:
- # array copy
- # Copy from d starting at 0 into data at offset going len(d)
- self.data[self.offset:self.offset] = d[0:]
- self.offset += length
- else:
- l = self.left()
- # fill array with 0
- self.data[self.offset:self.offset+l] = [0]
- self.offset += 1
- self.ok = False
-
- def append(self, v):
- if self.offset < len(self.data):
- self.data[self.offset] = v
- self.offset += 1
- else:
- self.ok = False
-
- def capacity(self):
- return len(self.data)
-
- def dataBlock(self, buf, length):
- if length <= self.left():
- # copy array
- buf[0:] = self.data[self.offset:length]
- self.offset += length
- return True
- else:
- self.ok = False
- return False
-
- def dataBlock(self, buf, startOffset, length):
- if length <= self.left():
- # Copy array
- buf[startOffset:]=self.data[self.offset:length]
- self.offset += length
- return True
- else:
- self.ok = False
- return False
-
- def isValid(self):
- return self.ok
-
- def left(self):
- return self.capacity - self.offset
-
- def next(self):
- if self.offset < self.capacity:
- tmp = self.data[self.offset] & 0xFF
- self.offset+=1
- return tmp
- else:
- self.ok = False
- return 0
-
- def readBool(self):
- return self.readLong() > 0
-
- def readDouble(self):
- if self.left() < 8:
- self.ok = False
- return 0
- i = self.next() | self.next() << 8 | self.next() << 16 | self.next() << 24 | self.next() << 32 | self.next() << 40 | self.next() << 48 | self.next() << 56
- return i
-
- def readFloat():
- if self.left() < 4:
- self.ok = False
- return 0
- i = self.next() | self.next() << 8 | self.next() << 16 | self.next << 24
- return float(i)
-
- def readLong(self):
- i = 0
- v = self.next()
- if v & 0x80 == 0:
- i = v & 0x7f
- elif v & 0xC0 == 0x80:
- i = (v & 0x3F) << 8 | self.next()
- elif v & 0xF0 == 0xF0:
- tmp = int(v & 0xFC)
- if tmp == 0xF0:
- i = self.next() << 24 | self.next() << 16 | self.next() << 8 | self.next()
- elif tmp == 0xF4:
- i = self.next << 56 | self.next() << 48 | self.next() << 40 | self.next() << 32 | self.next() << 24 | self.next() << 16 | self.next() << 8 | self.next()
- elif tmp == 0xF8:
- i = self.readLong()
- i = ~i
- elif tmp == 0xFC:
- i = v & 0x03
- i = ~i
- else:
- self.ok = False
- i = 0
- elif v & 0xF0 == 0xE0:
- i = (v & 0x0F) << 24 | self.next() << 16 | self.next() << 8 | self.next()
- elif v &0xE0 == 0xC0:
- i = (v & 0x1F) << 16 | self.next() << 8 | self.next()
-
- return i
-
- def rewind(self):
- self.offset = 0
-
- def setBuffer(self, d):
- self.data = d
- self.ok = True
- self.offset = 0
- self.capacity = len(d)
-
- def size(self):
- return self.offset
-
- def skip(self, length):
- if self.left() >= length:
- self.offset += length
- else:
- self.ok = False
-
- def writeBool(self, b):
- v = int(b == True)
- self.writeLong(v)
-
- def writeDouble(self, v):
- i = int(v)
- self.append(i & 0xFF)
-
-
- def writeFloat(self, v):
- i = int(v)
- self.append(i & 0xFF)
- self.append((i >> 8) & 0xFF)
- self.append((i >> 16) & 0xFF)
- self.append((i >> 24) & 0xFF)
-
- def writeLong(self, v):
- i = v
- if i & 0x8000000000000000L > 0 and ~i < 0x100000000L:
- # Signed number
- i = ~i
- if i < 0x3:
- # Shortcase for -1 to -4
- self.append(0xFC | i)
- return
- else:
- self.append(0xF8)
- if i < 0x80:
- # Need top bit clear
- self.append(i)
- elif i < 0x4000:
- self.append((i >> 8) | 0x80)
- self.append(i & 0xFF)
- elif i < 0x200000:
- self.append((i >> 16) | 0xC0)
- self.append((i >> 8) & 0xFF)
- self.append(i & 0xFF)
- elif i < 0x10000000:
- self.append((i >> 24) | 0xE0)
- self.append((i >> 16) & 0xFF)
- self.append((i >> 8) & 0xFF)
- self.append(i & 0xFF)
- elif i < 0x100000000L:
- self.append(0xF0)
- self.append((i >> 24) & 0xFF)
- self.append((i >> 16) & 0xFF)
- self.append((i >> 8) & 0xFF)
- self.append(i & 0xFF)
- else:
- self.append(0xF4)
- self.append((i >> 56) & 0xFF)
- self.append((i >> 48) & 0xFF)
- self.append((i >> 40) & 0xFF)
- self.append((i >> 32) & 0xFF)
- self.append((i >> 24) & 0xFF)
- self.append((i >> 16) & 0xFF)
- self.append((i >> 8) & 0xFF)
- self.append(i & 0xFF)
-
-
+#!/bin/env python
+
+class PacketDataStream(object):
+ def __init__(self, data):
+ self._data = list(data)
+ self._pos = 0
+ self._ok = True
+ self._capacity = len(data)
+
+ def valid(self):
+ return self._ok
+
+ def size(self):
+ return self._pos
+
+ def left(self):
+ return self._capacity - self._pos
+
+ def append(self, value):
+ if (self._pos < self._capacity):
+ self._data[self._pos] = chr(value)
+ self._pos += 1
+ else:
+ self._ok = False
+
+ def appendDataBlock(self, data):
+ length = len(data)
+ if length <= self.left():
+ self._data[self._pos:self._pos + length] = data
+ self._pos += length
+ else:
+ self._ok = False
+
+ def next(self):
+ if (self._pos < self._capacity):
+ ret = ord(self._data[self._pos])
+ self._pos += 1
+ return ret
+ else:
+ self._ok = False
+ return 0
+
+ def rewind(self):
+ self._pos = 0
+
+ def skip(self, length):
+ if length <= self.left():
+ self._pos += length
+ else:
+ self._ok = False
+
+ def getDataBlock(self, length):
+ if length <= self.left():
+ data = self._data[self._pos:self._pos + length]
+ self._pos += length
+ return data
+ else:
+ self._ok = False
+ return []
+
+ def getInt(self):
+ v = self.next()
+ i = 0
+
+ if (v & 0x80) == 0x00:
+ i = v & 0x7F
+ elif (v & 0xC0) == 0x80:
+ i = (v & 0x3F) << 8 | self.next()
+ elif (v & 0xF0) == 0xF0:
+ x = v & 0xFC
+ if x == 0xF0:
+ i = self.next() << 24 | self.next() << 16 | self.next() << 8 | self.next()
+ elif x == 0xF4:
+ i = self.next() << 56 | self.next() << 48 | self.next() << 40 | self.next() << 32 | self.next() << 24 | self.next() << 16 | self.next() << 8 | self.next()
+ elif x == 0xF8:
+ i = self.getInt()
+ i = ~i
+ elif x == 0xFC:
+ i = v & 0x03
+ i = ~i
+ else:
+ ok = False
+ i = 0
+ elif (v & 0xF0) == 0xE0:
+ i = (v & 0x0F) << 24 | self.next() << 16 | self.next() << 8 | self.next()
+ elif (v & 0xE0) == 0xC0:
+ i =(v & 0x1F) << 16 | self.next() << 8 | self.next()
+
+ return i
+
+ def putInt(self, value):
+ i = value;
+
+ if (i & 0x8000000000000000) and (~i < 0x100000000):
+ i = ~i;
+ if i <= 0x3:
+ # Shortcase for -1 to -4
+ self.append(0xFC | i);
+ else:
+ self.append(0xF8);
+ if i < 0x80:
+ # Need top bit clear
+ self.append(i);
+ elif i < 0x4000:
+ # Need top two bits clear
+ self.append((i >> 8) | 0x80);
+ self.append(i & 0xFF);
+ elif i < 0x200000:
+ # Need top three bits clear
+ self.append((i >> 16) | 0xC0);
+ self.append((i >> 8) & 0xFF);
+ self.append(i & 0xFF);
+ elif i < 0x10000000:
+ # Need top four bits clear
+ self.append((i >> 24) | 0xE0);
+ self.append((i >> 16) & 0xFF);
+ self.append((i >> 8) & 0xFF);
+ self.append(i & 0xFF);
+ elif i < 0x100000000:
+ # It's a full 32-bit integer.
+ self.append(0xF0);
+ self.append((i >> 24) & 0xFF);
+ self.append((i >> 16) & 0xFF);
+ self.append((i >> 8) & 0xFF);
+ self.append(i & 0xFF);
+ else:
+ # It's a 64-bit value.
+ self.append(0xF4);
+ self.append((i >> 56) & 0xFF);
+ self.append((i >> 48) & 0xFF);
+ self.append((i >> 40) & 0xFF);
+ self.append((i >> 32) & 0xFF);
+ self.append((i >> 24) & 0xFF);
+ self.append((i >> 16) & 0xFF);
+ self.append((i >> 8) & 0xFF);
+ self.append(i & 0xFF);
Oops, something went wrong.

0 comments on commit d81ed02

Please sign in to comment.