fix socket send; increase buffer
[electrum-server.git] / transports / stratum_tcp.py
index ce39aca..85fc8b7 100644 (file)
@@ -4,15 +4,15 @@ import threading
 import time
 import Queue as queue
 
-from processor import Session, Dispatcher, Shared
+from processor import Session, Dispatcher, timestr
 
 class TcpSession(Session):
 
     def __init__(self, connection, address):
-        self._connection = connection
-        self.address = address
         Session.__init__(self)
-        print "New session", address
+        self._connection = connection
+        self.address = address[0]
+        self.name = "TCP session"
 
     def connection(self):
         if self.stopped():
@@ -22,18 +22,20 @@ class TcpSession(Session):
 
     def stop(self):
         self._connection.close()
-        print "Terminating connection:", self.address[0]
+        #print "Terminating connection:", self.address
         with self.lock:
             self._stopped = True
 
     def send_response(self, response):
-        raw_response = json.dumps(response)
+        data = json.dumps(response) + "\n"
         # Possible race condition here by having session
         # close connection?
         # I assume Python connections are thread safe interfaces
         try:
             connection = self.connection()
-            connection.send(raw_response + "\n")
+            while data:
+                l = connection.send(data)
+                data = data[l:]
         except:
             self.stop()
 
@@ -41,9 +43,9 @@ class TcpSession(Session):
 
 class TcpClientRequestor(threading.Thread):
 
-    def __init__(self, shared, processor, session):
-        self.shared = shared
-        self.processor = processor
+    def __init__(self, dispatcher, session):
+        self.shared = dispatcher.shared
+        self.dispatcher = dispatcher
         self.message = ""
         self.session = session
         threading.Thread.__init__(self)
@@ -68,7 +70,7 @@ class TcpClientRequestor(threading.Thread):
 
     def receive(self):
         try:
-            return self.session.connection().recv(1024)
+            return self.session.connection().recv(2048)
         except:
             return ''
 
@@ -86,7 +88,7 @@ class TcpClientRequestor(threading.Thread):
         try:
             command = json.loads(raw_command)
         except:
-            self.processor.push_response({"error": "bad JSON", "request": raw_command})
+            self.dispatcher.push_response({"error": "bad JSON", "request": raw_command})
             return True
 
         try:
@@ -96,17 +98,17 @@ class TcpClientRequestor(threading.Thread):
             method = command['method']
         except KeyError:
             # Return an error JSON in response.
-            self.processor.push_response({"error": "syntax error", "request": raw_command})
+            self.dispatcher.push_response({"error": "syntax error", "request": raw_command})
         else:
-            self.processor.push_request(self.session,command)
+            self.dispatcher.push_request(self.session,command)
 
         return True
 
 class TcpServer(threading.Thread):
 
-    def __init__(self, shared, processor, host, port):
-        self.shared = shared
-        self.processor = processor
+    def __init__(self, dispatcher, host, port):
+        self.shared = dispatcher.shared
+        self.dispatcher = dispatcher.request_dispatcher
         threading.Thread.__init__(self)
         self.daemon = True
         self.host = host
@@ -121,10 +123,10 @@ class TcpServer(threading.Thread):
         sock.listen(1)
         while not self.shared.stopped():
             session = TcpSession(*sock.accept())
-            client_req = TcpClientRequestor(self.shared, self.processor, session)
+            client_req = TcpClientRequestor(self.dispatcher, session)
             client_req.start()
-            self.processor.add_session(session)
-            self.processor.collect_garbage()
+            self.dispatcher.add_session(session)
+            self.dispatcher.collect_garbage()