Fu Juntang
2021-09-17 5c912c70e9333298ff48f7ea15424f72ca977b99
src/socket/shm_socket.h
@@ -4,25 +4,9 @@
#include "usg_common.h"
#include "usg_typedef.h"
#include "shm_queue.h"
enum shm_socket_flag_t
{
  SHM_MSG_TIMEOUT = 1,
  SHM_MSG_NOWAIT = 2
};
enum shm_socket_error_type_t {
   SHM_SOCKET_ECONNFAILED = 1,
   SHM_SOCKET_ETIMEOUT = 2
};
enum shm_connection_status_t {
   SHM_CONN_CLOSED=1,
   SHM_CONN_LISTEN=2,
   SHM_CONN_ESTABLISHED=3
};
#include "proc_def.h"
#include "lock_free_queue.h"
#include <functional>
enum shm_socket_type_t
{
@@ -30,86 +14,81 @@
   SHM_SOCKET_DGRAM = 2
   
};
enum shm_msg_type_t
{
   SHM_SOCKET_OPEN = 1,
   SHM_SOCKET_OPEN_REPLY = 2,
   SHM_SOCKET_CLOSE = 3,
   SHM_COMMON_MSG = 4
};
#define BUS_ACTION_STOP 1
typedef struct shm_msg_t {
   int key;
   shm_msg_type_t type;
typedef struct shm_packet_t {
  int key;
   size_t size;
   void * buf;
   char uuid[64];
   int action;
} shm_msg_t;
} shm_packet_t;
typedef struct shm_socket_t {
   shm_socket_type_t socket_type;
   // 本地key
   int key;
  char proc_id[MAX_STR_LEN];
   bool force_bind;
   int mutex;
   shm_connection_status_t status;
   SHMQueue<shm_msg_t> *queue;
   SHMQueue<shm_msg_t> *remoteQueue;
   LockFreeQueue<shm_msg_t, DM_Allocator> *messageQueue;
   LockFreeQueue<shm_msg_t, DM_Allocator> *acceptQueue;
   std::map<int, shm_socket_t* > *clientSocketMap;
   pthread_t dispatch_thread;
   pthread_mutex_t mutex;
   LockFreeQueue<shm_packet_t> *queue;  //self queue
   LockFreeQueue<shm_packet_t> *remoteQueue; // peer queue
   std::map<std::string, shm_packet_t> recvbuf; // for uuid
   std::map<int, shm_packet_t> recvbuf2; //for thread local
} shm_socket_t;
typedef std::function<void(void *recvbuf, int recvsize, int key, void **sendbuf, int *sendsize, void *user_data)> recvandsend_callback_fn;
size_t shm_socket_remove_keys(int keys[], size_t length);
size_t shm_socket_remove_keys_exclude(int keys[], size_t length);
shm_socket_t *shm_open_socket(shm_socket_type_t socket_type);
shm_socket_t *shm_socket_open(shm_socket_type_t socket_type);
int shm_close_socket(shm_socket_t * socket) ;
int shm_socket_close(shm_socket_t * sockt) ;
int shm_socket_stop(shm_socket_t * sockt);
int shm_socket_bind(shm_socket_t * socket, int key) ;
int shm_socket_force_bind(shm_socket_t * socket, int key) ;
int shm_listen(shm_socket_t * socket) ;
shm_socket_t* shm_accept(shm_socket_t* socket);
int shm_connect(shm_socket_t * socket, int key);
int shm_send(shm_socket_t * socket, const void *buf, const int size) ;
int shm_recv(shm_socket_t * socket, void **buf, int *size) ;
int shm_socket_bind_proc_id(shm_socket_t *sockt, const char *buf, int len);
/**
 * @flags : SHM_MSG_NOWAIT
 * @flags : BUS_NOWAIT_FLAG
 */
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_sendto(shm_socket_t *socket, const void *buf, const int size, const int key, const struct timespec * timeout = NULL, const int flags=0, int reset = 0, int data_set = 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 reset = 0, int data_set = 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);
typedef std::set<int,  std::less<int>, SHM_STL_Allocator<int> > SvrProc;
typedef std::map<SHMString, SvrProc *, std::less<SHMString>, SHM_STL_Allocator<std::pair<const SHMString, SvrProc *> > > SvrTcs;
typedef std::map<int, SHMString, std::less<int>, SHM_STL_Allocator<std::pair<int, const SHMString> > > ProcPartZone;
/**
 * 功能同shm_sendandrecv, 但是不是线程安全的
 * @callback  void (*recvandsend_callback_fn)(void *recvbuf, int recvsize, int key, void **sendbuf, int *sendsize, void * user_data)
 *                  @recvbuf 收到的数据
 *                  @recvsize 收到的数据的大小
 *                  @key 接受数据并并发送数据的对象
 *                  @sendbuf 存储返回值的地址,表示返回的数据
 *                  @sendsize 存储返回值的地址, 返回数据的长度
 *
 */
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);
int shm_recvandsend(shm_socket_t *sockt,  recvandsend_callback_fn callback,
                    const struct timespec *timeout = NULL, int flag = 0,  void * user_data = NULL);
void proc_copy(char *dst, void *src, int *count);
#endif