1个文件已删除
1 文件已重命名
14个文件已修改
| | |
| | | char * |
| | | bus_strerror(int err) |
| | | { |
| | | int s, eindex; |
| | | int s; |
| | | char *buf; |
| | | eindex = err - EBUS_BASE; |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | |
| | | s = pthread_once(&once, createKey); |
| | |
| | | err_exit(s, "pthread_setspecific"); |
| | | } |
| | | |
| | | if (eindex < 0 || eindex >= _bus_nerr || _bus_errlist[eindex] == NULL) |
| | | { |
| | | snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", eindex); |
| | | } |
| | | else |
| | | { |
| | | strncpy(buf, _bus_errlist[eindex], MAX_ERROR_LEN - 1); |
| | | buf[MAX_ERROR_LEN - 1] = '\0'; /* Ensure null termination */ |
| | | |
| | | if(err < EBUS_BASE) { |
| | | // libc错误 |
| | | if (err < 0 || err >= _sys_nerr || _sys_errlist[err] == NULL) |
| | | { |
| | | snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err); |
| | | } |
| | | else |
| | | { |
| | | strncpy(buf, _sys_errlist[err], MAX_ERROR_LEN - 1); |
| | | buf[MAX_ERROR_LEN - 1] = '\0'; /* Ensure null termination */ |
| | | } |
| | | } else { |
| | | //自定义错误 |
| | | err -= EBUS_BASE; |
| | | if (err < 0 || err >= _bus_nerr || _bus_errlist[err] == NULL) |
| | | { |
| | | snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err); |
| | | } |
| | | else |
| | | { |
| | | strncpy(buf, _bus_errlist[err], MAX_ERROR_LEN - 1); |
| | | buf[MAX_ERROR_LEN - 1] = '\0'; /* Ensure null termination */ |
| | | } |
| | | |
| | | } |
| | | |
| | | return buf; |
| | |
| | | |
| | | |
| | | |
| | | #define EBUS_BASE 10000 |
| | | #define EBUS_TIMEOUT 10001 |
| | | #define EBUS_CLOSED 10002 |
| | | #define EBUS_KEY_INUSED 10003 |
| | | #define EBUS_NET 10004 |
| | | #define EBUS_BASE 500 |
| | | #define EBUS_TIMEOUT 501 |
| | | #define EBUS_CLOSED 502 |
| | | #define EBUS_KEY_INUSED 503 |
| | | #define EBUS_NET 504 |
| | | |
| | | extern int bus_errno; |
| | | |
| | |
| | | continue; |
| | | else { |
| | | // LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout"); |
| | | return rv; |
| | | return -1; |
| | | } |
| | | } |
| | | return 0; |
| | |
| | | continue; |
| | | else { |
| | | // LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout"); |
| | | return rv; |
| | | return -1; |
| | | } |
| | | } |
| | | return 0; |
| | |
| | | { |
| | | uint32_t currentReadIndex; |
| | | uint32_t currentWriteIndex; |
| | | uint32_t tmpIndex; |
| | | int s; |
| | | |
| | | // sigset_t mask_all, pre; |
| | |
| | | #ifdef _WITH_LOCK_FREE_Q_KEEP_REAL_SIZE |
| | | if (m_count == Q_SIZE) { |
| | | if( (flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) |
| | | return -1; |
| | | return errno; |
| | | else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | const struct timespec ts = TimeUtil::trim_time(timeout); |
| | | s = futex((int *)&m_count, FUTEX_WAIT, Q_SIZE, &ts, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | |
| | | } else { |
| | | s = futex((int *)&m_count, FUTEX_WAIT, Q_SIZE, NULL, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | return -1; |
| | | return errno; |
| | | } |
| | | } |
| | | |
| | | } |
| | | #else |
| | | if (currentReadIndex == currentWriteIndex - Q_SIZE + 1 ) |
| | | tmpIndex = (uint32_t)(currentWriteIndex - Q_SIZE + 1); |
| | | if (currentReadIndex == tmpIndex ) |
| | | { |
| | | // the queue is full |
| | | if( (flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) |
| | | return -1; |
| | | return errno; |
| | | else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | const struct timespec ts = TimeUtil::trim_time(timeout); |
| | | s = futex((int *)&m_readIndex, FUTEX_WAIT, currentWriteIndex - Q_SIZE + 1, &ts, NULL, 0); |
| | | s = futex((int *)&m_readIndex, FUTEX_WAIT, tmpIndex, &ts, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | |
| | | } else { |
| | | s = futex((int *)&m_readIndex, FUTEX_WAIT, currentWriteIndex - Q_SIZE + 1, NULL, NULL, 0); |
| | | s = futex((int *)&m_readIndex, FUTEX_WAIT, tmpIndex, NULL, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | return -1; |
| | | return errno; |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | if( (flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | const struct timespec ts = TimeUtil::trim_time(timeout); |
| | |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | |
| | | } else { |
| | | s = futex((int *)&m_count, FUTEX_WAIT, 0, NULL, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | } |
| | | } |
| | |
| | | // waiting to commit the data into it |
| | | if( (flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | const struct timespec ts = TimeUtil::trim_time(timeout); |
| | |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | |
| | | } else { |
| | | s = futex((int *)¤tMaximumReadIndex, FUTEX_WAIT, currentReadIndex, NULL, NULL, 0); |
| | | if (s == -1 && errno != EAGAIN && errno != EINTR) { |
| | | // sigprocmask(SIG_SETMASK, &pre, NULL); |
| | | return -1; |
| | | return errno; |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | |
| | | template<typename ELEM_T, |
| | | typename Allocator, |
| | | template<typename T, typename AT> class Q_TYPE> |
| | | typename Allocator, |
| | | template<typename T, typename AT> class Q_TYPE> |
| | | int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) { |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n"); |
| | | if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | if (psem_trywait(&slots) == -1) { |
| | | return -1; |
| | | } |
| | | } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | if (psem_timedwait(&slots, timeout) == -1) { |
| | | return -1; |
| | | } |
| | | } else { |
| | | if (psem_wait(&slots) == -1) { |
| | | return -1; |
| | | } |
| | | } |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n"); |
| | | if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | if (psem_trywait(&slots) == -1) { |
| | | return errno; |
| | | } |
| | | } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | if (psem_timedwait(&slots, timeout) == -1) { |
| | | return errno; |
| | | } |
| | | } else { |
| | | if (psem_wait(&slots) == -1) { |
| | | return errno; |
| | | } |
| | | } |
| | | |
| | | |
| | | if (m_qImpl.push(a_data)) { |
| | | psem_post(&items); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n"); |
| | | return 0; |
| | | } |
| | | return -1; |
| | | if (m_qImpl.push(a_data)) { |
| | | psem_post(&items); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n"); |
| | | return 0; |
| | | } |
| | | return -1; |
| | | |
| | | } |
| | | |
| | |
| | | typename Allocator, |
| | | template<typename T, typename AT> class Q_TYPE> |
| | | int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) { |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before...."); |
| | | |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n"); |
| | | if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | if (psem_trywait(&items) == -1) { |
| | | return errno; |
| | | } |
| | | } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before. flag=%d , %d\n", flag, timeout->tv_sec); |
| | | if (psem_timedwait(&items, timeout) == -1) { |
| | | return errno; |
| | | } |
| | | } else { |
| | | if (psem_wait(&items) == -1) { |
| | | return errno; |
| | | } |
| | | } |
| | | |
| | | |
| | | if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | if (psem_trywait(&items) == -1) { |
| | | return -1; |
| | | } |
| | | } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | if (psem_timedwait(&items, timeout) == -1) { |
| | | return -1; |
| | | } |
| | | } else { |
| | | if (psem_wait(&items) == -1) { |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | psem_post(&slots); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); |
| | | return 0; |
| | | } |
| | | return -1; |
| | | if (m_qImpl.pop(a_data)) { |
| | | psem_post(&slots); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); |
| | | return 0; |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | template<typename ELEM_T, |
| | |
| | | bool full(); |
| | | bool empty(); |
| | | |
| | | int push(const ELEM_T &a_data); |
| | | int push_nowait(const ELEM_T &a_data); |
| | | int push_timeout(const ELEM_T &a_data, const struct timespec *timeout); |
| | | int pop(ELEM_T &a_data); |
| | | int pop_nowait(ELEM_T &a_data); |
| | | int pop_timeout(ELEM_T &a_data, struct timespec *timeout); |
| | | int push(const ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0); |
| | | int pop(ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0); |
| | | |
| | | ELEM_T &operator[](unsigned i); |
| | | |
| | |
| | | return queue->empty(); |
| | | } |
| | | |
| | | |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::push(const ELEM_T &a_data) { |
| | | int rv = queue->push(a_data); |
| | | if(rv == -1) { |
| | | return errno; |
| | | } else { |
| | | int SHMQueue<ELEM_T>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) { |
| | | |
| | | |
| | | int rv = queue->push(a_data, timeout, flag); |
| | | if(rv == 0) { |
| | | return 0; |
| | | } |
| | | if(rv == ETIMEDOUT) |
| | | return EBUS_TIMEOUT; |
| | | else { |
| | | LoggerFactory::getLogger()->error("LockFreeQueue push_timeout: %s", bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::push_nowait(const ELEM_T &a_data) { |
| | | int rv = queue->push(a_data, NULL, BUS_NOWAIT_FLAG); |
| | | if(rv == -1) { |
| | | if (errno == EAGAIN) |
| | | return EAGAIN; |
| | | else { |
| | | err_msg(errno, "LockFreeQueue push_nowait"); |
| | | return errno; |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | int SHMQueue<ELEM_T>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) { |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::push_timeout(const ELEM_T &a_data, const struct timespec *timeout) { |
| | | |
| | | int rv = queue->push(a_data, timeout, BUS_TIMEOUT_FLAG); |
| | | if(rv == -1) { |
| | | if(errno == ETIMEDOUT) |
| | | return EBUS_TIMEOUT; |
| | | else { |
| | | LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout"); |
| | | return errno; |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::pop(ELEM_T &a_data) { |
| | | LoggerFactory::getLogger()->debug("SHMQueue pop before\n"); |
| | | int rv = queue->pop(a_data); |
| | | |
| | | LoggerFactory::getLogger()->debug("SHMQueue pop before\n"); |
| | | if(rv == -1) { |
| | | |
| | | return errno; |
| | | } else { |
| | | int rv = queue->pop(a_data, timeout, flag); |
| | | if(rv == 0) { |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::pop_nowait(ELEM_T &a_data) { |
| | | int rv = queue->pop(a_data, NULL, BUS_NOWAIT_FLAG); |
| | | |
| | | if(rv == -1) { |
| | | if (errno == EAGAIN) |
| | | return errno; |
| | | else { |
| | | LoggerFactory::getLogger()->error(errno, " SHMQueue pop_nowait"); |
| | | return errno; |
| | | } |
| | | if(rv == ETIMEDOUT) |
| | | return EBUS_TIMEOUT; |
| | | else { |
| | | LoggerFactory::getLogger()->error("LockFreeQueue pop_timeout: %s", bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | return 0; |
| | | |
| | | } |
| | | |
| | | template <typename ELEM_T> |
| | | int SHMQueue<ELEM_T>::pop_timeout(ELEM_T &a_data, struct timespec *timeout) { |
| | | |
| | | int rv; |
| | | rv = queue->pop(a_data, timeout, BUS_TIMEOUT_FLAG); |
| | | if(rv == -1) { |
| | | if (errno == ETIMEDOUT) { |
| | | return EBUS_TIMEOUT; |
| | | } else { |
| | | LoggerFactory::getLogger()->error(errno, " SHMQueue pop_timeout"); |
| | | return errno; |
| | | } |
| | | } |
| | | return 0; |
| | | return rv; |
| | | |
| | | } |
| | | |
| | |
| | | int rv; |
| | | |
| | | if( (rv = svsem_wait(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_remove\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_remove\n"); |
| | | } |
| | | tailq_header_t *my_tailq_head = hashtable->array[code] ; |
| | | if ( my_tailq_head == NULL) |
| | | { |
| | | if((rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_remove\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_remove\n"); |
| | | } |
| | | return NULL; |
| | | } else { |
| | |
| | | } |
| | | |
| | | if((rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_remove\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_remove\n"); |
| | | } |
| | | |
| | | return NULL; |
| | |
| | | |
| | | int rv; |
| | | if(( rv = svsem_wait(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_put\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_put\n"); |
| | | } |
| | | |
| | | _hashtable_put(hashtable, key, value); |
| | | |
| | | if(( rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_put\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_put\n"); |
| | | } |
| | | } |
| | | |
| | |
| | | int key = START_KEY; |
| | | rv = svsem_wait(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | |
| | | while(_hashtable_get(hashtable, key) != NULL) { |
| | |
| | | |
| | | rv = svsem_post(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | return key; |
| | | } |
| | |
| | | tailq_entry_t *item; |
| | | int rv; |
| | | if( (rv = svsem_wait(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n"); |
| | | } |
| | | for (int i = 0; i < MAPSIZE; i++) |
| | | { |
| | |
| | | } |
| | | |
| | | if((rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n"); |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n"); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::sendto_timeout(const void *buf, const int size, const int key, const struct timespec *timeout) { |
| | | return shm_sendto(shm_socket, buf, size, key, timeout, 0); |
| | | return shm_sendto(shm_socket, buf, size, key, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | // 发送信息立刻返回。 |
| | | int ShmModSocket::sendto_nowait( const void *buf, const int size, const int key){ |
| | | return shm_sendto(shm_socket, buf, size, key, NULL, (int)BUS_NOWAIT_FLAG); |
| | | return shm_sendto(shm_socket, buf, size, key, NULL, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::recvfrom_timeout( void **buf, int *size, int *key, struct timespec *timeout) { |
| | | int ShmModSocket::recvfrom_timeout( void **buf, int *size, int *key, const struct timespec *timeout) { |
| | | int rv = shm_recvfrom(shm_socket, buf, size, key, timeout, BUS_TIMEOUT_FLAG); |
| | | |
| | | return rv; |
| | | // printf("ShmModSocket::recvfrom_timeout\n"); |
| | | // return 501; |
| | | } |
| | | |
| | | int ShmModSocket::recvfrom_nowait( void **buf, int *size, int *key){ |
| | |
| | | return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, NULL, 0); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::sendandrecv_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, struct timespec *timeout){ |
| | | return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, 0); |
| | | int ShmModSocket::sendandrecv_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, const struct timespec *timeout){ |
| | | return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | int ShmModSocket::sendandrecv_nowait(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ |
| | | return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, (int)BUS_NOWAIT_FLAG); |
| | | return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | int ShmModSocket::sendandrecv_unsafe( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ |
| | | return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, NULL, 0); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::sendandrecv_unsafe_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, struct timespec *timeout){ |
| | | return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, 0); |
| | | int ShmModSocket::sendandrecv_unsafe_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, const struct timespec *timeout){ |
| | | return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | int ShmModSocket::sendandrecv_unsafe_nowait(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ |
| | | return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, (int)BUS_NOWAIT_FLAG); |
| | | return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | |
| | | return _sub_( topic, size, key, NULL, 0); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::sub_timeout(char *topic, int size, int key, struct timespec *timeout){ |
| | | return _sub_(topic, size, key, timeout, 0); |
| | | int ShmModSocket::sub_timeout(char *topic, int size, int key, const struct timespec *timeout){ |
| | | return _sub_(topic, size, key, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | int ShmModSocket::sub_nowait(char *topic, int size, int key) { |
| | | return _sub_(topic, size, key, NULL, (int)BUS_NOWAIT_FLAG); |
| | | return _sub_(topic, size, key, NULL, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | |
| | | return _desub_( topic, size, key, NULL, 0); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::desub_timeout(char *topic, int size, int key, struct timespec *timeout){ |
| | | return _desub_(topic, size, key, timeout, 0); |
| | | int ShmModSocket::desub_timeout(char *topic, int size, int key, const struct timespec *timeout){ |
| | | return _desub_(topic, size, key, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | int ShmModSocket::desub_nowait(char *topic, int size, int key) { |
| | | return _desub_(topic, size, key, NULL, (int)BUS_NOWAIT_FLAG); |
| | | return _desub_(topic, size, key, NULL, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | |
| | | return _pub_(topic, topic_size, content, content_size, key, NULL, 0); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int ShmModSocket::pub_timeout(char *topic, int topic_size, void *content, int content_size, int key, struct timespec * timeout){ |
| | | return _pub_( topic, topic_size, content, content_size, key, timeout, 0); |
| | | int ShmModSocket::pub_timeout(char *topic, int topic_size, void *content, int content_size, int key, const struct timespec * timeout){ |
| | | return _pub_( topic, topic_size, content, content_size, key, timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | int ShmModSocket::pub_nowait(char *topic, int topic_size, void *content, int content_size, int key){ |
| | | return _pub_(topic, topic_size, content, content_size, key, NULL, (int)BUS_NOWAIT_FLAG); |
| | | return _pub_(topic, topic_size, content, content_size, key, NULL, BUS_NOWAIT_FLAG); |
| | | } |
| | | |
| | | |
| | |
| | | * @key 总线端口 |
| | | */ |
| | | int ShmModSocket::_sub_(char *topic, int topic_size, int key, |
| | | struct timespec *timeout, int flags) { |
| | | const struct timespec *timeout, int flags) { |
| | | |
| | | |
| | | int ret; |
| | |
| | | /** |
| | | * @key 总线端口 |
| | | */ |
| | | int ShmModSocket::_desub_(char *topic, int topic_size, int key, |
| | | struct timespec *timeout, int flags) { |
| | | int ShmModSocket::_desub_(char *topic, int topic_size, int key, const struct timespec *timeout, int flags) { |
| | | // char buf[8192]; |
| | | int ret; |
| | | if(topic == NULL) { |
| | |
| | | if(size > 0) { |
| | | ret = shm_sendto(shm_socket, buf, size, key, timeout, flags); |
| | | free(buf); |
| | | if(ret == EBUS_TIMEOUT) { |
| | | logger->error(ret, "ShmModSocket::_desub_ key %d failed, %s", key, bus_strerror(EBUS_TIMEOUT)); |
| | | return EBUS_TIMEOUT; |
| | | } else { |
| | | logger->error(ret, "ShmModSocket::_desub_ key %d failed!", key); |
| | | return ret; |
| | | } |
| | | |
| | | if(ret == 0) { |
| | | return 0; |
| | | } else { |
| | | logger->error("ShmModSocket::_desub_ key %d failed, %s", key, bus_strerror(ret)); |
| | | return ret; |
| | | } |
| | | } else { |
| | | return -1; |
| | | } |
| | |
| | | * @str "<**pub**>{经济}" |
| | | */ |
| | | |
| | | int ShmModSocket::_pub_(char *topic, int topic_size, void *content, int content_size, int key, |
| | | struct timespec *timeout, int flags) { |
| | | int ShmModSocket::_pub_(char *topic, int topic_size, void *content, int content_size, int key, const struct timespec *timeout, int flags) { |
| | | // int head_len; |
| | | // char buf[8192+content_size]; |
| | | // snprintf(buf, 8192, "%spub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, topic, TOPIC_RIDENTIFIER); |
| | |
| | | |
| | | private: |
| | | |
| | | int _sub_( char *topic, int size, int key, struct timespec *timeout, int flags); |
| | | int _pub_( char *topic, int topic_size, void *content, int content_size, int key, struct timespec *timeout, int flags); |
| | | int _sub_( char *topic, int size, int key, const struct timespec *timeouts, int flags); |
| | | int _pub_( char *topic, int topic_size, void *content, int content_size, int key, const struct timespec *timeouts, int flags); |
| | | |
| | | int _desub_( char *topic, int size, int key, struct timespec *timeout, int flags); |
| | | int _desub_( char *topic, int size, int key, const struct timespec *timeouts, int flags); |
| | | |
| | | |
| | | static int get_bus_sendbuf(bus_head_t &request_head, void *topic_buf, int topic_size, void *content_buf, int content_size, void **retbuf); |
| | |
| | | */ |
| | | int recvfrom(void **buf, int *size, int *key); |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int recvfrom_timeout(void **buf, int *size, int *key, struct timespec *timeout); |
| | | int recvfrom_timeout(void **buf, int *size, int *key, const struct timespec *timeout); |
| | | int recvfrom_nowait(void **buf, int *size, int *key); |
| | | |
| | | /** |
| | |
| | | */ |
| | | int sendandrecv(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) ; |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendandrecv_timeout(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, struct timespec *timeout) ; |
| | | int sendandrecv_timeout(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, const struct timespec *timeout) ; |
| | | int sendandrecv_nowait(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) ; |
| | | |
| | | |
| | | int sendandrecv_unsafe(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) ; |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendandrecv_unsafe_timeout(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, struct timespec *timeout) ; |
| | | int sendandrecv_unsafe_timeout(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, const struct timespec *timeout) ; |
| | | int sendandrecv_unsafe_nowait(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) ; |
| | | |
| | | /** |
| | |
| | | */ |
| | | int sub(char *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sub_timeout(char *topic, int size, int key, struct timespec *timeout); |
| | | int sub_timeout(char *topic, int size, int key, const struct timespec *timeout); |
| | | int sub_nowait(char *topic, int size, int key); |
| | | |
| | | |
| | |
| | | */ |
| | | int desub( char *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int desub_timeout(char *topic, int size, int key, struct timespec *timeout); |
| | | int desub_timeout(char *topic, int size, int key, const struct timespec *timeout); |
| | | int desub_nowait(char *topic, int size, int key) ; |
| | | |
| | | /** |
| | |
| | | */ |
| | | int pub(char *topic, int topic_size, void *content, int content_size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int pub_timeout(char *topic, int topic_size, void *content, int content_size, int key, struct timespec *timeout); |
| | | int pub_timeout(char *topic, int topic_size, void *content, int content_size, int key, const struct timespec *timeout); |
| | | int pub_nowait(char *topic, int topic_size, void *content, int content_size, int key); |
| | | |
| | | |
| | |
| | | msg.size = 0; |
| | | msg.type = SHM_SOCKET_OPEN_REPLY; |
| | | |
| | | if (client_socket->remoteQueue->push_timeout(msg, &timeout) == 0) { |
| | | if (client_socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG) == 0) { |
| | | client_socket->status = SHM_CONN_ESTABLISHED; |
| | | return client_socket; |
| | | } else { |
| | |
| | | msg.key = socket->key; |
| | | msg.size = 0; |
| | | msg.type = SHM_SOCKET_OPEN; |
| | | socket->remoteQueue->push_timeout(msg, &timeout); |
| | | socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | |
| | | //接受open reply |
| | | if (socket->queue->pop(msg) == 0) { |
| | |
| | | |
| | | // 短连接方式发送 |
| | | int shm_sendto(shm_socket_t *socket, const void *buf, const int size, |
| | | const int key, const struct timespec *timeout, const int flags) { |
| | | const int key, const struct timespec *timeout, const int flag) { |
| | | |
| | | int s; |
| | | int rv; |
| | |
| | | dest.buf = mm_malloc(size); |
| | | memcpy(dest.buf, buf, size); |
| | | |
| | | |
| | | if( (flags & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | rv = remoteQueue->push_nowait(dest); |
| | | } else if((flags & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | rv = remoteQueue->push_timeout(dest, timeout); |
| | | } else { |
| | | rv = remoteQueue->push(dest); |
| | | } |
| | | rv = remoteQueue->push(dest, timeout, flag); |
| | | |
| | | if (rv == 0) { |
| | | // printf("shm_sendto push after\n"); |
| | | return 0; |
| | | } else { |
| | | mm_free(dest.buf); |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("sendto key %d failed %s", key, bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "sendto key %d failed", key); |
| | | } |
| | | logger->debug("sendto key %d failed %s", key, bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | } |
| | | |
| | | int shm_recvfrom2(shm_socket_t *socket, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { |
| | | return 501; |
| | | } |
| | | |
| | | // 短连接方式接受 |
| | | int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key, struct timespec *timeout, int flags) { |
| | | int shm_recvfrom(shm_socket_t *sokt, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { |
| | | int s; |
| | | int rv; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | if (sokt->socket_type != SHM_SOCKET_DGRAM) { |
| | | logger->error("shm_socket.shm_recvfrom: Can't invoke shm_recvfrom method in a %d type socket which " |
| | | "is not a SHM_SOCKET_DGRAM socket ", |
| | | socket->socket_type); |
| | | sokt->socket_type); |
| | | exit(1); |
| | | } |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | |
| | | if ((s = pthread_mutex_lock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_lock(&(sokt->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_lock"); |
| | | |
| | | if (socket->queue == NULL) { |
| | | if (socket->key == 0) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | if (sokt->queue == NULL) { |
| | | if (sokt->key == 0) { |
| | | sokt->key = hashtable_alloc_key(hashtable); |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | if(!_shm_socket_check_key(sokt)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | sokt->queue = new SHMQueue<shm_msg_t>(sokt->key, 16); |
| | | } |
| | | |
| | | if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_unlock(&(sokt->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_unlock"); |
| | | |
| | | shm_msg_t src; |
| | | |
| | | if((flags & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | rv = socket->queue->pop_nowait(src); |
| | | } else if((flags & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | rv = socket->queue->pop_timeout(src, timeout); |
| | | // printf("0 shm_recvfrom====%d\n", rv); |
| | | } else { |
| | | rv = socket->queue->pop(src); |
| | | } |
| | | printf ("====== before ======\n"); |
| | | rv = sokt->queue->pop(src, timeout, flag); |
| | | printf ("====== after ======\n %d", rv); |
| | | |
| | | if (rv == 0) { |
| | | if(buf != NULL) { |
| | |
| | | mm_free(src.buf); |
| | | return 0; |
| | | } else { |
| | | if(rv > EBUS_BASE) { |
| | | logger->debug("shm_recvfrom failed %s", bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "shm_recvfrom failed"); |
| | | } |
| | | logger->debug("shm_recvfrom failed %s", bus_strerror(rv)); |
| | | return rv; |
| | | |
| | | } |
| | |
| | | // use thread local |
| | | int _shm_sendandrecv_thread_local(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_size, const struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | | int rv; |
| | | |
| | |
| | | |
| | | int _shm_sendandrecv_alloc_new(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_size, const struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | | int rv; |
| | | |
| | |
| | | |
| | | int shm_sendandrecv_unsafe(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_size, const struct timespec *timeout, int flags) { |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | logger->error( "shm_socket.shm_sendandrecv_unsafe : Can't invoke shm_sendandrecv method in a %d type socket " |
| | | "which is not a SHM_SOCKET_DGRAM socket ", |
| | |
| | | |
| | | 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_size, const struct timespec *timeout, int flags) { |
| | | return _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); |
| | | } |
| | | |
| | |
| | | close_msg.size = 0; |
| | | close_msg.type = SHM_SOCKET_CLOSE; |
| | | if (notifyRemote && socket->remoteQueue != NULL) { |
| | | socket->remoteQueue->push_timeout(close_msg, &timeout); |
| | | socket->remoteQueue->push(close_msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | } |
| | | |
| | | if (socket->queue != NULL) { |
| | |
| | | iter != socket->clientSocketMap->end(); iter++) { |
| | | client_socket = iter->second; |
| | | |
| | | client_socket->remoteQueue->push_timeout(close_msg, &timeout); |
| | | client_socket->remoteQueue->push(close_msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | client_socket->remoteQueue = NULL; |
| | | |
| | | delete client_socket->messageQueue; |
| | |
| | | */ |
| | | int shm_sendto(shm_socket_t *socket, const void *buf, const int size, const int key, const struct timespec * timeout = NULL, const int flags=0); |
| | | |
| | | int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key, struct timespec * timeout = NULL, int flags=0); |
| | | int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key, const struct timespec * timeout = NULL, int flags=0); |
| | | |
| | | 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 = NULL, int flags=0); |
| | | const struct timespec * timeout = NULL, int flags=0); |
| | | |
| | | /** |
| | | * 功能同shm_sendandrecv, 但是不是线程安全的 |
| | | */ |
| | | int shm_sendandrecv_unsafe(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 = NULL, int flags=0); |
| | | const struct timespec * timeout = NULL, int flags=0); |
| | | |
| | | |
| | | |
| | |
| | | while (semop(semid, &sops, 1) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_dec"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | sops.sem_num = 0; |
| | | sops.sem_op = -1; |
| | | sops.sem_flg = IPC_NOWAIT | 0; |
| | | |
| | | |
| | | if (semop(semid, &sops, 1) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_dec_nowait"); |
| | | return errno; |
| | | } |
| | | |
| | | return 0; |
| | | return semop(semid, &sops, 1) ; |
| | | } |
| | | |
| | | int svsem_timedwait(const int semid, const struct timespec *timeout) { |
| | |
| | | while (semtimedop(semid, &sops, 1, timeout) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_psem_timedwait"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | int rv = semop(semid, &sops, 1); |
| | | if (rv == -1) { |
| | | // err_msg(errno, "svsem_inc"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | return 0; |
| | | } |
| | |
| | | arg.val = 1; |
| | | if (semctl(semid, 1, SETVAL, arg) == -1) { |
| | | err_msg(errno, "svsem_set"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | //释放mutex |
| | |
| | | while (semop(semid, sops, 2) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "Svsvsem_dec"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | |
| | |
| | | while (semop(semid, sops, 1) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "Svsvsem_dec"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | |
| | | if (semctl(semid, 1, SETVAL, arg) == -1) { |
| | | err_msg(errno, "svsem_set"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | return 0; |
| | | } |
| | |
| | | while (semop(semid, &sops, 1) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_zero"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | while (semop(semid, &sops, 1) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_zero_nowait"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | while (semtimedop(semid, &sops, 1, timeout) == -1) |
| | | if (errno != EINTR) { |
| | | // err_msg(errno, "svsem_zero_timeout"); |
| | | return errno; |
| | | return -1; |
| | | } |
| | | |
| | | return 0; |
| | |
| | | int svsem_set(int semid, int val) { |
| | | union semun arg; |
| | | arg.val = val; |
| | | if (semctl(semid, 0, SETVAL, arg) == -1) { |
| | | err_msg(errno, "svsem_set"); |
| | | return errno; |
| | | } |
| | | return 0; |
| | | |
| | | return semctl(semid, 0, SETVAL, arg); |
| | | } |
| | | |
| | | |
| | |
| | | struct timespec TimeUtil::calc_abs_time(const struct timespec *ts) { |
| | | |
| | | struct timespec res; |
| | | struct timespec timeout; |
| | | if (clock_gettime(CLOCK_REALTIME, &timeout) == -1) |
| | | struct timespec cur; |
| | | if (clock_gettime(CLOCK_REALTIME, &cur) == -1) |
| | | err_exit(errno, "clock_gettime"); |
| | | |
| | | res.tv_sec = timeout.tv_sec + ts->tv_sec; |
| | | res.tv_nsec = timeout.tv_nsec + ts->tv_nsec; |
| | | res.tv_sec = cur.tv_sec + ts->tv_sec; |
| | | res.tv_nsec = cur.tv_nsec + ts->tv_nsec; |
| | | res.tv_sec = res.tv_sec + floor(res.tv_nsec / NANO); |
| | | res.tv_nsec = res.tv_nsec % NANO; |
| | | return res; |
| | |
| | | #include "shm_mm_wrapper.h" |
| | | #include "usg_common.h" |
| | | #include "mm.h" |
| | | #include "logger_factory.h" |
| | | #include "bus_error.h" |
| | | |
| | | static Logger *logger = LoggerFactory::getLogger(); |
| | | |
| | | BusServerSocket * server_socket; |
| | | void sigint_handler(int sig) { |
| | |
| | | |
| | | void *run_recv(void *skptr) { |
| | | while(true) { |
| | | printf("================run_recv\n"); |
| | | logger->debug("================run_recv\n"); |
| | | sleep(1); |
| | | } |
| | | } |
| | |
| | | ShmModSocket *sk = new ShmModSocket(); |
| | | |
| | | pthread_t tid; |
| | | pthread_create(&tid, NULL, run_recv, (void *)sk); |
| | | |
| | | int size; |
| | | |
| | | char action[512]; |
| | | char topic[512]; |
| | | char content[512]; |
| | | long i = 0; |
| | | |
| | | pthread_create(&tid, NULL, run_recv, (void *)sk); |
| | | |
| | | while (true) { |
| | | //printf("Usage: pub <topic> [content] or sub <topic>\n"); |
| | | printf("Can I help you? sub, pub, desub or quit\n"); |
| | | scanf("%s",action); |
| | | printf("Can I help you? sub, pub, desub or quit %d\n", i++); |
| | | // sleep(100); |
| | | scanf("%s", action); |
| | | |
| | | if(strcmp(action, "sub") == 0) { |
| | | printf("Please input topic!\n"); |