Revision deb34f59 ml.c
ml.c | ||
---|---|---|
49 | 49 |
#include <errno.h> |
50 | 50 |
#include <time.h> |
51 | 51 |
#include <math.h> |
52 |
#include "msg_types.h" |
|
52 | 53 |
#include "util/udpSocket.h" |
53 | 54 |
#include "util/stun.h" |
54 | 55 |
#include "transmissionHandler.h" |
... | ... | |
1235 | 1236 |
|
1236 | 1237 |
} |
1237 | 1238 |
|
1239 |
void keepalive_fn(evutil_socket_t fd, short what, void *arg) { |
|
1240 |
socketID_handle peer = arg; |
|
1241 |
|
|
1242 |
int con_id = mlConnectionExist(peer, false); |
|
1243 |
if (con_id < 0 || connectbuf[con_id]->defaultSendParams.keepalive <= 0) { |
|
1244 |
/* Connection fell from under us or keepalive was disabled */ |
|
1245 |
free(arg); |
|
1246 |
return; |
|
1247 |
} |
|
1248 |
|
|
1249 |
/* do what we gotta do */ |
|
1250 |
if ( connectbuf[con_id]->status == READY) { |
|
1251 |
char keepaliveMsg[32] = ""; |
|
1252 |
sprintf(keepaliveMsg, "KEEPALIVE %d", connectbuf[con_id]->keepalive_seq++); |
|
1253 |
send_msg(con_id, MSG_TYPE_ML_KEEPALIVE, keepaliveMsg, 1 + strlen(keepaliveMsg), false, |
|
1254 |
&(connectbuf[con_id]->defaultSendParams)); |
|
1255 |
} |
|
1256 |
|
|
1257 |
/* re-schedule */ |
|
1258 |
struct timeval t = { 0,0 }; |
|
1259 |
t.tv_sec = time(NULL) + connectbuf[con_id]->defaultSendParams.keepalive; |
|
1260 |
|
|
1261 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t); |
|
1262 |
} |
|
1263 |
|
|
1264 |
void setupKeepalive(int conn_id) { |
|
1265 |
/* Save the peer's address for us */ |
|
1266 |
socketID_handle peer = malloc(sizeof(socket_ID)); |
|
1267 |
memcpy(peer, &connectbuf[conn_id]->external_socketID, sizeof(socket_ID)); |
|
1268 |
|
|
1269 |
struct timeval t = { 0,0 }; |
|
1270 |
t.tv_sec = time(NULL) + connectbuf[conn_id]->defaultSendParams.keepalive; |
|
1271 |
|
|
1272 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t); |
|
1273 |
} |
|
1274 |
|
|
1238 | 1275 |
/* connection functions */ |
1239 | 1276 |
int mlOpenConnection(socketID_handle external_socketID,receive_connection_cb connection_cb,void *arg, const send_params defaultSendParams){ |
1240 | 1277 |
|
... | ... | |
1257 | 1294 |
con_id = mlConnectionExist(external_socketID, false); |
1258 | 1295 |
if (con_id >= 0) { |
1259 | 1296 |
// overwrite defaultSendParams |
1297 |
bool newKeepalive = |
|
1298 |
connectbuf[con_id]->defaultSendParams.keepalive == 0 && defaultSendParams.keepalive != 0; |
|
1260 | 1299 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1300 |
if (newKeepalive) setupKeepalive(con_id); |
|
1261 | 1301 |
// if so check if it is ready to use |
1262 | 1302 |
if (connectbuf[con_id]->status == READY) { |
1263 | 1303 |
// if so use the callback immidiatley |
... | ... | |
1302 | 1342 |
connectbuf[con_id]->connection_last->connection_cb = connection_cb; |
1303 | 1343 |
connectbuf[con_id]->connection_last->arg = arg; |
1304 | 1344 |
connectbuf[con_id]->external_connectionID = -1; |
1345 |
|
|
1305 | 1346 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1347 |
if (defaultSendParams.keepalive) setupKeepalive(con_id); |
|
1306 | 1348 |
break; |
1307 | 1349 |
} |
1308 | 1350 |
} //end of for |
Also available in: Unified diff