| | |
| | | request_head = NetModSocket::decode_request_head(request_head_bs); |
| | | |
| | | // printf("server received request from host = %s:%d, key = %d, timeout=%d,\n", |
| | | request_head.host, request_head.port , request_head.key, request_head.timeout); |
| | | // request_head.host, request_head.port , request_head.key, request_head.timeout); |
| | | |
| | | if(request_head.content_length > max_buf) { |
| | | |
| | |
| | | if (rio_readn(connfd, topic_buf, request_head.topic_length) != request_head.topic_length ) { |
| | | return -1; |
| | | } |
| | | LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | // LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | response_head.key = request_head.key; |
| | |
| | | } |
| | | |
| | | /* One-time key creation function */ |
| | | void NetModSocket::_createKey_(void) |
| | | void NetModSocket::_createConnPoolKey_(void) |
| | | { |
| | | int ret; |
| | | |
| | |
| | | NetConnPool *mpool; |
| | | |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | ret = pthread_once(&once, _createKey_); |
| | | ret = pthread_once(&once, _createConnPoolKey_); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once"); |
| | | exit(1); |
| | |
| | | NetConnPool *mpool; |
| | | |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | ret = pthread_once(&once, _createKey_); |
| | | ret = pthread_once(&once, _createConnPoolKey_); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once"); |
| | | exit(1); |
| | |
| | | // 销毁threadlocal pool |
| | | static void _destroyConnPool_(void *_pool); |
| | | // 创建thread local key |
| | | static void _createKey_(void); |
| | | static void _createConnPoolKey_(void); |
| | | |
| | | //读取返回信息 |
| | | int read_response(int clientfd, net_mod_recv_msg_t *recv_msg); |
| | |
| | | } |
| | | } |
| | | |
| | | static pthread_once_t _once_ = PTHREAD_ONCE_INIT; |
| | | static pthread_key_t _tmp_recv_socket_key_; |
| | | |
| | | /* Free thread-specific data buffer */ |
| | | static void _destrory_tmp_recv_socket_(void *tmp_socket) |
| | | { |
| | | logger->debug("%d destroy tmp socket\n", pthread_self()); |
| | | shm_close_socket((shm_socket_t *)tmp_socket); |
| | | } |
| | | |
| | | /* One-time key creation function */ |
| | | static void _create_tmp_recv_socket_key(void) |
| | | { |
| | | int s; |
| | | |
| | | /* Allocate a unique thread-specific data key and save the address |
| | | of the destructor for thread-specific data buffers */ |
| | | s = pthread_key_create(&_tmp_recv_socket_key_, _destrory_tmp_recv_socket_); |
| | | if (s != 0) { |
| | | logger->error(s, "pthread_key_create"); |
| | | abort(); /* dump core and terminate */ |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, |
| | | const int send_size, const int send_key, void **recv_buf, |
| | | int *recv_size, struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | | int rv; |
| | | |
| | | // 用thread local 保证每个线程用一个独占的socket接受对方返回的信息 |
| | | shm_socket_t *tmp_socket; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | logger->error( "shm_socket.shm_sendandrecv: Can't invoke shm_sendandrecv method in a %d type socket " |
| | | "which is not a SHM_SOCKET_DGRAM socket ", |
| | | socket->socket_type); |
| | | exit(1); |
| | | } |
| | | int recv_key; |
| | | int rv; |
| | | |
| | | shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); |
| | | |
| | | rv = pthread_once(&_once_, _create_tmp_recv_socket_key); |
| | | if (rv != 0) { |
| | | logger->error(rv, "shm_sendandrecv pthread_once"); |
| | | exit(1); |
| | | } |
| | | |
| | | tmp_socket = (shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_); |
| | | if (tmp_socket == NULL) |
| | | { |
| | | /* If first call from this thread, allocate buffer for thread, and save its location */ |
| | | logger->debug("%d create tmp socket\n", pthread_self() ); |
| | | tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); |
| | | |
| | | rv = pthread_setspecific(_tmp_recv_socket_key_, tmp_socket); |
| | | if (rv != 0) { |
| | | logger->error(rv, "shm_sendandrecv : pthread_setspecific"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | if ((rv = shm_sendto(tmp_socket, send_buf, send_size, send_key, timeout, flags)) == 0) { |
| | | rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); |
| | | shm_close_socket(tmp_socket); |
| | | |
| | | return rv; |
| | | } else { |
| | | shm_close_socket(tmp_socket); |
| | | |
| | | return rv; |
| | | } |
| | | return -1; |
| | |
| | | int rv; |
| | | int remote_port; |
| | | while ( (rv = net_mod_socket_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) { |
| | | printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf); |
| | | // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf); |
| | | sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf); |
| | | net_mod_socket_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port); |
| | | free(recvbuf); |
| | |
| | | net_node_t *node_arr = (net_node_t *) calloc(entry_arr_len, sizeof(net_node_t)); |
| | | for(i = 0; i < entry_arr_len; i++) { |
| | | property_arr_len = str_split(entry_arr[i], ":", &property_arr); |
| | | printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]); |
| | | // printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]); |
| | | node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), atoi(property_arr[2])}; |
| | | free(entry_arr[i]); |
| | | free(property_arr[1]); |
| | |
| | | PLATFORM=$(shell $(ROOT)/systype.sh) |
| | | include $(ROOT)/Make.defines.$(PLATFORM) |
| | | |
| | | #RPATH += -Wl,-rpath=${ROOT}/lib |
| | | RPATH += -Wl,-rpath=$(ROOT)/lib:$(DEST)/lib |
| | | # 开源工具包路径 |
| | | LDDIR += -L${DEST}/lib |
| | | |
| | |
| | | }Targ; |
| | | |
| | | LockFreeQueue<task_t, DM_Allocator> task_queue(128); |
| | | |
| | | void *client; |
| | | |
| | | void *worker(void *socket) { |
| | | pthread_detach(pthread_self()); |
| | |
| | | dgram_mod_close_socket(socket); |
| | | } |
| | | |
| | | void client(int port) { |
| | | void startClient(int port) { |
| | | void *socket = dgram_mod_open_socket(); |
| | | int size; |
| | | void *recvbuf; |
| | |
| | | char sendbuf[512]; |
| | | int scale = 100000; |
| | | int i; |
| | | void *socket = dgram_mod_open_socket(); |
| | | |
| | | |
| | | char filename[512]; |
| | | sprintf(filename, "test%d.tmp", targ->id); |
| | |
| | | for (i = 0; i < scale; i++) { |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | fprintf(fp, "requst:%s\n", sendbuf); |
| | | dgram_mod_sendandrecv(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize); |
| | | dgram_mod_sendandrecv(client, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize); |
| | | fprintf(fp, "reply: %s\n", (char *)recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | fclose(fp); |
| | | dgram_mod_close_socket(socket); |
| | | |
| | | return (void *)i; |
| | | } |
| | | |
| | |
| | | char sendbuf[512]; |
| | | |
| | | struct timeval start; |
| | | |
| | | client = dgram_mod_open_socket(); |
| | | |
| | | gettimeofday(&start, NULL); |
| | | for (i = 0; i < processors; i++) { |
| | | targs[i].port = port; |
| | |
| | | long diffsec = (long) (difftime/1000000); |
| | | long diffmsec = difftime - diffsec*1000000; |
| | | printf("cost: %ld sec: %ld msc\n", diffsec, diffmsec); |
| | | dgram_mod_close_socket(client); |
| | | } |
| | | |
| | | |
| | |
| | | } else if (strcmp("mclient", argv[1]) == 0) { |
| | | startClients(port); |
| | | } else if (strcmp("client", argv[1]) == 0) { |
| | | client(port); |
| | | startClient(port); |
| | | } else { |
| | | printf("input invalidate arguments\n"); |
| | | } |