------------------------------
# pragma package (smart_init)
# define IPX_SERVER_SOCKET 0x4504
# define IPX_CLIENT_SOCKET 0x4514
# define WSA_NETEVENT (WM_USER +2)
SOCKET IPXSocket;
sockaddr_ipx CallAddress;
sockaddr_ipx OurAddress;
extern bool IsServer;
extern int KolKub;
extern int MaxOch;
// ----------------------- Підключення --------------------- -----------------------
void __ fastcall TMainForm :: IPXConnect (void)
{
WSADATA WSAData;
int rc, OptVal = 1;
rc = WSAStartup (MAKEWORD (2,0), & WSAData);
if (rc! = 0)
{
Application-> MessageBox ("Помилка ініціалізації WSAStartup "," Помилка ", MB_OK + MB_ICONHAND);
Application-> Terminate ();
}
IPXSocket = socket (AF_IPX, SOCK_DGRAM, NSPROTO_IPX);
if (IPXSocket == INVALID_SOCKET)
{
Application-> MessageBox ("Протокол IPX НЕ встановлений "," Помилка ", MB_OK + MB_ICONHAND);
Application-> Terminate ();
}
memset (& OurAddress, 0, sizeof (OurAddress));
OurAddress.sa_family = AF_IPX;
if (IsServer)
OurAddress.sa_socket = htons ((unsigned short) (IPX_SERVER_SOCKET));
else
OurAddress.sa_socket = htons ((unsigned short) (IPX_CLIENT_SOCKET));
if (bind (IPXSocket, (LPSOCKADDR) & OurAddress, sizeof (sockaddr_ipx)) == SOCKET_ERROR)
{
Application-> MessageBox ("Адресна помилка IPX "," Помилка ", MB_OK + MB_ICONHAND);
closesocket (IPXSocket);
IPXSocket = INVALID_SOCKET;
Application-> Terminate ();
}
rc = setsockopt (IPXSocket, SOL_SOCKET, SO_BROADCAST, (char *) & OptVal, sizeof (OptVal));
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError ();
Application-> MessageBox ("Помилка setsockopt "," Помилка ", MB_OK + MB_ICONHAND);
closesocket (IPXSocket);
IPXSocket = INVALID_SOCKET;
Application-> Terminate ();
}
if (! IsServer)
{
memset (& CallAddress, 0, sizeof (CallAddress));
CallAddress.sa_family = AF_IPX;
CallAddress.sa_socket = htons (IPX_SERVER_SOCKET);
CallAddress.sa_nodenum [0] = 0xff;
CallAddress.sa_nodenum [1] = 0xff;
CallAddress.sa_nodenum [2] = 0xff;
CallAddress.sa_nodenum [3] = 0xff;
CallAddress.sa_nodenum [4] = 0xff;
CallAddress.sa_nodenum [5] = 0xff;
rc = sendto (IPXSocket, "Connect", 8, 0, (PSOCKADDR) & CallAddress, sizeof (SOCKADDR));
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError ();
Application-> MessageBox ("Помилка sendto IPX", "Помилка", MB_OK + MB_ICONHAND);
closesocket (IPXSocket);
IPXSocket = INVALID_SOCKET;
Application-> Terminate ();
}
}
rc = WSAAsyncSelect (IPXSocket, Handle, WSA_NETEVENT, FD_READ);
if (rc! = 0)
{
Application-> MessageBox ("Помилка WSAAsyncSelect "," Помилка ", MB_OK + MB_ICONHAND);
closesocket (IPXSocket);
IPXSocket = INVALID_SOCKET;
Application-> Terminate ();
}
WindowProc = IPXWndProc;
}
// ------------------------------ прийом -------------- -------------------------
void __ fastcall TMainForm :: IPXWndProc (Messages :: TMessage & Message)
{
int rc, l = sizeof (CallAddress);
char Buf [80];
if (Message.Msg == WSA_NETEVENT)
{
if (Message.LParam == FD_READ)
{
rc = recvfrom ((SOCKET) Message.WParam, Buf, sizeof (Buf) -1, 0, (PSOCKADDR) & CallAddress, & l);
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError ();
Application-> MessageBox ("Помилка recvfrom IPX", "Помилка", MB_OK + MB_ICONHAND);
closesocket (IPXSocket);
IPXSocket = INVALID_SOCKET;
Application-> Terminate ();
}
if (rc> = 1)
{
if (! strcmp (Buf, "connect"))
{
char * StrPered = DannieToChar (0, KolKub, MaxOch);
IPXSend (strPered);
InterfaceBeginNewGame ();
ChangeInterfacePerehodHoda (1);
}
else
{
if (IsVisitApplicationBox) return;
Priem (CharToMas (Buf));
}
}
}
}
WndProc (Message);
}
// ------------------------- відправка ------------------- --------------------------
void __ fastcall TMainForm :: IPXSend (Char * Buf)
{
int rc;
rc = sendto (IPXSocket, Buf, strlen (Buf) +1, 0, (PSOCKADDR) & CallAddress, sizeof (SOCKADDR));
В
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError ();
Application-> MessageBox ("...