File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using Godot ;
22using System ;
33using System . Linq ;
4+ using System . Net . Sockets ;
45using System . Threading ;
56using System . Threading . Tasks ;
67using BabbleCalibration . Scripts ;
@@ -20,6 +21,28 @@ public partial class MainScene : Node
2021 public GodotPacketHandler PacketHandler { get ; private set ; }
2122
2223 private bool _sendPackets = true ;
24+
25+ private Socket TryConnect ( int retries = 5 )
26+ {
27+ int reconnectCounter = 0 ;
28+ while ( true )
29+ {
30+ try
31+ {
32+ var sock = new SocketFactory ( ) . CreateClient ( "127.0.0.1" , 2425 ) ;
33+ return sock ;
34+ }
35+ catch ( Exception e )
36+ {
37+ reconnectCounter ++ ;
38+ Thread . Sleep ( 500 ) ;
39+ if ( reconnectCounter > retries )
40+ {
41+ return null ;
42+ }
43+ }
44+ }
45+ }
2346
2447 public Transform3D OriginOffset { get ; private set ; }
2548
@@ -88,6 +111,13 @@ public override void _Ready()
88111 }
89112 else
90113 {
114+ Socket sock = TryConnect ( ) ;
115+ if ( sock == null )
116+ {
117+ GD . Print ( "Could not connect to Baballonia" ) ;
118+ GetTree ( ) . Quit ( - 1 ) ;
119+ return ;
120+ }
91121 try
92122 {
93123 PacketHandler = new GodotPacketHandler
@@ -97,7 +127,7 @@ public override void _Ready()
97127 new GodotLogger ( ) ,
98128 new EventDrivenJsonClient
99129 (
100- new EventDrivenTcpClient ( new SocketFactory ( ) . CreateClient ( "127.0.0.1" , 2425 ) )
130+ new EventDrivenTcpClient ( sock )
101131 )
102132 )
103133 ) ;
You can’t perform that action at this time.
0 commit comments