Skip to content

Commit 8399739

Browse files
committed
including ws2def.h
1 parent e09d09f commit 8399739

File tree

2 files changed

+148
-9
lines changed

2 files changed

+148
-9
lines changed

src/dds.net-connector-cpp.lib/connector/src/internal/inc/network_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stdlib.h>
3535
#include <winsock2.h>
3636
#include <WS2tcpip.h>
37+
#include <ws2def.h>
3738
#include <fcntl.h>
3839

3940
#pragma comment(lib, "ws2_32.lib")

src/dds.net-connector-cpp.lib/connector/src/internal/src/network_client.cpp

Lines changed: 147 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ dds::net::connector::_internal::
6666

6767
if (wsaStartCount == 0)
6868
{
69-
WSADATA wsaData;
69+
WSADATA wsaData;
7070

71-
int windowsStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
71+
int windowsStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
7272

73-
if (windowsStartupResult != 0)
74-
{
73+
if (windowsStartupResult != 0)
74+
{
7575
std::string msg = "WSAStartup failed with error code: ";
76-
msg += windowsStartupResult;
76+
msg += windowsStartupResult;
7777

78-
this->logger->error(msg.c_str());
79-
}
78+
this->logger->error(msg.c_str());
79+
}
8080
else
8181
{
8282
this->logger->info("WSA Started.");
83-
}
83+
}
8484
}
8585

8686
wsaStartCount++;
@@ -141,9 +141,147 @@ void
141141

142142
void
143143
dds::net::connector::_internal::
144-
ioThreadFunc(NetworkClient* client)
144+
ioThreadFunc(NetworkClient* net)
145145
{
146+
while (net->isIOThreadStarted)
147+
{
148+
if (net->isConnected == false)
149+
{
150+
net->socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
151+
146152

153+
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
154+
if (net->socketFileDescriptor == -1)
155+
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
156+
if (net->socketFileDescriptor == INVALID_SOCKET)
157+
#else
158+
#error "Cannot check socket validity on selected platform"
159+
#endif
160+
{
161+
std::string msg = "Socket cannot be created for TCP Client connecting with ";
162+
msg += net->ipv4;
163+
msg += ":";
164+
msg += net->tcpPort;
165+
166+
net->logger->error(msg.c_str());
167+
168+
net->isIOThreadStarted = false;
169+
return;
170+
}
171+
try
172+
{
173+
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
174+
{
175+
Blocking = false
176+
};
177+
}
178+
catch (Exception)
179+
{
180+
try
181+
{
182+
socket ? .Close();
183+
socket ? .Dispose();
184+
}
185+
catch { }
186+
187+
socket = null!;
188+
}
189+
}
190+
else
191+
{
192+
if (!socket.Connected)
193+
{
194+
try
195+
{
196+
socket.ConnectAsync(targetEndPoint);
197+
198+
dataToServerQueue.Clear();
199+
dataFromServerQueue.Clear();
200+
201+
ConnectedWithServer ? .Invoke();
202+
}
203+
catch
204+
{
205+
Thread.Sleep(100);
206+
}
207+
}
208+
else
209+
{
210+
bool doneAnythingInIteration = false;
211+
212+
try
213+
{
214+
//-
215+
//- Receiving data
216+
//-
217+
218+
if (socket.Available > 0)
219+
{
220+
doneAnythingInIteration = true;
221+
222+
byte[] bytes = new byte[socket.Available];
223+
224+
int totalReceived = socket.Receive(bytes, SocketFlags.None);
225+
226+
dataFromServerQueue.Enqueue(new PacketFromServer(bytes));
227+
}
228+
229+
//-
230+
//- Transmitting data
231+
//-
232+
233+
while (dataToServerQueue.CanDequeue())
234+
{
235+
doneAnythingInIteration = true;
236+
237+
PacketToServer packet = dataToServerQueue.Dequeue();
238+
239+
socket.Send(packet.Data, packet.TotalBytesToBeSent, SocketFlags.None);
240+
}
241+
}
242+
catch
243+
{
244+
DisconnectedFromServer ? .Invoke();
245+
246+
try
247+
{
248+
socket ? .Close();
249+
socket ? .Dispose();
250+
}
251+
catch { }
252+
253+
socket = null!;
254+
}
255+
256+
if (!doneAnythingInIteration)
257+
{
258+
Thread.Sleep(10);
259+
}
260+
}
261+
}
262+
} // while (isIOThreadStarted)
263+
264+
if (socket ? .Connected == true)
265+
{
266+
try
267+
{
268+
DisconnectedFromServer ? .Invoke();
269+
socket ? .DisconnectAsync(false);
270+
}
271+
catch { }
272+
}
273+
274+
275+
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
276+
if (net->socketFileDescriptor != -1)
277+
{
278+
close(socketFileDescriptor);
279+
}
280+
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
281+
closesocket(socketFileDescriptor);
282+
#else
283+
#error "Cannot close socket on selected platform"
284+
#endif
147285
}
148286

149287

0 commit comments

Comments
 (0)