Skip to content

Commit

Permalink
Merge pull request #121 from martius-lab/Paco/client-reconnect
Browse files Browse the repository at this point in the history
client connection timeout
  • Loading branch information
PacoSchatz authored Mar 15, 2024
2 parents c6a57ee + 9ee0e19 commit fb4d43a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions comprl/client/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,15 @@ def connect_agent(agent: IAgent, host: str = "localhost", port: int = 65335):
None
"""

def __on_error(reason):
agent.on_error(f"Connection to the server failed: {reason}.")
if reactor.running:
reactor.stop()

connectProtocol(
TCP4ClientEndpoint(reactor, host, port),
TCP4ClientEndpoint(reactor, host, port, timeout=30),
ClientProtocol(agent),
# we lose the protocol here, is this a problem?
)
).addErrback(__on_error)
reactor.run() # type: ignore[attr-defined]
3 changes: 3 additions & 0 deletions examples/hockey/weak_hockey_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ def on_end_game(result, stats):


Weak_Hockey_Agent.run("token" + str(input("enter 1, 2, 3 or 4 to choose token: ")))

# restarting the reactor is not easy. This might work:
# https://www.blog.pythonlibrary.org/2016/09/14/restarting-a-twisted-reactor/

0 comments on commit fb4d43a

Please sign in to comment.