1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* Copyright (C) 2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
* @file myx_grt_module_messaging.c
* @brief GRT module messaging related functions.
*
* See also: <a href="../grt.html#ModuleMessaging">Module Messaging</a>
*/
#include "myx_grt_private.h"
#include <errno.h>
# include <time.h>
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
# define MSG_DONTWAIT 0
#else
# include <sys/types.h>
# include <sys/time.h>
# include <sys/select.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/fcntl.h>
# include <netdb.h>
# include <unistd.h>
# define closesocket close
#endif
int myx_grt_setup_messaging(MYX_GRT *grt)
{
struct sockaddr_in addr;
grt->comm_server_sock= socket(PF_INET, SOCK_STREAM, 0);
for (;;)
{
grt->comm_server_port= 2000+rand()%60000;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(grt->comm_server_port);
memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
if (bind(grt->comm_server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
if (errno == WSAEADDRINUSE)
continue;
#else
if (errno == EADDRINUSE) // retry with another port
continue;
#endif
g_error("could not bind socket: %s", g_strerror(errno));
closesocket(grt->comm_server_sock);
return -1;
}
else
break;
}
if (listen(grt->comm_server_sock, 5) < 0)
{
g_error("could not listen on socket: %s", g_strerror(errno));
closesocket(grt->comm_server_sock);
return -1;
}
return 0;
}
/**
****************************************************************************
* @brief Prepares the GRT to call a module that will perform messaging.
*
* @param grt - the GRT environment. There should be no other messaging sessions
* enabled in the GRT.
*
* @return 0 if ok, -1 on error.
*****************************************************************************/
int myx_grt_setup_module_messaging(MYX_GRT *grt)
{
g_return_val_if_fail(grt->comm_cookie==NULL, -1);
srand(time(NULL));
grt->comm_cookie= g_strdup_printf("%i%p", rand(), grt);
grt->comm_sock= -1;
return 0;
}
/**
****************************************************************************
* @brief Cleans up module messaging related state in the GRT.
*
* Should be called after a module that performs messaging is called.
*
* @param grt - the GRT environment
*
* @return 0
*****************************************************************************/
int myx_grt_cleanup_module_messaging(MYX_GRT *grt)
{
g_free(grt->comm_cookie);
grt->comm_cookie= NULL;
closesocket(grt->comm_sock);
grt->comm_sock= -1;
return 0;
}
/**
****************************************************************************
* @brief Returns the file descriptor used for the messaging socket.
*
* Use this to retrieve a file descriptor suitable for use with select(),
* to verify whether a module has connected to the socket and
* myx_grt_check_module_connected() should be called.
*
* You don't need this function unless you want to poll for several file
* descriptors at once or other specific uses.
*
* @param grt
*
* @return A file descriptor.
*****************************************************************************/
MYX_SOCKET myx_grt_get_messaging_fd(MYX_GRT *grt)
{
return grt->comm_server_sock;
}
/**
****************************************************************************
* @brief Returns the file descriptor used in the current messaging session.
*
* The returned file descriptor is a socket with an established connection
* to the module.
*
* You usually don't need this function and should use
* myx_grt_send_module_message() and myx_grt_check_module_message() to
* send and receive data from the moduole.
*
* @param grt
*
* @return The file descriptor or -1 if there's no current messaging session.
*****************************************************************************/
MYX_SOCKET myx_grt_get_messaging_module_fd(MYX_GRT *grt)
{
return grt->comm_sock;
}
/**
****************************************************************************
* @brief Checks whether the module function has already connected to the GRT.
*
* This should be called by the main program after the
* myx_grt_setup_module_messaging() and the module function call has been
* issued (in a secondary thread). If it returns 1, you should be able
* to communicate with the module using myx_grt_send_module_message() and
* myx_grt_check_module_message()
*
* @param grt - grt environment where myx_grt_setup_module_messaging has been
* called before.
*
* @return 0 if the module still has not connected
* @return 1 if the module has connected
* @return -1 if there was an error
*****************************************************************************/
int myx_grt_check_module_connected(MYX_GRT *grt)
{
fd_set rfd;
MYX_SOCKET rc;
struct timeval timeout;
struct sockaddr_in addr;
int addrlen;
char *msg;
g_return_val_if_fail(grt->comm_cookie!=NULL, -1);
if (grt->comm_sock >= 0)
return 1;
FD_ZERO(&rfd);
FD_SET(grt->comm_server_sock, &rfd);
timeout.tv_sec= 0;
timeout.tv_usec= 0;
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
if ((rc= select(0, &rfd, NULL, NULL, &timeout)) < 0)
return -1;
#else
if ((rc= select(grt->comm_server_sock+1, &rfd, NULL, NULL, &timeout)) < 0)
return -1;
#endif
if (rc == 0)
return 0;
if ((grt->comm_sock= accept(grt->comm_server_sock, (struct sockaddr*)&addr, &addrlen)) < 0)
return 0;
msg= NULL;
// wait for auth cookie, timeout in 10seconds
if (myx_grt_check_module_message(grt, &msg, 10000) < 0
|| !msg || strcmp(msg, grt->comm_cookie)!=0)
{
g_free(msg);
closesocket(grt->comm_sock);
grt->comm_sock= -1;
return 0;
}
return 1;
}
/**
****************************************************************************
* @brief Checks for messages from the currently active module function call.
*
* @param grt
* @param message output parameter where the message will be placed (or NULL)
* Should be a zero-terminated UTF-8 string.
* @param timeout number of milliseconds to wait for the message. If -1 it will
* block until a message is received (not recommended).
*
* @return -1 on error, 0 on success.
*****************************************************************************/
int myx_grt_check_module_message(MYX_GRT *grt, char **message, int timeout_ms)
{
fd_set rfd;
int rc;
struct timeval timeout;
char *tmp;
int tmp_pos;
int tmp_size= 32;
g_return_val_if_fail(grt->comm_sock >= 0, -1);
FD_ZERO(&rfd);
FD_SET(grt->comm_server_sock, &rfd);
*message= NULL;
if (timeout_ms >= 0)
{
timeout.tv_sec= timeout_ms/1000;
timeout.tv_usec= (timeout_ms%1000)*10;
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
if ((rc= select(0, &rfd, NULL, NULL, &timeout)) < 0)
return -1;
#else
if ((rc= select(grt->comm_sock+1, &rfd, NULL, NULL, &timeout)) < 0)
return -1;
#endif
if (rc == 0)
return 0;
}
tmp_pos= 0;
tmp= g_malloc(tmp_size);
for (;;)
{
int c;
if (tmp_pos == tmp_size)
{
tmp_size+= 32;
tmp= g_realloc(tmp, tmp_size);
}
c= recv(grt->comm_sock, tmp+tmp_pos, 1, 0);
if (c <= 0)
{
rc= -1;
break;
}
if (tmp[tmp_pos] == 0)
{
rc= 0;
break;
}
tmp_pos+= c;
}
*message= tmp;
return rc;
}
/**
****************************************************************************
* @brief Sends a message to the current active module function call.
*
* @param grt
* @param message Message string to be passed to the module.
* Should be a zero terminated UTF-8 string.
*
* @return 0 if ok, -1 on error.
*****************************************************************************/
int myx_grt_send_module_message(MYX_GRT *grt, const char *message)
{
g_return_val_if_fail(grt->comm_sock >= 0, -1);
if (send(grt->comm_sock, message, (int)strlen(message)+1, 0) < 0)
return -1;
return 0;
}
|