Редакция 3 | Весь файл | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 3 | Редакция 5 | ||
---|---|---|---|
Строка 1... | Строка 1... | ||
1 | # -*- coding: utf-8 -*-
|
1 | # -*- coding: utf-8 -*-
|
2 | import sys |
2 | import sys |
3 | 3 | ||
4 | class PacketProcessor:
|
- | |
5 | """ Class for processing tcp packets,
|
- | |
6 | and make DC++ packets. (Merge packets from
|
- | |
7 | one command). """
|
- | |
8 | def __init__(self): |
- | |
9 | """ Clear buffer and initialize class. """
|
- | |
10 | buffer = "" |
- | |
11 | - | ||
12 | def _isFull(self, command): |
- | |
13 | """ Check buffer for full command. """
|
- | |
14 | if len(command) == 0: |
- | |
15 | return False |
- | |
16 | if command[len(command) - 1] == '|': |
- | |
17 | return True |
- | |
18 | else:
|
- | |
19 | return False |
- | |
20 | - | ||
21 | def Process(self, socket, packet, commandProcessor): |
- | |
22 | """ Process new tcp packet. """
|
- | |
23 | # Save previous packets
|
- | |
24 | prev = self.buffer |
- | |
25 | # Clear buffer
|
- | |
26 | self.buffer = "" |
- | |
27 | # Merge previous and packet
|
- | |
28 | buf = prev + packet
|
- | |
29 | # Split
|
- | |
30 | commands = buf.split('|') |
- | |
31 | # If Merge result not is full command.
|
- | |
32 | if not self._isFull(buf): |
- | |
33 | # If count of commands in Merge result is 1
|
- | |
34 | if len(commands) == 1: |
- | |
35 | # Save Merge result to buffer
|
- | |
36 | self.buffer = commands[0] |
- | |
37 | commands = [] |
- | |
38 | else:
|
- | |
39 | # Save first from all commands to buffer
|
- | |
40 | self.buffer = commands[len(commands) - 1] |
- | |
41 | commands = commands[0:len(commands)-1] |
- | |
42 | # Process every command
|
- | |
43 | for i in commands: |
- | |
44 | commandProcessor(i, socket) |
- | |
45 | - | ||
46 | def ClearBuffer(self): |
- | |
47 | self.buffer = "" |
- | |
48 | - | ||
49 | - | ||
50 | def createKey(lock): |
4 | def createKey(lock): |
51 | """ Function create key from lock for DC connections. """
|
5 | """ Function create key from lock for DC connections. """
|
52 | key = {} |
6 | key = {} |
53 | for i in xrange(1, len(lock)): |
7 | for i in xrange(1, len(lock)): |
54 | key[i] = ord(lock[i]) ^ ord(lock[i-1]) |
8 | key[i] = ord(lock[i]) ^ ord(lock[i-1]) |