-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModbusUDP_WinSock.cpp
More file actions
197 lines (166 loc) · 6.1 KB
/
ModbusUDP_WinSock.cpp
File metadata and controls
197 lines (166 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//---------------------------------------------------------------------------
#pragma hdrstop
#include <algorithm>
#include "ModbusUDP_WinSock.h"
using std::min;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma comment( lib, "ws2_32" )
//---------------------------------------------------------------------------
namespace Modbus {
//---------------------------------------------------------------------------
namespace Master {
//---------------------------------------------------------------------------
UDPProtocolWinSock::UDPProtocolWinSock( String Host, uint16_t Port )
: host_( Host ), port_( Port ), socket_( INVALID_SOCKET )
, serverAddrLen_( 0 )
, recvBufferPos_( 0 ), recvBufferSize_( 0 )
{
ZeroMemory( &serverAddr_, sizeof( serverAddr_ ) );
recvBuffer_.Length = 2048;
WSADATA wsaData;
if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != 0 ) {
throw EBaseException( _D( "UDP: WSAStartup failed" ) );
}
}
//---------------------------------------------------------------------------
UDPProtocolWinSock::~UDPProtocolWinSock()
{
try {
DoClose();
WSACleanup();
}
catch ( ... ) {
}
}
//---------------------------------------------------------------------------
String UDPProtocolWinSock::DoGetHost() const
{
return host_;
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoSetHost( String Val )
{
host_ = Val;
}
//---------------------------------------------------------------------------
uint16_t UDPProtocolWinSock::DoGetPort() const noexcept
{
return port_;
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoSetPort( uint16_t Val )
{
port_ = Val;
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoOpen()
{
DoClose();
ADDRINFOW hints = {};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
ADDRINFOW* result = nullptr;
if ( GetAddrInfoW( host_.c_str(), String( port_ ).c_str(), &hints, &result ) != 0 ) {
throw EBaseException( _D( "UDP: GetAddrInfoW failed" ) );
}
SOCKET sock = INVALID_SOCKET;
for ( ADDRINFOW* ptr = result; ptr != nullptr; ptr = ptr->ai_next ) {
sock = ::socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
if ( sock == INVALID_SOCKET )
continue;
// Store resolved server address for sendto — guard against oversized entries
if ( ptr->ai_addrlen > sizeof( serverAddr_ ) ) {
closesocket( sock );
sock = INVALID_SOCKET;
continue;
}
memcpy( &serverAddr_, ptr->ai_addr, ptr->ai_addrlen );
serverAddrLen_ = static_cast<int>( ptr->ai_addrlen );
break;
}
FreeAddrInfoW( result );
if ( sock == INVALID_SOCKET ) {
throw EBaseException( _D( "UDP: socket creation failed" ) );
}
socket_ = sock;
// 2 second receive timeout
DWORD readTimeout = 2000;
setsockopt( socket_, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*>( &readTimeout ), sizeof( readTimeout ) );
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoClose()
{
if ( socket_ != INVALID_SOCKET ) {
closesocket( socket_ );
socket_ = INVALID_SOCKET;
}
}
//---------------------------------------------------------------------------
bool UDPProtocolWinSock::DoIsConnected() const noexcept
{
return socket_ != INVALID_SOCKET;
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoInputBufferClear()
{
recvBufferPos_ = 0;
recvBufferSize_ = 0;
// Drain any pending datagrams without blocking
u_long available = 0;
ioctlsocket( socket_, FIONREAD, &available );
while ( available > 0 ) {
char buf[2048];
sockaddr_storage from;
int fromLen = sizeof( from );
recvfrom( socket_, buf, sizeof( buf ), 0,
reinterpret_cast<sockaddr*>( &from ), &fromLen );
ioctlsocket( socket_, FIONREAD, &available );
}
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoWrite( TBytes const OutBuffer )
{
#if defined( _DEBUG )
DebugBytesToHex( _D( "UDP TX: " ), OutBuffer );
#endif
const char* data = reinterpret_cast<const char*>( &OutBuffer[0] );
int length = OutBuffer.Length;
if ( sendto( socket_, data, length, 0,
reinterpret_cast<const sockaddr*>( &serverAddr_ ),
serverAddrLen_ ) == SOCKET_ERROR ) {
throw EBaseException( _D( "UDP: sendto failed" ) );
}
// Receive the response immediately — mirrors the Indy pattern
recvBuffer_.Length = 2048;
sockaddr_storage from;
int fromLen = sizeof( from );
int received = recvfrom( socket_,
reinterpret_cast<char*>( &recvBuffer_[0] ),
recvBuffer_.Length, 0,
reinterpret_cast<sockaddr*>( &from ), &fromLen );
if ( received == SOCKET_ERROR ) {
throw EBaseException( _D( "UDP: recvfrom failed (timeout?)" ) );
}
recvBufferPos_ = 0;
recvBufferSize_ = received;
}
//---------------------------------------------------------------------------
void UDPProtocolWinSock::DoRead( TBytes & InBuffer, size_t Length )
{
if ( recvBufferSize_ - recvBufferPos_ < static_cast<int>( Length ) ) {
throw EBaseException( _D( "UDP read timeout" ) );
}
InBuffer = recvBuffer_.CopyRange( recvBufferPos_, Length );
recvBufferPos_ += static_cast<int>( Length );
#if defined( _DEBUG )
DebugBytesToHex( _D( "UDP RX: " ), InBuffer );
#endif
}
//---------------------------------------------------------------------------
}; // End of namespace Master
//---------------------------------------------------------------------------
}; // End of namespace Modbus
//---------------------------------------------------------------------------