25
25
26
26
#include <windows.h>
27
27
#include "../src/include/CompatibilityDefs.h"
28
- #include <stdio.h>
29
- #include <conio.h>
30
- #include <tchar.h>
31
28
32
29
#pragma comment( lib, "Ws2_32.lib" )
33
30
@@ -56,15 +53,15 @@ DWORD WINAPI MDD_udpReceivingThread(LPVOID pUdp) {
56
53
LeaveCriticalSection (& udp -> receiveLock );
57
54
if (socketError ) {
58
55
ModelicaFormatMessage ("MDDUDPSocket.h: Receiving not possible, socket not valid.\n" );
59
- ExitThread (SOCKET_ERROR );
56
+ ExitThread (1 );
60
57
}
61
58
}
62
59
return 0 ;
63
60
}
64
61
65
62
DllExport void * MDD_udpConstructor (int port , int bufferSize ) {
66
63
67
- int rc ; /* Error variable */
64
+ int rc ; /* Error variable */
68
65
WSADATA wsa ;
69
66
SOCKADDR_IN addr ;
70
67
DWORD id1 ;
@@ -79,23 +76,27 @@ DllExport void * MDD_udpConstructor(int port, int bufferSize) {
79
76
udp -> SocketID = socket (AF_INET ,SOCK_DGRAM ,0 );
80
77
if (udp -> SocketID == INVALID_SOCKET ) {
81
78
free (udp );
79
+ udp = NULL ;
82
80
rc = WSAGetLastError ();
83
81
WSACleanup ();
84
82
ModelicaFormatError ("MDDUDPSocket.h: Error at socket(): %ld\n" , rc );
83
+ return udp ;
85
84
}
86
85
udp -> receiving = 1 ;
87
86
udp -> bufferSize = bufferSize ;
88
87
udp -> receivedBytes = 0 ;
89
88
addr .sin_family = AF_INET ;
90
- addr .sin_port = htons (port );
89
+ addr .sin_port = htons (( u_short ) port );
91
90
addr .sin_addr .s_addr = INADDR_ANY ;
92
91
93
92
if (port ) {
94
93
rc = bind (udp -> SocketID ,(SOCKADDR * )& addr ,sizeof (SOCKADDR_IN ));
95
94
if (rc == INVALID_SOCKET ) {
96
95
free (udp );
96
+ udp = NULL ;
97
97
WSACleanup ();
98
98
ModelicaFormatError ("MDDUDPSocket.h: Error at bind(..) to port %d\n" , port );
99
+ return udp ;
99
100
}
100
101
udp -> receiveBuffer = (char * )calloc (bufferSize , 1 );
101
102
udp -> receiveBufferExport = (char * )calloc (bufferSize , 1 );
@@ -106,8 +107,10 @@ DllExport void * MDD_udpConstructor(int port, int bufferSize) {
106
107
free (udp -> receiveBuffer );
107
108
free (udp -> receiveBufferExport );
108
109
free (udp );
110
+ udp = NULL ;
109
111
WSACleanup ();
110
112
ModelicaError ("MDDUDPSocket.h: Error creating UDP Receiver thread.\n" );
113
+ return udp ;
111
114
}
112
115
ModelicaFormatMessage ("MDDUDPSocket.h: Waiting for data on port %d.\n" , port );
113
116
}
@@ -125,9 +128,9 @@ DllExport void MDD_udpDestructor(void * p_udp) {
125
128
if (rc == SOCKET_ERROR ) {
126
129
ModelicaFormatMessage ("MDDUDPSocket.h: shutdown failed: %d\n" , WSAGetLastError ());
127
130
}
128
- rc = closesocket (udp -> SocketID );
131
+ closesocket (udp -> SocketID );
129
132
if (udp -> hThread ) {
130
- DWORD dwEc = - 1 ;
133
+ DWORD dwEc = 1 ;
131
134
while (GetExitCodeThread (udp -> hThread , & dwEc ) && dwEc == STILL_ACTIVE ) {
132
135
;
133
136
}
@@ -146,7 +149,7 @@ DllExport void MDD_udpSend(void * p_udp, const char * ipAddress, int port,
146
149
if (udp ) {
147
150
SOCKADDR_IN addr ;
148
151
addr .sin_family = AF_INET ;
149
- addr .sin_port = htons (port );
152
+ addr .sin_port = htons (( u_short ) port );
150
153
addr .sin_addr .s_addr = inet_addr (ipAddress );
151
154
sendto (udp -> SocketID ,data ,dataSize ,0 ,(SOCKADDR * )& addr ,sizeof (SOCKADDR_IN ));
152
155
}
0 commit comments