187 lines
No EOL
4.4 KiB
Text
187 lines
No EOL
4.4 KiB
Text
/*@!Encoding:1252*/
|
||
includes
|
||
{
|
||
#include "Common.cin"
|
||
#include "TcpUdpCommon.cin"
|
||
}
|
||
|
||
variables
|
||
{
|
||
TcpSocket g_%NODE_NAME%_Socket;
|
||
|
||
byte gRxBuffer[8192];
|
||
}
|
||
|
||
|
||
word TcpOpenSocket()
|
||
{
|
||
char Local_IP[16];
|
||
dword localIp;
|
||
word localPort;
|
||
dword i = 0;
|
||
CHAR errorText[200];
|
||
|
||
localIp = SetupIp(Local_IP);
|
||
localPort = random(65535-10240)+10240;
|
||
|
||
if (localIp == INVALID_IP)
|
||
return INVALID_IP;
|
||
|
||
// Try to open socket
|
||
do
|
||
{
|
||
g_%NODE_NAME%_Socket = TcpSocket::Open(localIp, localPort);
|
||
if (g_%NODE_NAME%_Socket.GetLastSocketError() != 0)
|
||
{
|
||
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(errorText, elcount(errorText));
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, g_%NODE_NAME%_Socket.GetLastSocketError());
|
||
}
|
||
}
|
||
while (g_%NODE_NAME%_Socket.GetLastSocketError() != 0 && i++ < 9);
|
||
|
||
if (g_%NODE_NAME%_Socket.GetLastSocketError() != 0)
|
||
{
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Tcp socket!");
|
||
return g_%NODE_NAME%_Socket.GetLastSocketError();
|
||
}
|
||
else
|
||
{
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Tcp socket opened on %s:%d.", Local_IP, localPort);
|
||
}
|
||
return 0;
|
||
|
||
}
|
||
|
||
word TcpConnectTo(char Remote_IP[], word remotePort)
|
||
{
|
||
dword remoteIp;
|
||
|
||
// Convert IP string to Number
|
||
remoteIp = IpGetAddressAsNumber(Remote_IP);
|
||
if (remoteIp == INVALID_IP)
|
||
{
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Error: invalid server Ip address!");
|
||
|
||
return 1;
|
||
}
|
||
|
||
return TcpConnectTo(remoteIp, remotePort);
|
||
}
|
||
|
||
word TcpConnectTo(dword remoteIp, word remotePort)
|
||
{
|
||
long fehler;
|
||
|
||
// Try to open a socket
|
||
fehler = TcpOpenSocket();
|
||
if (fehler != 0)
|
||
{
|
||
g_%NODE_NAME%_SocketState = ERROR;
|
||
return fehler;
|
||
}
|
||
|
||
g_%NODE_NAME%_RemoteIP = remoteIp;
|
||
g_%NODE_NAME%_RemotePort = remotePort;
|
||
|
||
|
||
// Connect to Server
|
||
if (g_%NODE_NAME%_Socket.Connect(remoteIp, remotePort) != 0)
|
||
{
|
||
fehler = g_%NODE_NAME%_Socket.GetLastSocketError();
|
||
|
||
if (fehler != WSAEWOULDBLOCK) // OnTcpConnect will be called otherwise
|
||
{
|
||
write("<%NODE_NAME%> No Port-Connection: %d", fehler);
|
||
g_%NODE_NAME%_SocketState = ERROR;
|
||
return fehler;
|
||
}
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
|
||
g_%NODE_NAME%_SocketState = OK;
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
void OnTcpConnect(dword socket, long result)
|
||
{
|
||
if (result != 0)
|
||
{
|
||
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
||
writeLineEx(0, 2, "<%NODE_NAME%> OnTcpConnect error (%d): %s", g_%NODE_NAME%_Socket.GetLastSocketError(), gIpLastErrStr);
|
||
g_%NODE_NAME%_SocketState = ERROR;
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
writeLineEx(0, 1, "<%NODE_NAME%> Successfully connected to server");
|
||
g_%NODE_NAME%_SocketState = OK;
|
||
}
|
||
}
|
||
|
||
void TcpRecv()
|
||
{
|
||
int result;
|
||
|
||
if (g_%NODE_NAME%_SocketState != OK)
|
||
{
|
||
writeLineEx(0, 2, "TcpRecv: Socket status is not OK!");
|
||
return;
|
||
}
|
||
|
||
result = g_%NODE_NAME%_Socket.Receive(gRxBuffer, elcount(gRxBuffer));
|
||
|
||
if (result != 0) // Calling OnTcpReceive otherwise
|
||
{
|
||
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
|
||
|
||
if (gIpLastErr != WSA_IO_PENDING) // Calling OnTcpReceive otherwise
|
||
{
|
||
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
||
writeLineEx(0, 2, "<%NODE_NAME%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||
g_%NODE_NAME%_Socket.Close();
|
||
g_%NODE_NAME%_SocketState = CLOSED;
|
||
}
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
void TcpSnd(byte buffer[])
|
||
{
|
||
char str[20*3];
|
||
|
||
switch (g_%NODE_NAME%_SocketState)
|
||
{
|
||
case CLOSED:
|
||
TcpConnectTo(g_%NODE_NAME%_RemoteIP, g_%NODE_NAME%_RemotePort);
|
||
if (g_%NODE_NAME%_SocketState != OK)
|
||
{
|
||
writeLineEx(0, 2, "TcpSnd: Reconnecting failed!");
|
||
return;
|
||
}
|
||
case OK:
|
||
break;
|
||
default:
|
||
writeLineEx(0, 2, "TcpSnd: Socket status is not OK!");
|
||
return;
|
||
}
|
||
|
||
bin_to_strhex(buffer, str);
|
||
writeLineEx(0, 1, "<%NODE_NAME%> TcpSnd: %s (L<>nge: %d)", str, elCount(buffer));
|
||
|
||
if (g_%NODE_NAME%_Socket.Send(buffer, elCount(buffer)) != 0)
|
||
{
|
||
gIpLastErr = g_%NODE_NAME%_Socket.GetLastSocketError();
|
||
|
||
if (gIpLastErr != WSA_IO_PENDING)
|
||
{
|
||
g_%NODE_NAME%_Socket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
||
writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||
g_%NODE_NAME%_Socket.Close();
|
||
g_%NODE_NAME%_SocketState = CLOSED;
|
||
}
|
||
}
|
||
} |