- 论坛徽章:
- 0
|
我现在的做法是,创建一个单独的线程,然后通过socket的send/recv来发送/接收一些整数,代码如下:
- 2355 static int cma_remsync_send(node_t *node, sync_no_t sync_no)
- 2356 {
- 2357 int ret;
- 2358
- 2359 ret = send(test->conn_sock, (void *)&sync_no, sizeof (sync_no_t), 0);
- 2360 if (ret < 0) {
- 2361 ERROR("cma_remsync_send: send sync_no failed %s (%d)\n",
- 2362 strerror(errno), errno);
- 2363 return 1;
- 2364 }
- 2365
- 2366 return 0;
- 2367 }
- 2368
- 2369 static int cma_remsync_recv(node_t *node, sync_no_t sync_no)
- 2370 {
- 2371 int ret;
- 2372 sync_no_t rsync_no = SYNC_NONE;
- 2373
- 2374 ret = recv(test->conn_sock, (void *)&rsync_no, sizeof (sync_no_t), 0);
- 2375 if (ret < 0) {
- 2376 ERROR("cma_remsync_recv: recv sync_no failed %s (%d)\n",
- 2377 strerror(errno), errno);
- 2378 return 1;
- 2379 }
- 2380
- 2381 if (rsync_no != sync_no) {
- 2382 ERROR("cma_remsync_recv: recv'd sync_no (%d) isn't as expected "
- 2383 "(%d)\n", rsync_no, sync_no);
- 2384 return 1;
- 2385 }
- 2386
- 2387 return 0;
- 2388 }
- 2389
- 2390 int cma_remsync(node_t *node, sync_no_t no)
- 2391 {
- 2392 sync_no_t sync_no = no;
- 2393 int errors = 0;
- 2394
- 2395 if (test->cm_test_type == CM_TEST_SERVER) {
- 2396 errors += cma_remsync_send(node, sync_no);
- 2397 errors += cma_remsync_recv(node, sync_no);
- 2398 } else {
- 2399 errors += cma_remsync_recv(node, sync_no);
- 2400 errors += cma_remsync_send(node, sync_no);
- 2401 }
- 2402
- 2403 return errors;
- 2404 }
复制代码
sync_no_t是我定义的一组枚举变量。
但是我觉得实现不太好,首先很明显的就是客户端必须先到达各个同步点,否则服务器端的send就会失败;另外recv应该也会有个超时的问题吧。
希望大家能指点一下,如何完善上面这个函数,越简单越好。
如果觉得我这个实现方案就不对,那有没有更好的实现方法呢,或者现成的库函数(不能额外安装包)
[ 本帖最后由 wawxdyy 于 2009-7-14 18:36 编辑 ] |
|