2014-05-08 16:44:01 +02:00
|
|
|
|
/*@!Encoding:1252*/
|
|
|
|
|
includes
|
|
|
|
|
{
|
|
|
|
|
#include "Common.cin"
|
|
|
|
|
#include "TcpUdpCommon.cin"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
variables
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
UdpSocket gSocket;
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
byte gRxBuffer[8192];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
word UdpOpenSocket()
|
|
|
|
|
{
|
|
|
|
|
dword i = 0;
|
|
|
|
|
CHAR errorText[200];
|
|
|
|
|
|
2014-05-21 12:55:57 +02:00
|
|
|
|
if (EthGetAdapterStatus() != 2) // Not connected
|
|
|
|
|
{
|
|
|
|
|
writeLineEx(0, 3, "<%NODE_NAME%> Error: Adapter status not ok: %d!", EthGetAdapterStatus());
|
2014-05-08 16:44:01 +02:00
|
|
|
|
return INVALID_IP;
|
2014-05-21 12:55:57 +02:00
|
|
|
|
}
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
// Try to open socket
|
|
|
|
|
do
|
|
|
|
|
{
|
2014-05-21 12:55:57 +02:00
|
|
|
|
gSocket = UdpSocket::Open(0, 0); // Open socket on any IP and Port
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocket.GetLastSocketError() != 0)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
|
2014-05-21 12:55:57 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Udp socket: %s (%d)!", errorText, gSocket.GetLastSocketError());
|
2014-05-08 16:44:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 15:49:19 +02:00
|
|
|
|
while (gSocket.GetLastSocketError() != 0 && i++ < 9);
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocket.GetLastSocketError() != 0)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Udp socket!");
|
2014-05-09 15:49:19 +02:00
|
|
|
|
return gSocket.GetLastSocketError();
|
2014-05-08 16:44:01 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-21 12:55:57 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Udp socket opened.");
|
2014-05-08 16:44:01 +02:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
word UdpConnectTo(char Remote_IP[], word remotePort)
|
|
|
|
|
{
|
|
|
|
|
dword remoteIp;
|
|
|
|
|
|
|
|
|
|
// Convert IP string to Number
|
|
|
|
|
remoteIp = IpGetAddressAsNumber(Remote_IP);
|
|
|
|
|
if (remoteIp == INVALID_IP)
|
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Error: invalid server Ip address!");
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return UdpConnectTo(remoteIp, remotePort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
word UdpConnectTo(dword remoteIp, word remotePort)
|
|
|
|
|
{
|
|
|
|
|
long fehler;
|
|
|
|
|
|
|
|
|
|
// Try to open a socket
|
|
|
|
|
fehler = UdpOpenSocket();
|
|
|
|
|
if (fehler != 0)
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = ERROR;
|
2014-05-08 16:44:01 +02:00
|
|
|
|
return fehler;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gRemoteIP = remoteIp;
|
|
|
|
|
gRemotePort = remotePort;
|
|
|
|
|
gSocketState = OK;
|
2014-05-08 16:44:01 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpRecv()
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocketState != OK)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
writeLineEx(0, 2, "UdpRecv: Socket status is not OK!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
result = gSocket.ReceiveFrom(gRxBuffer, elcount(gRxBuffer));
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
if (result != 0) // Calling OnUdpReceive otherwise
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gIpLastErr = gSocket.GetLastSocketError();
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
if (gIpLastErr != WSA_IO_PENDING) // Calling OnUdpReceive otherwise
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> UdpRecv Error (%d): %s", gIpLastErr, gIpLastErrStr);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.Close();
|
|
|
|
|
gSocketState = CLOSED;
|
2014-05-08 16:44:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 10:46:53 +02:00
|
|
|
|
void UdpSnd(byte buffer[], word length)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
2014-05-15 14:43:52 +02:00
|
|
|
|
//char str[20*3];
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
switch (gSocketState)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
case CLOSED:
|
2014-05-09 15:49:19 +02:00
|
|
|
|
UdpConnectTo(gRemoteIP, gRemotePort);
|
|
|
|
|
if (gSocketState != OK)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
writeLineEx(0, 2, "UdpSnd: Reconnecting failed!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case OK:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
writeLineEx(0, 2, "UdpSnd: Socket status is not OK!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 14:43:52 +02:00
|
|
|
|
//bin_to_strhex(buffer, str);
|
2014-05-15 17:05:20 +02:00
|
|
|
|
//writeLineEx(0, 1, "<%NODE_NAME%> UdpSnd: %s (L<>nge: %d)", str, length);
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
2014-05-12 10:46:53 +02:00
|
|
|
|
if (gSocket.SendTo(gRemoteIP, gRemotePort, buffer, length) != 0)
|
2014-05-08 16:44:01 +02:00
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gIpLastErr = gSocket.GetLastSocketError();
|
2014-05-08 16:44:01 +02:00
|
|
|
|
|
|
|
|
|
if (gIpLastErr != WSA_IO_PENDING)
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> UdpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.Close();
|
|
|
|
|
gSocketState = CLOSED;
|
2014-05-08 16:44:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|