Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Split up start_server() and start_client().
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Feb 20, 2018
1 parent d1a0c51 commit d275a9d
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions ptvsd/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,32 +879,57 @@ def on_pydevd_send_curr_exception_trace_proceeded(self, seq, args):
pass


def start_server(port):
"""Return a socket to a (new) local pydevd-handling daemon.
The daemon supports the pydevd client wire protocol, sending
requests and handling responses (and events).
########################
# lifecycle

This is a replacement fori _pydevd_bundle.pydevd_comm.start_server.
"""
server = socket.socket(socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def _create_server(port):
server = _new_sock()
server.bind(('127.0.0.1', port))
server.listen(1)
client, addr = server.accept()
return server


def _create_client():
return _new_sock()


def _new_sock():
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return sock


def _start(client, server):
name = 'ptvsd.Client' if server is None else 'ptvsd.Server'

pydevd = PydevdSocket(lambda *args: proc.on_pydevd_event(*args))
proc = VSCodeMessageProcessor(client, pydevd)

server_thread = threading.Thread(target=proc.process_messages,
name='ptvsd.Server')
name=name)
server_thread.daemon = True
server_thread.start()

atexit.register(proc.close)
return pydevd, proc, server_thread


########################
# pydevd hooks

def start_server(port):
"""Return a socket to a (new) local pydevd-handling daemon.
The daemon supports the pydevd client wire protocol, sending
requests and handling responses (and events).
This is a replacement fori _pydevd_bundle.pydevd_comm.start_server.
"""
server = _create_server(port)
client, _ = server.accept()
pydevd, proc, _ = _start(client, server)
atexit.register(proc.close)
return pydevd


Expand All @@ -916,22 +941,10 @@ def start_client(host, port):
This is a replacement fori _pydevd_bundle.pydevd_comm.start_client.
"""
client = socket.socket(socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client = _create_client()
client.connect((host, port))

pydevd = PydevdSocket(lambda *args: proc.on_pydevd_event(*args))
proc = VSCodeMessageProcessor(client, pydevd)

server_thread = threading.Thread(target=proc.process_messages,
name='ptvsd.Client')
server_thread.daemon = True
server_thread.start()

pydevd, proc, _ = _start(client, None)
atexit.register(proc.close)

return pydevd


Expand Down

0 comments on commit d275a9d

Please sign in to comment.