summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-27 11:16:31 +0800
committerTeddy <[email protected]>2013-08-27 11:16:31 +0800
commit21364472978aac014e42486ecb0b5b6f8e4ee7c2 (patch)
tree295884d06ba2530e2b46915c28692bdc468ebab5 /server
parent2c3891cf77538c57b172404c5c35b846ec6b0de6 (diff)
Exception catching in server
Diffstat (limited to 'server')
-rw-r--r--server/piztor/exc.py11
-rw-r--r--server/piztor/prob.py5
-rw-r--r--server/piztor/server.py51
3 files changed, 31 insertions, 36 deletions
diff --git a/server/piztor/exc.py b/server/piztor/exc.py
index 2c53dbf..390193c 100644
--- a/server/piztor/exc.py
+++ b/server/piztor/exc.py
@@ -4,14 +4,5 @@ class PiztorError(Exception):
class DBCurruptedError(PiztorError):
pass
-class ConnectionError(PiztorError):
- pass
-
-class ReqReadError(ConnectionError):
- pass
-
-class BadReqError(ConnectionError):
- pass
-
-class BadTokenError(ConnectionError):
+class BadReqError(PiztorError):
pass
diff --git a/server/piztor/prob.py b/server/piztor/prob.py
index 9798c18..248f717 100644
--- a/server/piztor/prob.py
+++ b/server/piztor/prob.py
@@ -56,9 +56,10 @@ def send(data):
from sys import argv
-#username = "hello"
-username = "12345678901234567890"
+username = "hello"
password = "world"
+#username = "1234567890123456789012"
+#password = "world12345678901234567890"
gid = 1
if len(argv) == 2:
diff --git a/server/piztor/server.py b/server/piztor/server.py
index eb321fc..fae5ca7 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -367,31 +367,34 @@ class PTP(Protocol, TimeoutMixin):
self.buff += data
self.resetTimeout()
logger.info("Buffer length is now: %d", len(self.buff))
- if len(self.buff) > 4:
- try:
- self.length, self.optcode = struct.unpack("!LB", self.buff[:5])
- if not PTP.check_header(self.optcode): # invalid header
- raise struct.error
- except struct.error:
- raise BadReqError("Malformed request header")
- if self.length > PTP._MAX_REQUEST_SIZE:
- print self.length, PTP._MAX_REQUEST_SIZE
- raise BadReqError("The size of remaining part is too big")
-
- # Incomplete length info
- if self.length == -1: return
-
- if len(self.buff) == self.length:
- h = PTP.handlers[self.optcode]()
- reply = h.handle(self.buff[5:])
- logger.info("Wrote: %s", get_hex(reply))
- self.transport.write(reply)
- self.transport.loseConnection()
-
- elif len(self.buff) > self.length:
+ if len(self.buff) <= 4:
+ return
+ try:
+ if self.length == -1:
+ try:
+ self.length, self.optcode = struct.unpack("!LB", self.buff[:5])
+ if not PTP.check_header(self.optcode): # invalid header
+ raise struct.error
+ except struct.error:
+ raise BadReqError("Malformed request header")
+ if self.length > PTP._MAX_REQUEST_SIZE:
+ print self.length, PTP._MAX_REQUEST_SIZE
+ raise BadReqError("The size of remaining part is too big")
+ if len(self.buff) == self.length:
+ h = PTP.handlers[self.optcode]()
+ reply = h.handle(self.buff[5:])
+ logger.info("Wrote: %s", get_hex(reply))
+ self.transport.write(reply)
+ self.transport.loseConnection()
+ elif len(self.buff) > self.length:
+ raise BadReqError("The actual length is larger than promised")
+ except BadReqError as e:
+ logger.warn("Rejected a bad request: %s", str(e))
+ except DBCorruptedError:
+ logger.error("*** Database corruption ***")
+ finally:
self.transport.loseConnection()
-
def connectionLost(self, reason):
logger.info("The connection is lost")
self.setTimeout(None)
@@ -414,5 +417,5 @@ from twisted.internet import reactor
f = PTPFactory()
f.protocol = PTP
reactor.listenTCP(2222, f)
-logger.warning("The server is on")
+logger.warning("The server is lanuched")
reactor.run()