fix download link to bitcoind
[electrum-server.git] / transports / stratum_tcp.py
index ffdd871..88ec0cf 100644 (file)
@@ -1,10 +1,13 @@
 import json
+import Queue as queue
 import socket
 import threading
 import time
-import Queue as queue
+import traceback, sys
+
+from processor import Session, Dispatcher
+from utils import print_log
 
-from processor import Session, Dispatcher, print_log
 
 class TcpSession(Session):
 
@@ -31,8 +34,17 @@ class TcpSession(Session):
             return self._connection
 
     def stop(self):
+        if self.stopped():
+            return
+
+        try:
+            self._connection.shutdown(socket.SHUT_RDWR)
+        except:
+            # print_log("problem shutting down", self.address)
+            # traceback.print_exc(file=sys.stdout)
+            pass
+
         self._connection.close()
-        #print "Terminating connection:", self.address
         with self.lock:
             self._stopped = True
 
@@ -50,7 +62,6 @@ class TcpSession(Session):
             self.stop()
 
 
-
 class TcpClientRequestor(threading.Thread):
 
     def __init__(self, dispatcher, session):
@@ -93,7 +104,7 @@ class TcpClientRequestor(threading.Thread):
 
         raw_command = self.message[0:raw_buffer].strip()
         self.message = self.message[raw_buffer + 1:]
-        if raw_command == 'quit': 
+        if raw_command == 'quit':
             self.session.stop()
             return False
 
@@ -112,10 +123,11 @@ class TcpClientRequestor(threading.Thread):
             # Return an error JSON in response.
             self.dispatcher.push_response({"error": "syntax error", "request": raw_command})
         else:
-            self.dispatcher.push_request(self.session,command)
+            self.dispatcher.push_request(self.session, command)
 
         return True
 
+
 class TcpServer(threading.Thread):
 
     def __init__(self, dispatcher, host, port, use_ssl, ssl_certfile, ssl_keyfile):
@@ -132,22 +144,33 @@ class TcpServer(threading.Thread):
 
     def run(self):
         if self.use_ssl:
-            print_log( "TCP/SSL server started.")
+            print_log("TCP/SSL server started.")
         else:
-            print_log( "TCP server started.")
+            print_log("TCP server started.")
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         sock.bind((self.host, self.port))
-        sock.listen(1)
+        sock.listen(5)
+
         while not self.shared.stopped():
+
+            try:
+                connection, address = sock.accept()
+            except:
+                traceback.print_exc(file=sys.stdout)
+                time.sleep(0.1)
+                continue
+
             try:
-                session = TcpSession(*sock.accept(), use_ssl=self.use_ssl, ssl_certfile=self.ssl_certfile, ssl_keyfile=self.ssl_keyfile)
+                session = TcpSession(connection, address, use_ssl=self.use_ssl, ssl_certfile=self.ssl_certfile, ssl_keyfile=self.ssl_keyfile)
             except BaseException, e:
                 error = str(e)
-                print_log("cannot start TCP session", error)
+                print_log("cannot start TCP session", error, address)
+                connection.close()
+                time.sleep(0.1)
                 continue
+
             self.dispatcher.add_session(session)
             self.dispatcher.collect_garbage()
             client_req = TcpClientRequestor(self.dispatcher, session)
             client_req.start()
-