2014-05-07 12:13:24 +02:00
|
|
|
|
/*@!Encoding:1252*/
|
|
|
|
|
includes
|
|
|
|
|
{
|
|
|
|
|
#include "Common.cin"
|
2014-05-08 16:44:01 +02:00
|
|
|
|
#include "TcpUdpCommon.cin"
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
variables
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
TcpSocket gSocket;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-05-08 10:26:26 +02:00
|
|
|
|
word TcpOpenSocket()
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
|
|
|
|
char Local_IP[16];
|
|
|
|
|
dword localIp;
|
|
|
|
|
word localPort;
|
|
|
|
|
dword i = 0;
|
|
|
|
|
CHAR errorText[200];
|
|
|
|
|
|
|
|
|
|
// Try to open socket
|
|
|
|
|
do
|
|
|
|
|
{
|
2014-05-21 13:26:45 +02:00
|
|
|
|
gSocket = TcpSocket::Open(0, 0);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocket.GetLastSocketError() != 0)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
|
2014-05-21 13:26:45 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket: %s (%d)!", errorText, gSocket.GetLastSocketError());
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 15:49:19 +02:00
|
|
|
|
while (gSocket.GetLastSocketError() != 0 && i++ < 9);
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocket.GetLastSocketError() != 0)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket!");
|
2014-05-09 15:49:19 +02:00
|
|
|
|
return gSocket.GetLastSocketError();
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-21 13:26:45 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Tcp socket opened.");
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
word TcpConnectTo(char Remote_IP[], word remotePort)
|
|
|
|
|
{
|
|
|
|
|
dword remoteIp;
|
2014-05-08 15:34:28 +02:00
|
|
|
|
|
|
|
|
|
// 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 15:34:28 +02:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TcpConnectTo(remoteIp, remotePort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
word TcpConnectTo(dword remoteIp, word remotePort)
|
|
|
|
|
{
|
2014-05-07 12:13:24 +02:00
|
|
|
|
long fehler;
|
|
|
|
|
|
|
|
|
|
// Try to open a socket
|
2014-05-08 10:26:26 +02:00
|
|
|
|
fehler = TcpOpenSocket();
|
2014-05-07 12:13:24 +02:00
|
|
|
|
if (fehler != 0)
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = ERROR;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
return fehler;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gRemoteIP = remoteIp;
|
|
|
|
|
gRemotePort = remotePort;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Connect to Server
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocket.Connect(remoteIp, remotePort) != 0)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
fehler = gSocket.GetLastSocketError();
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
|
|
|
|
if (fehler != WSAEWOULDBLOCK) // OnTcpConnect will be called otherwise
|
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
write("<%NODE_NAME%> No Port-Connection: %d", fehler);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = ERROR;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
return fehler;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = OK;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnTcpConnect(dword socket, long result)
|
|
|
|
|
{
|
|
|
|
|
if (result != 0)
|
|
|
|
|
{
|
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%> OnTcpConnect error (%d): %s", gSocket.GetLastSocketError(), gIpLastErrStr);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = ERROR;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocketState = OK;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpRecv()
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
if (gSocketState != OK)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-08 15:34:28 +02:00
|
|
|
|
writeLineEx(0, 2, "TcpRecv: Socket status is not OK!");
|
2014-05-07 12:13:24 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
result = gSocket.Receive(gRxBuffer, elcount(gRxBuffer));
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
|
|
|
|
if (result != 0) // Calling OnTcpReceive otherwise
|
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gIpLastErr = gSocket.GetLastSocketError();
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
|
|
|
|
if (gIpLastErr != WSA_IO_PENDING) // Calling OnTcpReceive 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%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.Close();
|
|
|
|
|
gSocketState = CLOSED;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 10:46:53 +02:00
|
|
|
|
void TcpSnd(byte buffer[], word length)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-15 14:43:52 +02:00
|
|
|
|
//char str[20*3];
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
2014-05-09 15:49:19 +02:00
|
|
|
|
switch (gSocketState)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-08 10:26:26 +02:00
|
|
|
|
case CLOSED:
|
2014-05-09 15:49:19 +02:00
|
|
|
|
TcpConnectTo(gRemoteIP, gRemotePort);
|
|
|
|
|
if (gSocketState != OK)
|
2014-05-08 15:34:28 +02:00
|
|
|
|
{
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Reconnecting failed!");
|
2014-05-08 15:34:28 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2014-05-08 10:26:26 +02:00
|
|
|
|
case OK:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2014-05-15 17:05:20 +02:00
|
|
|
|
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Socket status is not OK!");
|
2014-05-08 10:26:26 +02:00
|
|
|
|
return;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
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%> TcpSnd: %s (L<>nge: %d)", str, length);
|
2014-05-07 12:13:24 +02:00
|
|
|
|
|
2014-05-12 10:46:53 +02:00
|
|
|
|
if (gSocket.Send(buffer, length) != 0)
|
2014-05-07 12:13:24 +02:00
|
|
|
|
{
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gIpLastErr = gSocket.GetLastSocketError();
|
2014-05-07 12:13:24 +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%> TcpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
|
2014-05-09 15:49:19 +02:00
|
|
|
|
gSocket.Close();
|
|
|
|
|
gSocketState = CLOSED;
|
2014-05-07 12:13:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|