| | |
| | | int rv; |
| | | int remote_port; |
| | | while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) { |
| | | sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf); |
| | | puts(sendbuf); |
| | | printf( "REGIST CENTER RECEIVED REQUEST FROM %d: %s", remote_port, recvbuf); |
| | | sprintf(sendbuf, "RECEIVED FROM %d, %s", remote_port, recvbuf); |
| | | dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port); |
| | | free(recvbuf); |
| | | } |
New file |
| | |
| | | #include "dgram_mod_socket.h" |
| | | #include "shm_mm.h" |
| | | #include "usg_common.h" |
| | | |
| | | void server(int port) { |
| | | void *socket = dgram_mod_open_socket(SURVEY); |
| | | dgram_mod_bind(socket, port); |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | int rv; |
| | | int remote_port; |
| | | while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) { |
| | | printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | dgram_mod_close_socket(socket); |
| | | } |
| | | |
| | | void client(int port) { |
| | | void *socket = dgram_mod_open_socket(SURVEY); |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | long i = 0; |
| | | while (true) { |
| | | sprintf(sendbuf, "%d", i); |
| | | printf("SEND HEART:%s\n", sendbuf); |
| | | dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port); |
| | | free(recvbuf); |
| | | sleep(1); |
| | | i++; |
| | | } |
| | | dgram_mod_close_socket(socket); |
| | | } |
| | | |
| | | |
| | | |
| | | int main(int argc, char *argv[]) { |
| | | shm_init(512); |
| | | int port; |
| | | if (argc < 3) { |
| | | fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client"); |
| | | return 1; |
| | | } |
| | | |
| | | port = atoi(argv[2]); |
| | | |
| | | if (strcmp("server", argv[1]) == 0) { |
| | | server(port); |
| | | } |
| | | |
| | | if (strcmp("client", argv[1]) == 0) |
| | | client(port); |
| | | |
| | | |
| | | return 0; |
| | | } |
| | |
| | | static inline void rm_fblock(void *rbp) |
| | | { |
| | | // the successor of the previous block of rbp point to next block of rbp |
| | | // err_msg(0, "SUCCRP(PREV_FBLKP(rbp) %p\n", SUCCRP(PREV_FBLKP(rbp)) ); |
| | | // err_msg(0, "NEXT_FBLKP(rbp)%p\n", NEXT_FBLKP(rbp) ); |
| | | |
| | | PUT_PTR(SUCCRP(PREV_FBLKP(rbp)), NEXT_FBLKP(rbp)); |
| | | // the predecessor of then next block of rbp point to previous block of rbp |
| | |
| | | int dgram_mod_close_socket(void * _socket) { |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | shm_close_socket(socket->shm_socket); |
| | | // if(socket->recv_queue_map != NULL) { |
| | | // for(auto iter = socket->recv_queue_map->begin(); iter != socket->recv_queue_map->end(); iter++) { |
| | | // delete iter->second; |
| | | // socket->recv_queue_map->erase(iter); |
| | | |
| | | // } |
| | | // delete socket->recv_queue_map; |
| | | // } |
| | | |
| | | |
| | | // if(socket->recv_thread != 0) |
| | | // pthread_cancel(socket->recv_thread); |
| | | free(_socket); |
| | | } |
| | | |
| | |
| | | int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) { |
| | | |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | // if(socket->mod == REQ_REP && socket->recv_thread != 0) { |
| | | // err_exit(0, "you have used sendandrecv method os you can not use recvfrom method any more. these two method can not be used at the same time."); |
| | | // return -1; |
| | | // } |
| | | return shm_recvfrom(socket->shm_socket, buf, size, port); |
| | | } |
| | | |
| | | // void *_dgram_mod_run_recv(void * _socket) { |
| | | // pthread_detach(pthread_self()); |
| | | // dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | // void *buf; |
| | | // int size; |
| | | // int port; |
| | | // shm_msg_t msg; |
| | | // LockFreeQueue<shm_msg_t, DM_Allocator> *queue; |
| | | // std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter; |
| | | // // printf("==============_dgram_mod_run_recv recv before\n"); |
| | | // while(shm_recvfrom(socket->shm_socket, &buf, &size, &port) == 0) { |
| | | // if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) { |
| | | // queue = iter->second; |
| | | // } else { |
| | | // queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | // socket->recv_queue_map->insert({port, queue}); |
| | | // } |
| | | |
| | | // msg.buf = buf; |
| | | // msg.size = size; |
| | | // msg.port = port; |
| | | // // printf("==============_dgram_mod_run_recv push before\n"); |
| | | // queue->push(msg); |
| | | // // printf("==============_dgram_mod_run_recv push after\n"); |
| | | |
| | | // } |
| | | // return NULL; |
| | | |
| | | |
| | | |
| | | // } |
| | | |
| | | |
| | | |
| | | |
| | | int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) { |
| | | |
| | | dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; |
| | | if(socket->mod != REQ_REP) { |
| | | err_exit(0, "you can't use this method other than REQ_REP mod!"); |
| | | } |
| | | // if(socket->recv_queue_map == NULL) { |
| | | // socket->recv_queue_map = new std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >; |
| | | // } |
| | | |
| | | // std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter; |
| | | // LockFreeQueue<shm_msg_t, DM_Allocator> *queue; |
| | | // if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) { |
| | | // queue = iter->second; |
| | | // } else { |
| | | // queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | // socket->recv_queue_map->insert({port, queue}); |
| | | // } |
| | | |
| | | // if (socket->recv_thread == 0) { |
| | | |
| | | // pthread_create(&(socket->recv_thread ), NULL, _dgram_mod_run_recv , _socket); |
| | | |
| | | // } |
| | | |
| | | // shm_msg_t msg; |
| | | // if(queue->pop(msg)) { |
| | | // *recv_buf = msg.buf; |
| | | // *recv_size = msg.size; |
| | | // return 0; |
| | | // } |
| | | |
| | | int recv_port; |
| | | int rv; |
| | | |
| | | shm_socket_t *shm_socket = shm_open_socket(SHM_SOCKET_DGRAM); |
| | | if (shm_sendto(shm_socket, send_buf, send_size, send_port) == 0) { |
| | | rv = shm_recvfrom(shm_socket, recv_buf, recv_size, &recv_port); |
| | | shm_close_socket(shm_socket); |
| | | return rv; |
| | | } |
| | | |
| | | |
| | | return -1; |
| | | return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size); |
| | | |
| | | } |
| | |
| | | |
| | | int shm_send(shm_socket_t * socket, const void *buf, const int size) ; |
| | | |
| | | int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size); |
| | | |
| | | int shm_recv(shm_socket_t * socket, void **buf, int *size) ; |
| | | |
| | | int shm_sendto(shm_socket_t *socket, const void *buf, const int size, const int port); |
| | |
| | | } |
| | | } |
| | | |
| | | int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) { |
| | | int recv_port; |
| | | int rv; |
| | | |
| | | shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); |
| | | if (shm_sendto(tmp_socket, send_buf, send_size, send_port) == 0) { |
| | | rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_port); |
| | | shm_close_socket(tmp_socket); |
| | | return rv; |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 绑定key到队列,但是并不会创建队列。如果没有对应指定key的队列提示错误并退出 |
| | |
| | | include $(ROOT)/Make.defines.$(PLATFORM) |
| | | |
| | | |
| | | PROGS = dgram_socket_test dgram_mod_req_rep |
| | | PROGS = dgram_socket_test dgram_mod_req_rep dgram_mod_survey |
| | | |
| | | |
| | | build: $(PROGS) |
| | |
| | | int rv; |
| | | int remote_port; |
| | | while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) { |
| | | printf( "收到来自%d注册信息: %s", remote_port, recvbuf); |
| | | printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | dgram_mod_close_socket(socket); |
| | |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | long i = 0; |
| | | while (true) { |
| | | printf("request: "); |
| | | scanf("%s", sendbuf); |
| | | dgram_mod_send(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &size); |
| | | printf("reply: %s\n", (char *)recvbuf); |
| | | sprintf(sendbuf, "%d", i); |
| | | printf("SEND HEART:%s\n", sendbuf); |
| | | dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port); |
| | | free(recvbuf); |
| | | sleep(1); |
| | | i++; |
| | | } |
| | | dgram_mod_close_socket(socket); |
| | | } |