| | |
| | | static void _create_tmp_recv_socket_key(void); |
| | | |
| | | // 检查key是否已经被使用,是返回0, 否返回1 |
| | | static inline int _shm_socket_check_key(shm_socket_t *socket) { |
| | | void *tmp_ptr = mm_get_by_key(socket->key); |
| | | if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | logger->error("%s. key = %d ", bus_strerror(EBUS_KEY_INUSED), socket->key); |
| | | return 0; |
| | | } |
| | | return 1; |
| | | // static int _shm_socket_check_key(shm_socket_t *socket) { |
| | | // void *tmp_ptr = mm_get_by_key(socket->key); |
| | | // if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) { |
| | | // bus_errno = EBUS_KEY_INUSED; |
| | | // logger->error("%s. key = %d ", bus_strerror(EBUS_KEY_INUSED), socket->key); |
| | | // return 0; |
| | | // } |
| | | // return 1; |
| | | // } |
| | | |
| | | // 检查key是否已经被使用, 未被使用则绑定key |
| | | static int check_and_bind_queue(shm_socket_t * sockt) { |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | hashtable_lock(hashtable); |
| | | void *tmp_ptr = mm_get_by_key(sockt->key); |
| | | if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !sockt->force_bind ) { |
| | | hashtable_unlock(hashtable); |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | sockt->queue = new SHMQueue<shm_msg_t>(sockt->key, 16); |
| | | hashtable_unlock(hashtable); |
| | | return 0; |
| | | } |
| | | |
| | | SHMQueue<shm_msg_t> *_attach_remote_queue(int key); |
| | |
| | | return 0; |
| | | } |
| | | |
| | | int shm_listen(shm_socket_t *socket) { |
| | | int shm_socket_get_key(shm_socket_t *sk){ |
| | | return sk->key; |
| | | } |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_STREAM) { |
| | | int shm_listen(shm_socket_t *sockt) { |
| | | int rv; |
| | | if (sockt->socket_type != SHM_SOCKET_STREAM) { |
| | | logger->error("can not invoke shm_listen method with a socket which is not a " |
| | | "SHM_SOCKET_STREAM socket"); |
| | | exit(1); |
| | |
| | | |
| | | int key; |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | if (socket->key == 0) { |
| | | if (sockt->key == 0) { |
| | | key = hashtable_alloc_key(hashtable); |
| | | socket->key = key; |
| | | sockt->key = key; |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | rv = check_and_bind_queue(sockt); |
| | | if(rv !=0 ) { |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | socket->acceptQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | socket->clientSocketMap = new std::map<int, shm_socket_t *>; |
| | | socket->status = SHM_CONN_LISTEN; |
| | | pthread_create(&(socket->dispatch_thread), NULL, _server_run_msg_rev, |
| | | (void *)socket); |
| | | sockt->queue = new SHMQueue<shm_msg_t>(sockt->key, 16); |
| | | sockt->acceptQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | sockt->clientSocketMap = new std::map<int, shm_socket_t *>; |
| | | sockt->status = SHM_CONN_LISTEN; |
| | | pthread_create(&(sockt->dispatch_thread), NULL, _server_run_msg_rev, |
| | | (void *)sockt); |
| | | |
| | | return 0; |
| | | } |
| | |
| | | * 接受客户端建立新连接的请求 |
| | | * |
| | | */ |
| | | shm_socket_t *shm_accept(shm_socket_t *socket) { |
| | | if (socket->socket_type != SHM_SOCKET_STREAM) { |
| | | shm_socket_t *shm_accept(shm_socket_t *sockt) { |
| | | if (sockt->socket_type != SHM_SOCKET_STREAM) { |
| | | logger->error("can not invoke shm_accept method with a socket which is not a " |
| | | "SHM_SOCKET_STREAM socket"); |
| | | exit(1); |
| | |
| | | shm_socket_t *client_socket; |
| | | shm_msg_t src; |
| | | |
| | | if (socket->acceptQueue->pop(src) == 0) { |
| | | if (sockt->acceptQueue->pop(src) == 0) { |
| | | |
| | | // print_msg("===accept:", src); |
| | | client_key = src.key; |
| | | // client_socket = (shm_socket_t *)malloc(sizeof(shm_socket_t)); |
| | | client_socket = shm_open_socket(socket->socket_type); |
| | | client_socket->key = socket->key; |
| | | client_socket = shm_open_socket(sockt->socket_type); |
| | | client_socket->key = sockt->key; |
| | | // client_socket->queue= socket->queue; |
| | | //初始化消息queue |
| | | client_socket->messageQueue = |
| | |
| | | //连接到对方queue |
| | | client_socket->remoteQueue = _attach_remote_queue(client_key); |
| | | |
| | | socket->clientSocketMap->insert({client_key, client_socket}); |
| | | sockt->clientSocketMap->insert({client_key, client_socket}); |
| | | |
| | | /* |
| | | * shm_accept 用户执行的方法 |
| | |
| | | //发送open_reply,回应客户端的connect请求 |
| | | struct timespec timeout = {1, 0}; |
| | | shm_msg_t msg; |
| | | msg.key = socket->key; |
| | | msg.key = sockt->key; |
| | | msg.size = 0; |
| | | msg.type = SHM_SOCKET_OPEN_REPLY; |
| | | |
| | |
| | | /** |
| | | * @return 0成功. 其他值失败 |
| | | */ |
| | | int shm_connect(shm_socket_t *socket, int key) { |
| | | if (socket->socket_type != SHM_SOCKET_STREAM) { |
| | | int shm_connect(shm_socket_t *sockt, int key) { |
| | | int rv; |
| | | if (sockt->socket_type != SHM_SOCKET_STREAM) { |
| | | logger->error( "can not invoke shm_connect method with a socket which is not " |
| | | "a SHM_SOCKET_STREAM socket"); |
| | | exit(1); |
| | |
| | | return -1; |
| | | } |
| | | |
| | | if (socket->key == 0) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | if (sockt->key == 0) { |
| | | sockt->key = hashtable_alloc_key(hashtable); |
| | | sockt->queue = new SHMQueue<shm_msg_t>(sockt->key, 16); |
| | | } else { |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | rv = check_and_bind_queue(sockt); |
| | | if(rv != 0 ) { |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | |
| | | if ((socket->remoteQueue = _attach_remote_queue(key)) == NULL) { |
| | | if ((sockt->remoteQueue = _attach_remote_queue(key)) == NULL) { |
| | | logger->error("connect to %d failted", key); |
| | | return -1; |
| | | } |
| | | socket->messageQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | sockt->messageQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); |
| | | |
| | | //发送open请求 |
| | | struct timespec timeout = {1, 0}; |
| | | shm_msg_t msg; |
| | | msg.key = socket->key; |
| | | msg.key = sockt->key; |
| | | msg.size = 0; |
| | | msg.type = SHM_SOCKET_OPEN; |
| | | socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | sockt->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | |
| | | //接受open reply |
| | | if (socket->queue->pop(msg) == 0) { |
| | | if (sockt->queue->pop(msg) == 0) { |
| | | // 在这里server端已经准备好接受客户端发送请求了,完成与服务端的连接 |
| | | if (msg.type == SHM_SOCKET_OPEN_REPLY) { |
| | | socket->status = SHM_CONN_ESTABLISHED; |
| | | pthread_create(&(socket->dispatch_thread), NULL, _client_run_msg_rev, |
| | | (void *)socket); |
| | | sockt->status = SHM_CONN_ESTABLISHED; |
| | | pthread_create(&(sockt->dispatch_thread), NULL, _client_run_msg_rev, |
| | | (void *)sockt); |
| | | } else { |
| | | logger->error( "shm_connect: 不匹配的应答信息!"); |
| | | exit(1); |
| | |
| | | return 0; |
| | | } |
| | | |
| | | int shm_send(shm_socket_t *socket, const void *buf, const int size) { |
| | | if (socket->socket_type != SHM_SOCKET_STREAM) { |
| | | int shm_send(shm_socket_t *sockt, const void *buf, const int size) { |
| | | if (sockt->socket_type != SHM_SOCKET_STREAM) { |
| | | logger->error("shm_socket.shm_send: can not invoke shm_send method with a socket which is not a " |
| | | "SHM_SOCKET_STREAM socket"); |
| | | exit(1); |
| | | } |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | if(socket->remoteQueue == NULL) { |
| | | if(sockt->remoteQueue == NULL) { |
| | | err_msg(errno, "当前客户端无连接!"); |
| | | return -1; |
| | | } |
| | | shm_msg_t dest; |
| | | dest.type = SHM_COMMON_MSG; |
| | | dest.key = socket->key; |
| | | dest.key = sockt->key; |
| | | dest.size = size; |
| | | dest.buf = mm_malloc(size); |
| | | memcpy(dest.buf, buf, size); |
| | | |
| | | if (socket->remoteQueue->push(dest) == 0) { |
| | | if (sockt->remoteQueue->push(dest) == 0) { |
| | | return 0; |
| | | } else { |
| | | logger->error(errno, "connection has been closed!"); |
| | |
| | | } |
| | | } |
| | | |
| | | int shm_recv(shm_socket_t *socket, void **buf, int *size) { |
| | | if (socket->socket_type != SHM_SOCKET_STREAM) { |
| | | int shm_recv(shm_socket_t *sockt, void **buf, int *size) { |
| | | if (sockt->socket_type != SHM_SOCKET_STREAM) { |
| | | logger->error( "shm_socket.shm_recv: can not invoke shm_recv method in a %d type socket which is " |
| | | "not a SHM_SOCKET_STREAM socket ", |
| | | socket->socket_type); |
| | | sockt->socket_type); |
| | | exit(1); |
| | | } |
| | | shm_msg_t src; |
| | | |
| | | if (socket->messageQueue->pop(src) == 0) { |
| | | if (sockt->messageQueue->pop(src) == 0) { |
| | | void *_buf = malloc(src.size); |
| | | memcpy(_buf, src.buf, src.size); |
| | | *buf = _buf; |
| | |
| | | |
| | | |
| | | // 短连接方式发送 |
| | | int shm_sendto(shm_socket_t *socket, const void *buf, const int size, |
| | | int shm_sendto(shm_socket_t *sockt, const void *buf, const int size, |
| | | const int key, const struct timespec *timeout, const int flag) { |
| | | |
| | | int s; |
| | | int rv; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | if (sockt->socket_type != SHM_SOCKET_DGRAM) { |
| | | logger->error( "shm_socket.shm_sendto: Can't invoke shm_sendto method in a %d type socket which is " |
| | | "not a SHM_SOCKET_DGRAM socket ", |
| | | socket->socket_type); |
| | | sockt->socket_type); |
| | | exit(0); |
| | | } |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | |
| | | |
| | | if ((s = pthread_mutex_lock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_lock(&(sockt->mutex))) != 0) |
| | | err_exit(s, "shm_sendto : pthread_mutex_lock"); |
| | | |
| | | if (socket->queue == NULL) { |
| | | if (socket->key == 0) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | if (sockt->queue == NULL) { |
| | | if (sockt->key == 0) { |
| | | sockt->key = hashtable_alloc_key(hashtable); |
| | | sockt->queue = new SHMQueue<shm_msg_t>(sockt->key, 16); |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | |
| | | rv = check_and_bind_queue(sockt); |
| | | if(rv !=0 ) { |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | |
| | | } |
| | | |
| | | if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_unlock(&(sockt->mutex))) != 0) |
| | | err_exit(s, "shm_sendto : pthread_mutex_unlock"); |
| | | |
| | | // There is some case where a socket need to send to himeself, for example when bus server need to stop, he need to send himself |
| | | // There is some case where a sockt need to send to himeself, for example when bus server need to stop, he need to send himself |
| | | // a top message. |
| | | |
| | | // if (key == socket->key) { |
| | | // logger->error( "can not send to your self!"); |
| | | // return -1; |
| | | // } |
| | | if (key == sockt->key) { |
| | | logger->error( "can not send to your self!"); |
| | | return EBUS_SENDTO_SELF; |
| | | } |
| | | |
| | | SHMQueue<shm_msg_t> *remoteQueue; |
| | | if ((remoteQueue = _attach_remote_queue(key)) == NULL) { |
| | |
| | | |
| | | shm_msg_t dest; |
| | | dest.type = SHM_COMMON_MSG; |
| | | dest.key = socket->key; |
| | | dest.key = sockt->key; |
| | | dest.size = size; |
| | | dest.buf = mm_malloc(size); |
| | | memcpy(dest.buf, buf, size); |
| | |
| | | rv = remoteQueue->push(dest, timeout, flag); |
| | | |
| | | if (rv == 0) { |
| | | // printf("shm_sendto push after\n"); |
| | | printf("%d sendto %d suc.\n", shm_socket_get_key(sockt), key); |
| | | return 0; |
| | | } else { |
| | | mm_free(dest.buf); |
| | | logger->debug("sendto key %d failed %s", key, bus_strerror(rv)); |
| | | logger->debug("%d sendto key %d failed %s", shm_socket_get_key(sockt), key, bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | } |
| | |
| | | if (sokt->queue == NULL) { |
| | | if (sokt->key == 0) { |
| | | sokt->key = hashtable_alloc_key(hashtable); |
| | | sokt->queue = new SHMQueue<shm_msg_t>(sokt->key, 16); |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(sokt)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | rv = check_and_bind_queue(sokt); |
| | | if(rv != 0 ) { |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | sokt->queue = new SHMQueue<shm_msg_t>(sokt->key, 16); |
| | | } |
| | | |
| | | if ((s = pthread_mutex_unlock(&(sokt->mutex))) != 0) |
| | |
| | | } |
| | | } |
| | | |
| | | int _shm_sendandrecv_alloc_new(shm_socket_t *socket, const void *send_buf, |
| | | int _shm_sendandrecv_alloc_new(shm_socket_t *sockt, const void *send_buf, |
| | | const int send_size, const int send_key, void **recv_buf, |
| | | int *recv_size, const struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | |
| | | // 用thread local 保证每个线程用一个独占的socket接受对方返回的信息 |
| | | shm_socket_t *tmp_socket; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | if (sockt->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); |
| | | sockt->socket_type); |
| | | 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); |
| | | printf("======send key =%d , recv key=%d\n", send_key, recv_key); |
| | | printf("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); |
| | | |
| | | if(rv != 0) { |
| | | printf("_shm_sendandrecv_alloc_new : %s\n", bus_strerror(rv)); |
| | |
| | | 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, const struct timespec *timeout, int flags) { |
| | | |
| | | return _shm_sendandrecv_alloc_new(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); |
| | | } |
| | | |