wangzhengquan
2020-09-25 00dba6082e245d917cb7d6eed3c627211ff41cd7
src/socket/dgram_mod_socket.c
@@ -1,139 +1,214 @@
#include "usg_common.h"
#include "dgram_mod_socket.h"
#include "shm_socket.h"
#include "shm_allocator.h"
#include "mem_pool.h"
#include "hashtable.h"
#include "sem_util.h"
#include "logger_factory.h"
#include "dmod_socket.h"
typedef struct dgram_mod_socket_t {
   socket_mod_t mod;
  shm_socket_t *shm_socket;
  pthread_t recv_thread;
   std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * > *recv_queue_map;
  DModSocket *m_socket;
} dgram_mod_socket_t;
void *dgram_mod_open_socket(int mod) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t));
   socket->mod = (socket_mod_t)mod;
   socket->recv_thread = 0;
   socket->recv_queue_map = NULL;
   socket->shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
int dgram_mod_remove_keys(int keys[], int length){
   return DModSocket::remove_keys(keys, length);
}
   printf("socket->shm_socket = %p \n" , socket->shm_socket);
int dgram_mod_remove_key(int key){
   int keys[] = {key};
   return DModSocket::remove_keys(keys, 1);
}
/**
 * 创建socket
 * @return socket地址
*/
void *dgram_mod_open_socket() {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t));
   // socket->mod = (socket_mod_t)mod;
   socket->m_socket = new DModSocket;
   return (void *)socket;
}
/**
 * 关闭socket
 * @return 0 成功, 其他值 失败的错误码
*/
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);
   delete socket->m_socket;
   free(_socket);
   return 0;
}
int dgram_mod_bind(void * _socket, int port){
/**
 * 绑定端口到socket, 如果不绑定则系统自动分配一个
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_bind(void * _socket, int port) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return  shm_socket_bind(socket->shm_socket, port);
   return socket->m_socket->bind(port);
}
int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port) {
/**
 * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_force_bind(void * _socket, int port) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return shm_sendto(socket->shm_socket, buf, size, port);
   return socket->m_socket->force_bind(port);
}
/**
 * 发送信息
 * @port 发送给谁
 * @return 0 成功, 其他值 失败的错误码
 */
int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->sendto(buf, size,port);
}
// 发送信息超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->sendto_timeout(buf, size, port, &timeout);
}
// 发送信息立刻返回。
int dgram_mod_sendto_nowait(void *_socket, const void *buf, const int size, const int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->sendto_nowait(buf, size,port);
}
/**
 * 接收信息
 * @port 从谁哪里收到的信息
 * @return 0 成功, 其他值 失败的错误码
*/
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);
   return socket->m_socket->recvfrom(buf, size, port);
}
// 接受信息超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->recvfrom_timeout(buf, size, port, &timeout);
}
void *_dgram_mod_run_recv(void * _socket) {
   pthread_detach(pthread_self());
int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port){
   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;
   return socket->m_socket->recvfrom_nowait(buf, size, port);
}
int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) {
/**
 * 发送请求信息并等待接收应答
 * @port 发送给谁
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int 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> * >;
   }
   return socket->m_socket->sendandrecv(send_buf, send_size, port, recv_buf, recv_size);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_sendandrecv_timeout(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->sendandrecv_timeout(send_buf, send_size, port, recv_buf, recv_size, &timeout);
}
int dgram_mod_sendandrecv_nowait(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->sendandrecv_nowait(send_buf, send_size, port, recv_buf, recv_size);
}
   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);
   }
/**
 * 启动bus
 *
 * @return 0 成功, 其他值 失败的错误码
*/
int  dgram_mod_start_bus(void * _socket) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->start_bus();
}
   shm_sendto(socket->shm_socket, send_buf, send_size, port);
   shm_msg_t msg;
// printf("==============dgram_mod_sendandrecv pop before\n");
   if(queue->pop(msg)) {
      *recv_buf = msg.buf;
      *recv_size = msg.size;
// printf("==============dgram_mod_sendandrecv pop after\n");
      return 0;
   }
   return -1;
/**
 * 订阅指定主题
 * @topic 主题
 * @size 主题长度
 * @port 总线端口
 */
int  dgram_mod_sub(void * _socket, void *topic, int size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->sub((char *)topic,  size,  port);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->sub_timeout((char *)topic,  size,  port, &timeout);
}
int  dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->sub_nowait((char *)topic,  size,  port);
}
/**
 * 取消订阅指定主题
 * @topic 主题
 * @size 主题长度
 * @port 总线端口
 */
int  dgram_mod_desub(void * _socket, void *topic, int size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->desub((char *)topic,  size,  port);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->desub_timeout((char *)topic,  size,  port, &timeout);
}
int  dgram_mod_desub_nowait(void * _socket, void *topic, int size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->desub_nowait((char *)topic,  size,  port);
}
/**
 * 发布主题
 * @topic 主题
 * @content 主题内容
 * @port 总线端口
 */
int  dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->pub((char *)topic, topic_size, content, content_size, port);
}
//  超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_pub_timeout(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   struct timespec timeout = {sec, nsec};
   return socket->m_socket->pub_timeout((char *)topic, topic_size, content, content_size, port, &timeout);
}
int  dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->pub_nowait((char *)topic, topic_size, content, content_size, port);
}
/**
 * 获取soket端口号
 */
int dgram_mod_get_port(void * _socket) {
   dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
   return socket->m_socket->get_port();
}
/**
 * 释放存储接收信息的buf
 */
void dgram_mod_free(void *buf) {
   free(buf);
}