condTimedWait fixed
[public/netxms.git] / src / server / tools / nxadm / comm.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Local administration tool
4 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: comm.cpp
21 **
22 **/
23
24 #include "nxadm.h"
25
26
27 //
28 // Global data
29 //
30
31 SOCKET g_hSocket = -1;
32
33
34 //
35 // Connect to server
36 //
37
38 BOOL Connect(void)
39 {
40 struct sockaddr_in sa;
41
42 // Create socket
43 g_hSocket = socket(AF_INET, SOCK_STREAM, 0);
44 if (g_hSocket == -1)
45 {
46 printf("Error creating socket\n");
47 return FALSE;
48 }
49
50 // Fill in address structure
51 memset(&sa, 0, sizeof(sa));
52 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
53 sa.sin_family = AF_INET;
54 sa.sin_port = htons(LOCAL_ADMIN_PORT);
55
56 // Connect to server
57 if (connect(g_hSocket, (struct sockaddr *)&sa, sizeof(sa)) == -1)
58 {
59 printf("Cannot establish connection with server\n");
60 closesocket(g_hSocket);
61 g_hSocket = -1;
62 return FALSE;
63 }
64
65 return TRUE;
66 }
67
68
69 //
70 // Disconnect from server
71 //
72
73 void Disconnect(void)
74 {
75 if (g_hSocket != -1)
76 {
77 shutdown(g_hSocket, 2);
78 closesocket(g_hSocket);
79 g_hSocket = -1;
80 }
81 }
82
83
84 //
85 // Send command to server
86 //
87
88 BOOL SendCommand(WORD wCmd)
89 {
90 return SendEx(g_hSocket, (char *)&wCmd, sizeof(WORD), 0) == sizeof(WORD);
91 }
92
93
94 //
95 // Receive responce from server
96 //
97
98 WORD RecvResponce(void)
99 {
100 WORD wResp;
101
102 return (recv(g_hSocket, (char *)&wResp, sizeof(WORD), 0) == sizeof(WORD)) ? wResp : LA_RESP_ERROR;
103 }
104
105
106 //
107 // Send string to server
108 //
109
110 BOOL SendString(char *szString)
111 {
112 WORD wLen;
113
114 wLen = strlen(szString);
115 if (SendEx(g_hSocket, (char *)&wLen, sizeof(WORD), 0) != 2)
116 return FALSE;
117
118 return SendEx(g_hSocket, szString, wLen, 0) == wLen;
119 }
120
121
122 //
123 // Receive string from client
124 //
125
126 int RecvString(char *pBuffer, int iBufSize)
127 {
128 int iError;
129 WORD wSize = 0;
130
131 // Receive string length
132 iError = recv(g_hSocket, (char *)&wSize, 2, 0);
133
134 if (wSize == 0xFFFF)
135 return 1; // Server responds with an error instead of string
136
137 if ((iError != 2) || (wSize > iBufSize - 1))
138 return -1; // Comm error
139
140 iError = recv(g_hSocket, pBuffer, wSize, 0);
141 if (iError != wSize)
142 return -1;
143 pBuffer[iError] = 0;
144 return 0;
145 }
146
147
148 //
149 // Receive double word from server
150 //
151
152 BOOL RecvDWord(DWORD *pBuffer)
153 {
154 return recv(g_hSocket, (char *)pBuffer, sizeof(DWORD), 0) == sizeof(DWORD);
155 }
156
157
158 //
159 // Send double word to server
160 //
161
162 BOOL SendDWord(DWORD dwValue)
163 {
164 return SendEx(g_hSocket, (char *)&dwValue, sizeof(DWORD), 0) == sizeof(DWORD);
165 }