Cooperation/Connection.py

changeset 162
28f235c426c4
parent 155
375e3c884874
child 164
b395b006d2a8
equal deleted inserted replaced
161:bb3cc98f4104 162:28f235c426c4
6 """ 6 """
7 Module implementing a class representing a peer connection. 7 Module implementing a class representing a peer connection.
8 """ 8 """
9 9
10 from PyQt4.QtCore import pyqtSignal, QTimer, QTime, QByteArray 10 from PyQt4.QtCore import pyqtSignal, QTimer, QTime, QByteArray
11 from PyQt4.QtGui import QMessageBox
11 from PyQt4.QtNetwork import QTcpSocket 12 from PyQt4.QtNetwork import QTcpSocket
13
14 import Preferences
12 15
13 MaxBufferSize = 1024 * 1024 16 MaxBufferSize = 1024 * 1024
14 TransferTimeout = 30 * 1000 17 TransferTimeout = 30 * 1000
15 PongTimeout = 60 * 1000 18 PongTimeout = 60 * 1000
16 PingInterval = 5 * 1000 19 PingInterval = 5 * 1000
17 SeparatorToken = '|||' 20 SeparatorToken = '|||'
18
19 21
20 class Connection(QTcpSocket): 22 class Connection(QTcpSocket):
21 """ 23 """
22 Class representing a peer connection. 24 Class representing a peer connection.
23 25
150 self.__buffer = QByteArray(self.read(self.__numBytesForCurrentDataType)) 152 self.__buffer = QByteArray(self.read(self.__numBytesForCurrentDataType))
151 if self.__buffer.size() != self.__numBytesForCurrentDataType: 153 if self.__buffer.size() != self.__numBytesForCurrentDataType:
152 self.abort() 154 self.abort()
153 return 155 return
154 156
155 user, serverPort = str(self.__buffer, encoding = "utf-8").split(":") 157 try:
158 user, serverPort = str(self.__buffer, encoding = "utf-8").split(":")
159 except ValueError:
160 self.abort()
161 return
156 self.__serverPort = int(serverPort) 162 self.__serverPort = int(serverPort)
163
157 self.__username = "{0}@{1}:{2}".format( 164 self.__username = "{0}@{1}:{2}".format(
158 user, 165 user,
159 self.peerAddress().toString(), 166 self.peerAddress().toString(),
160 self.peerPort() 167 self.peerPort()
161 ) 168 )
165 172
166 if not self.isValid(): 173 if not self.isValid():
167 self.abort() 174 self.abort()
168 return 175 return
169 176
177 if self.__serverPort != self.peerPort() and \
178 not Preferences.getCooperation("AutoAcceptConnections"):
179 # don't ask for reverse connections or
180 # if we shall accept automatically
181 res = QMessageBox.question(None,
182 self.trUtf8("New Connection"),
183 self.trUtf8("""<p>Accept connection from """
184 """<strong>{0}@{1}</strong>?</p>""").format(
185 user, self.peerAddress().toString()),
186 QMessageBox.StandardButtons(\
187 QMessageBox.No | \
188 QMessageBox.Yes),
189 QMessageBox.Yes)
190 if res == QMessageBox.No:
191 self.abort()
192 return
193
170 if not self.__isGreetingMessageSent: 194 if not self.__isGreetingMessageSent:
171 self.__sendGreetingMessage() 195 self.__sendGreetingMessage()
172 196
173 self.__pingTimer.start() 197 self.__pingTimer.start()
174 self.__pongTime.start() 198 self.__pongTime.start()

eric ide

mercurial