| 116 | def start_listen_sockets(self): |
| 117 | for sock_def, options in self.sockets.items(): |
| 118 | socktype, sock, info, _ = sock_def |
| 119 | log("start_listen_sockets() will add %s socket %s (%s)", socktype, sock, info) |
| 120 | self.socket_info[sock] = info |
| 121 | self.socket_options[sock] = options |
| 122 | self.idle_add(self.add_listen_socket, socktype, sock) |
| 123 | |
| 124 | def add_listen_socket(self, socktype, sock): |
| 125 | info = self.socket_info.get(sock) |
| 126 | log("add_listen_socket(%s, %s) info=%s", socktype, sock, info) |
| 127 | cleanup = add_listen_socket(socktype, sock, info, self._new_connection, None) |
| 128 | if cleanup: |
| 129 | self.socket_cleanup.append(cleanup) |
| 130 | |
| 131 | def _new_connection(self, socktype, listener, handle=0): |
| 132 | """ |
| 133 | Accept the new connection, |
| 134 | verify that there aren't too many, |
| 135 | start a thread to dispatch it to the correct handler. |
| 136 | """ |
| 137 | log("_new_connection%s", (listener, socktype, handle)) |
| 138 | if self.exit_code is not None: |
| 139 | log("ignoring new connection during shutdown") |
| 140 | return False |
| 141 | try: |
| 142 | self.handle_new_connection(socktype, listener, handle) |
| 143 | except Exception: |
| 144 | log.error("Error handling new connection", exc_info=True) |
| 145 | return self.exit_code is None |
| 146 | |
| 147 | def handle_new_connection(self, socktype, listener, handle): |
| 148 | socket_info = self.socket_info.get(listener) |
| 149 | assert socktype, "cannot find socket type for %s" % listener |
| 150 | socket_options = self.socket_options.get(listener, {}) |
| 151 | assert socktype!="named-pipe" |
| 152 | conn = accept_connection(socktype, listener, SOCKET_TIMEOUT, socket_options) |
| 153 | if conn is None: |
| 154 | return |
| 155 | #limit number of concurrent network connections: |
| 156 | if len(self._potential_protocols)>=MAX_CONCURRENT_CONNECTIONS: |
| 157 | log.error("Error: too many connections (%i)", len(self._potential_protocols)) |
| 158 | log.error(" ignoring new one: %s", conn.endpoint) |
| 159 | conn.close() |
| 160 | return |
| 161 | sock = conn._socket |
| 162 | socktype = conn.socktype |
| 163 | peername = conn.endpoint |
| 164 | sockname = sock.getsockname() |
| 165 | target = peername or sockname |
| 166 | log("handle_new_connection%s sockname=%s, target=%s", |
| 167 | (conn, socket_info, socket_options), sockname, target) |
| 168 | sock.settimeout(SOCKET_TIMEOUT) |
| 169 | log_new_connection(conn, socket_info) |
| 170 | |
| 171 | socktype = socktype.lower() |
| 172 | protocol = Protocol(self, conn, self.process_network_packet) |
| 173 | #protocol.large_packets.append(b"info-response") |
| 174 | protocol.socket_type = socktype |
| 175 | self._potential_protocols.append(protocol) |
| 176 | protocol.authenticators = () |
| 177 | protocol.start() |
| 178 | #self.schedule_verify_connection_accepted(protocol, self._accept_timeout) |
| 179 | |
| 180 | def process_network_packet(self, proto, packet): |
| 181 | log.info("packet: %s", packet) |
| 182 | log.info("packet type: %s", packet[0]) |
| 183 | if packet[0]==b"hello": |
| 184 | caps = typedict(packet[1]) |
| 185 | proto.parse_remote_caps(caps) |
| 186 | proto.enable_compressor_from_caps(caps) |
| 187 | proto.enable_encoder_from_caps(caps) |
| 188 | hello = get_network_caps() |
| 189 | proto.send_now(["hello", hello]) |
| 190 | info = self.get_info() |
| 191 | proto.send_now(["info-response", info]) |
| 192 | import time |
| 193 | time.sleep(1) |
| 194 | proto.close() |
| 195 | |
| 196 | |