| | |
| | | #include "shm_mod_socket.h" |
| | | #include "socket_io.h" |
| | | #include <poll.h> |
| | | |
| | | #define OPEN_MAX 1024 |
| | | #define GET(p) (*(uint32_t *)(p)) |
| | | #define PUT(p, val) (*(uint32_t *)(p) = (val)) |
| | | |
| | | |
| | | #include "socket_def.h" |
| | | |
| | | |
| | | |
| | |
| | | int key; |
| | | }; |
| | | |
| | | #define NET_MODE_REQUEST_HEAD_LENGTH (NI_MAXHOST + 5 * sizeof(uint32_t)) |
| | | #define NET_MODE_REQUEST_HEAD_LENGTH (NI_MAXHOST + 6 * sizeof(uint32_t)) |
| | | |
| | | |
| | | // 请求头 |
| | | struct net_mod_request_head_t { |
| | | uint32_t mod; |
| | | char host[NI_MAXHOST]; |
| | | uint32_t port; |
| | | uint32_t key; |
| | | uint32_t content_length; |
| | | uint32_t topic_length; |
| | | uint32_t content_length; // 请求内容 |
| | | uint32_t topic_length; // 请求主题 |
| | | int32_t timeout; // -1 block, 0 nowait , > 0 timeout |
| | | }; |
| | | |
| | | #define NET_MODE_RESPONSE_HEAD_LENGTH (NI_MAXHOST + 4 * sizeof(uint32_t)) |
| | | |
| | | |
| | | // 应答头 |
| | | struct net_mod_response_head_t { |
| | | // socket_mod_t mod; |
| | | char host[NI_MAXHOST]; |
| | | uint32_t port; |
| | | uint32_t key; |
| | | uint32_t code; |
| | | uint32_t code; //返回值, 0 为成功 |
| | | uint32_t content_length; |
| | | }; |
| | | |
| | |
| | | }; |
| | | |
| | | class NetModSocket { |
| | | struct pool{ /* Represents a pool of connected descriptors */ //line:conc:echoservers:beginpool |
| | | |
| | | int nready; /* Number of ready descriptors from select */ |
| | | int maxi; /* Highwater index into client array */ |
| | | struct pollfd conns[OPEN_MAX]; |
| | | // net_node_t *nodes[FD_SETSIZE]; |
| | | // rio_t clientrio[FD_SETSIZE]; /* Set of active read buffers */ |
| | | // std::map<int, net_node_t*> connfdNodeMap; |
| | | std::map<std::string, int> connectionMap; |
| | | } ; |
| | | |
| | | |
| | | friend class NetModServerSocket; |
| | | private: |
| | | |
| | | ShmModSocket shmModSocket; |
| | | pool req_resp_pool; |
| | | // pool req_resp_pool; |
| | | |
| | | |
| | | |
| | | // request header 编码为网络传输的字节 |
| | | static void * encode_request_head(net_mod_request_head_t & request); |
| | | // 解码request header |
| | | static net_mod_request_head_t decode_request_head(void *headbs); |
| | | |
| | | static void * encode_response_head(net_mod_response_head_t & response); |
| | | static net_mod_response_head_t decode_response_head(void *_headbs); |
| | | |
| | | void init_req_rep_req_resp_pool(); |
| | | int connect( net_node_t*); |
| | | void close_connect(int connfd); |
| | | // 销毁threadlocal pool |
| | | static void _destroyConnPool_(void *_pool); |
| | | // 创建thread local key |
| | | static void _createConnPoolKey_(void); |
| | | |
| | | //读取返回信息 |
| | | int read_response(int clientfd, net_mod_recv_msg_t *recv_msg); |
| | | int write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size); |
| | | // 发送请求信息 |
| | | int write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size, void *topic_buf, int topic_size); |
| | | |
| | | int _sendandrecv_(net_node_t *node_arr, int node_arr_len, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout); |
| | | |
| | | int _pub_(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int timeout) ; |
| | | |
| | | |
| | | public: |
| | | |
| | |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int bind( int port); |
| | | int bind( int key); |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int force_bind( int port); |
| | | int force_bind( int key); |
| | | |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息会一直等待 |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度.如果IP为空则为本地发送。 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2性能好 |
| | | * 缺点:不是线程安全的, 即不能有两个以上的线程同时使用这个对象的方法 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2 性能好。 3 采用thread local技术即保证了线程安全,又可以使用连接池缓存连接 |
| | | */ |
| | | int sendandrecv(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | |
| | | * 如果建立连接的节点没有接受到消息等待timeout的时间后返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | int sendandrecv_timout(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | int sendandrecv_timeout(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout); |
| | | |
| | | /** |
| | |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int sendto( const void *buf, const int size, const int port); |
| | | int sendto( const void *buf, const int size, const int key); |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendto_timeout( const void *buf, const int size, const int port, int sec, int nsec); |
| | | int sendto_timeout( const void *buf, const int size, const int key, int sec, int nsec); |
| | | // 发送信息立刻返回。 |
| | | int sendto_nowait( const void *buf, const int size, const int port); |
| | | int sendto_nowait( const void *buf, const int size, const int key); |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @key 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int recvfrom( void **buf, int *size, int *port); |
| | | int recvfrom( void **buf, int *size, int *key); |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int recvfrom_timeout( void **buf, int *size, int *port, int sec, int nsec); |
| | | int recvfrom_nowait( void **buf, int *size, int *port); |
| | | int recvfrom_timeout( void **buf, int *size, int *key, int sec, int nsec); |
| | | int recvfrom_nowait( void **buf, int *size, int *key); |
| | | |
| | | /** |
| | | * 本地发送请求信息并等待接收应答 |
| | | * @port 发送给谁 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int sendandrecv( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) ; |
| | | int sendandrecv( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size) ; |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendandrecv_timeout( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size, int sec, int nsec) ; |
| | | int sendandrecv_nowait( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) ; |
| | | int sendandrecv_timeout( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, int sec, int nsec) ; |
| | | int sendandrecv_nowait( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size) ; |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int start_bus(); |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int sub( void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sub_timeout( void *topic, int size, int port, int sec, int nsec); |
| | | int sub_nowait( void *topic, int size, int port); |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int desub( void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int desub_timeout( void *topic, int size, int port, int sec, int nsec); |
| | | int desub_nowait( void *topic, int size, int port); |
| | | |
| | | /** |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @port 总线端口 |
| | | */ |
| | | int pub( char *topic, int topic_size, void *content, int content_size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int pub_timeout( char *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec); |
| | | int pub_nowait( char *topic, int topic_size, void *content, int content_size, int port); |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | |
| | | * @content 内容,@content_size 内容长度 |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int pub(net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | int pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) ; |
| | | |
| | | int pub_nowait(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size); |
| | | /** |
| | | * @msec 毫秒 (千分之一秒) |
| | | */ |
| | | int pub_timeout(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int msec); |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | int sub( void *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sub_timeout( void *topic, int size, int key, int sec, int nsec); |
| | | int sub_nowait( void *topic, int size, int key); |
| | | |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | int get_port() ; |
| | | int desub( void *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int desub_timeout( void *topic, int size, int key, int sec, int nsec); |
| | | int desub_nowait( void *topic, int size, int key); |
| | | |
| | | /** |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @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, int sec, int nsec); |
| | | int pub_nowait( char *topic, int topic_size, void *content, int content_size, int key); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取soket key |
| | | */ |
| | | int get_key() ; |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |