Skip to content

Commit 142c5af

Browse files
authored
Merge pull request #3 from RedInJector/main
pull
2 parents de3e48d + e1d7831 commit 142c5af

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

Scripts/MainScene.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Godot;
22
using System;
33
using System.Linq;
4+
using System.Net.Sockets;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using 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
);

0 commit comments

Comments
 (0)