@@ -28,86 +28,114 @@ struct MDDTCPIPSocket_s {
28
28
SOCKET SocketID ;
29
29
};
30
30
31
- DllExport void * MDD_TCPIPClient_Constructor (const char * ipaddress , int port ) {
32
-
33
- int rc ; /* Error variable */
34
- WSADATA wsa ;
35
- struct addrinfo * result = NULL ;
36
- struct addrinfo * ptr = NULL ;
37
- struct addrinfo hints ;
38
- MDDTCPIPSocket * tcpip ;
39
- char port_str [20 ];
40
-
41
- /* Initialize Winsock */
42
- rc = WSAStartup (MAKEWORD (2 ,2 ), & wsa );
43
- if (rc != NO_ERROR ) {
44
- ModelicaFormatError ("MDDTCPIPSocket.h: WSAStartup failed with error: %d\n" , rc );
31
+ DllExport void * MDD_TCPIPClient_Constructor (void ) {
32
+ MDDTCPIPSocket * * tcpip = (MDDTCPIPSocket * * )calloc (sizeof (MDDTCPIPSocket * ), 1 );
33
+ if (tcpip ) {
34
+ * tcpip = (MDDTCPIPSocket * )calloc (sizeof (MDDTCPIPSocket ), 1 );
35
+ if (* tcpip ) {
36
+ int rc ; /* Error variable */
37
+ WSADATA wsa ;
38
+
39
+ (* tcpip )-> SocketID = INVALID_SOCKET ;
40
+
41
+ /* Initialize Winsock */
42
+ rc = WSAStartup (MAKEWORD (2 ,2 ), & wsa );
43
+ if (rc != NO_ERROR ) {
44
+ ModelicaFormatError ("MDDTCPIPSocket.h: WSAStartup failed with error: %d\n" , rc );
45
+ }
46
+ }
45
47
}
46
48
47
- memset (& hints , 0 , sizeof (hints ));
48
- hints .ai_family = AF_UNSPEC ;
49
- hints .ai_socktype = SOCK_STREAM ;
50
- hints .ai_protocol = IPPROTO_TCP ;
51
-
52
- /* Resolve the server address and port */
53
- _snprintf (port_str , 20 , "%d" , port );
54
- rc = getaddrinfo (ipaddress , port_str , & hints , & result );
55
- if (rc != NO_ERROR ) {
56
- WSACleanup ();
57
- ModelicaFormatError ("MDDTCPIPSocket.h: getaddrinfo failed with error: %d\n" , rc );
58
- }
59
-
60
- tcpip = (MDDTCPIPSocket * )calloc (sizeof (MDDTCPIPSocket ), 1 );
61
- /* Attempt to connect to an address until one succeeds */
62
- for (ptr = result ; ptr != NULL ; ptr = ptr -> ai_next ) {
63
-
64
- /* Create a SOCKET for connecting to server */
65
- tcpip -> SocketID = socket (ptr -> ai_family , ptr -> ai_socktype , ptr -> ai_protocol );
66
- if (tcpip -> SocketID == INVALID_SOCKET ) {
67
- free (tcpip );
68
- tcpip = NULL ;
69
- rc = WSAGetLastError ();
70
- WSACleanup ();
71
- ModelicaFormatError ("MDDTCPIPSocket.h: socket failed with error: %ld\n" , WSAGetLastError ());
72
- }
49
+ return (void * ) tcpip ;
50
+ }
73
51
74
- /* Connect to server */
75
- rc = connect (tcpip -> SocketID , ptr -> ai_addr , (int )ptr -> ai_addrlen );
76
- if (rc == SOCKET_ERROR ) {
77
- closesocket (tcpip -> SocketID );
78
- tcpip -> SocketID = INVALID_SOCKET ;
79
- continue ;
52
+ DllExport int MDD_TCPIPClient_Connect (void * p_tcpip , const char * ipaddress , int port ) {
53
+ MDDTCPIPSocket * * tcpip = (MDDTCPIPSocket * * ) p_tcpip ;
54
+ int ret = 0 ;
55
+ if (tcpip && * tcpip ) {
56
+ if ((* tcpip )-> SocketID == INVALID_SOCKET ) {
57
+ int rc ; /* Error variable */
58
+ struct addrinfo * result = NULL ;
59
+ struct addrinfo * ptr = NULL ;
60
+ struct addrinfo hints ;
61
+ char port_str [20 ];
62
+
63
+ memset (& hints , 0 , sizeof (hints ));
64
+ hints .ai_family = AF_UNSPEC ;
65
+ hints .ai_socktype = SOCK_STREAM ;
66
+ hints .ai_protocol = IPPROTO_TCP ;
67
+
68
+ /* Resolve the server address and port */
69
+ _snprintf (port_str , 20 , "%d" , port );
70
+ rc = getaddrinfo (ipaddress , port_str , & hints , & result );
71
+ if (rc != NO_ERROR ) {
72
+ free (* tcpip );
73
+ * tcpip = NULL ;
74
+ WSACleanup ();
75
+ ModelicaFormatError ("MDDTCPIPSocket.h: getaddrinfo failed with error: %d\n" , rc );
76
+ }
77
+
78
+ /* Attempt to connect to an address until one succeeds */
79
+ for (ptr = result ; ptr != NULL ; ptr = ptr -> ai_next ) {
80
+
81
+ /* Create a SOCKET for connecting to server */
82
+ (* tcpip )-> SocketID = socket (ptr -> ai_family , ptr -> ai_socktype , ptr -> ai_protocol );
83
+ if ((* tcpip )-> SocketID == INVALID_SOCKET ) {
84
+ free (* tcpip );
85
+ * tcpip = NULL ;
86
+ rc = WSAGetLastError ();
87
+ WSACleanup ();
88
+ ModelicaFormatError ("MDDTCPIPSocket.h: socket failed with error: %ld\n" , WSAGetLastError ());
89
+ }
90
+
91
+ /* Connect to server */
92
+ rc = connect ((* tcpip )-> SocketID , ptr -> ai_addr , (int )ptr -> ai_addrlen );
93
+ if (rc == SOCKET_ERROR ) {
94
+ closesocket ((* tcpip )-> SocketID );
95
+ (* tcpip )-> SocketID = INVALID_SOCKET ;
96
+ continue ;
97
+ }
98
+ break ;
99
+ }
100
+
101
+ freeaddrinfo (result );
102
+
103
+ if ((* tcpip )-> SocketID == INVALID_SOCKET ) {
104
+ free (* tcpip );
105
+ * tcpip = NULL ;
106
+ WSACleanup ();
107
+ ModelicaFormatError ("MDDTCPIPSocket.h: Unable to connect to server!\n" );
108
+ }
109
+
110
+ ret = 1 ;
111
+ }
112
+ else {
113
+ ret = 1 ;
80
114
}
81
- break ;
82
- }
83
-
84
- freeaddrinfo (result );
85
-
86
- if (tcpip -> SocketID == INVALID_SOCKET ) {
87
- free (tcpip );
88
- tcpip = NULL ;
89
- WSACleanup ();
90
- ModelicaFormatError ("MDDTCPIPSocket.h: Unable to connect to server!\n" );
91
115
}
92
-
93
- return (void * ) tcpip ;
116
+ return ret ;
94
117
}
95
118
96
119
DllExport void MDD_TCPIPClient_Destructor (void * p_tcpip ) {
97
- MDDTCPIPSocket * tcpip = (MDDTCPIPSocket * ) p_tcpip ;
120
+ MDDTCPIPSocket * * tcpip = (MDDTCPIPSocket * * ) p_tcpip ;
98
121
if (tcpip ) {
99
- shutdown (tcpip -> SocketID , SD_BOTH );
100
- closesocket (tcpip -> SocketID );
122
+ if (* tcpip ) {
123
+ if ((* tcpip )-> SocketID != INVALID_SOCKET ) {
124
+ shutdown ((* tcpip )-> SocketID , SD_BOTH );
125
+ closesocket ((* tcpip )-> SocketID );
126
+ }
127
+ free (* tcpip );
128
+ WSACleanup ();
129
+ }
101
130
free (tcpip );
102
131
}
103
- WSACleanup ();
104
132
}
105
133
106
134
DllExport int MDD_TCPIPClient_Send (void * p_tcpip , const char * data , int dataSize ) {
107
- MDDTCPIPSocket * tcpip = (MDDTCPIPSocket * ) p_tcpip ;
135
+ MDDTCPIPSocket * * tcpip = (MDDTCPIPSocket * * ) p_tcpip ;
108
136
int rc = EXIT_SUCCESS ;
109
- if (tcpip ) {
110
- rc = send (tcpip -> SocketID , data , dataSize , 0 );
137
+ if (tcpip && * tcpip ) {
138
+ rc = send (( * tcpip ) -> SocketID , data , dataSize , 0 );
111
139
if (rc == SOCKET_ERROR ) {
112
140
ModelicaFormatMessage ("MDDTCPIPSocket.h: send failed with error: %d\n" , WSAGetLastError ());
113
141
rc = EXIT_FAILURE ;
@@ -117,10 +145,10 @@ DllExport int MDD_TCPIPClient_Send(void * p_tcpip, const char * data, int dataSi
117
145
}
118
146
119
147
DllExport const char * MDD_TCPIPClient_Read (void * p_tcpip , int recvbuflen ) {
120
- MDDTCPIPSocket * tcpip = (MDDTCPIPSocket * ) p_tcpip ;
121
- if (tcpip ) {
148
+ MDDTCPIPSocket * * tcpip = (MDDTCPIPSocket * * ) p_tcpip ;
149
+ if (tcpip && * tcpip ) {
122
150
char * recvbuf = ModelicaAllocateString (recvbuflen );
123
- int rc = recv (tcpip -> SocketID , recvbuf , recvbuflen , 0 );
151
+ int rc = recv (( * tcpip ) -> SocketID , recvbuf , recvbuflen , 0 );
124
152
return recvbuf ;
125
153
}
126
154
return "" ;
0 commit comments