视频监听安防平台-libuv库通信协议封装-支持udp和tcpserver同时使用

本文主要是介绍视频监听安防平台-libuv库通信协议封装-支持udp和tcpserver同时使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

             视频监听安防平台-libuv库通信协议封装-支持udp和tcpserver同时使用

最近在上网找资料的时候,看到libuv的通信库挺强大的,而且一直都有人在维护,所有就使用这个通信库做服务端接收和发送,在库封装好之后,发现在接收和发送消息的时候总是卡主了,才发现是使用libuv库的发送的时候出现的问题,果断将发送直接用底层发送,使用socket发送,下面附注一下libuv通信库的源码,希望对大家有帮助。

1、首先下载libuv库,我是自己裁剪部分代码出来封装的库。先编译一份libuv.a的静态库

2、对应用层进行封装,下面粘贴一下源码:

#ifndef __UV_SOCKET_TRANS_H__
#define __UV_SOCKET_TRANS_H__#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <map>#include "MyList.h"
#include "MyThread.h"
#include "TypeDef.h"
#include "tool.h"
#include "SysIf.h"
#include "uv.h"#define UV_TCPSVR_LISTEN_MAXCONN 1024
#define UV_SOCKET_BUFFER_SIZE	1024*1024*10//typedef void (*UvRecvPacketCallBack)(int sock,const char *pData, int len,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpRecv);using namespace std;typedef struct {uv_write_t req;uv_buf_t buf;
} write_req_t;typedef struct _TUvSocketItem
{int 					socketfd;SOCKETTYPE_E			socktype;char					srcip[IPSTR_MAX_LEN + 1];int						srcport;char					destip[IPSTR_MAX_LEN + 1];int						destport;//uv_tcp_t 				tcphandle;//uv_udp_t				udphandle;//uv_buf_t 				recvbuf;   void 					*sockethandle;char 					*pTcpRecvData;int 					nTcpRecvLen;_TUvSocketItem(){socketfd = -1;socktype = SOCKETTYPE_UDP;srcip[0] = '\0';srcport = 0;destip[0] = '\0';destport = 0;sockethandle = NULL;pTcpRecvData = NULL;nTcpRecvLen = 0;}
}TUvSocketItem;class CUvSocketTransMgr
{
public:CUvSocketTransMgr();~CUvSocketTransMgr();public:bool BindSocket(const char _srcip[], int _srcport, SOCKETTYPE_E type, char* _destip = NULL, int _destport = 0);int MoveSocket(const char _srcip[],int _srcport,SOCKETTYPE_E type,const char* _destip,int _destport);virtual int RecvPacketCallBack(int sock,const char *pData, int len,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpRecv) = 0;int SendPacket(int sock,const char *pData, int Datalen,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpSend);
private:bool tcp4_echo_start(const char *ip, int port);bool udp4_echo_start(const char *ip, int port);bool run(int status = UV_RUN_DEFAULT);static void echo_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);static void on_closeserver(uv_handle_t* handle);static void on_close(uv_handle_t* handle);static void after_write(uv_write_t* req, int status);static void after_read(uv_stream_t* client, ssize_t nread, const uv_buf_t* rcvbuf);static void on_connection(uv_stream_t* tcpserver, int status);static void on_send(uv_udp_send_t* req, int status);static void on_recv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* rcvbuf, const struct sockaddr* addr, unsigned flags); 
private:void sip_tcp_sticky_packet(TUvSocketItem * pSocketItem, const char *pData, int len, const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort);TUvSocketItem *OpenAUvSocketItem(int socketfd, SOCKETTYPE_E type, char *srcip, int srcport, char *destip, int destport, void *handle);TUvSocketItem *FindAUvSocketItem(int socketfd);void RemoveAUvSocketItem(int socketfd);void RemoveAllUvSocketItem(void);std::map<int, TUvSocketItem*> m_UvSocketItemMap;                     //scoketfd ->  TUvSocketItemtypedef std::map<int, TUvSocketItem*>::iterator UVSOCKETMAP_IT;typedef std::map<int, TUvSocketItem*>::value_type UVSOCKETMAP_VT;
public:uv_loop_t* loop;uv_tcp_t tcpServer;uv_udp_t udpServer;uv_handle_t* server;char m_serverip[IPSTR_MAX_LEN + 1];int  m_serverport;uv_mutex_t mutex_data_;//clients map mutex
private:uv_mutex_t mutex_sockets_;//clients map mutexuv_thread_t start_threadhandle_;//start thread handlestatic void StartThread(void* arg);//start thread,run until use close the serverbool m_runing;
};#endif 

源文件:

#include "UVSocketTransMgr.h"CUvSocketTransMgr::CUvSocketTransMgr()
{loop = NULL;int iret = uv_mutex_init(&mutex_data_);if (iret) {printf("%s: uv_mutex_init failed iret:%s\n", __FUNCTION__, uv_err_name(iret));}iret = uv_mutex_init(&mutex_sockets_);if (iret) {printf("%s: uv_mutex_init failed iret:%s\n", __FUNCTION__, uv_err_name(iret));}m_serverip[0] = '\0';m_serverport = 0;m_UvSocketItemMap.clear();m_runing = false;
}CUvSocketTransMgr::~CUvSocketTransMgr()
{RemoveAllUvSocketItem();uv_stop(loop);//pthread_exit(NULL);uv_thread_join(&start_threadhandle_);uv_loop_delete(loop);uv_mutex_destroy(&mutex_data_);uv_mutex_destroy(&mutex_sockets_);
}void CUvSocketTransMgr::echo_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) 
{buf->base = (char *)malloc(suggested_size);buf->len = suggested_size;
}void CUvSocketTransMgr::on_closeserver(uv_handle_t* handle) 
{printf("%s:%d >>>>>>>on_closeserver:%p\n", __FUNCTION__, __LINE__, handle);
}void CUvSocketTransMgr::on_close(uv_handle_t* handle) 
{	printf("%s:%d >>>>>>>on_close:%p\n", __FUNCTION__, __LINE__, handle);free(handle);
}void CUvSocketTransMgr::after_write(uv_write_t* req, int status) 
{write_req_t* wr;/* Free the read/write buffer and the request */wr = (write_req_t*) req;free(wr->buf.base);free(wr);if (status == 0)return;printf("%s: uv_write error: %s - %s\n", __FUNCTION__, uv_err_name(status), uv_strerror(status));
}void CUvSocketTransMgr::after_read(uv_stream_t* client, ssize_t nread, const uv_buf_t* rcvbuf) 
{uv_stream_t *tcpserver = (uv_stream_t *)client->data;if (tcpserver == NULL){printf("%s:%d tcpserver is null\n", __FUNCTION__, __LINE__);return;}CUvSocketTransMgr *socketmgr = (CUvSocketTransMgr *)tcpserver->data;if (socketmgr == NULL){printf("%s:%d socketmgr is null\n", __FUNCTION__, __LINE__);return;}if (nread > 0){TUvSocketItem *pSocketItem = socketmgr->FindAUvSocketItem(client->io_watcher.fd);if (pSocketItem == NULL){printf("%s:%d find a uvsocketitem socket:%d is null\n", __FUNCTION__, __LINE__, client->io_watcher.fd);return;}if (pSocketItem != NULL){// sticky sip tcp packetsocketmgr->sip_tcp_sticky_packet(pSocketItem, rcvbuf->base, nread, pSocketItem->srcip, pSocketItem->srcport, pSocketItem->destip, pSocketItem->destport);}}if (nread < 0) {        if (nread != UV_EOF) {printf("%s:Read error %s\n", __FUNCTION__, uv_err_name(nread));   }socketmgr->RemoveAUvSocketItem(client->io_watcher.fd);uv_close((uv_handle_t*) client, on_close);    }free(rcvbuf->base);return;
}void CUvSocketTransMgr::on_connection(uv_stream_t* tcpserver, int status) 
{if (status != 0) {printf("%s: Connect error %s\n", __FUNCTION__, uv_err_name(status));}CUvSocketTransMgr *socketmgr = (CUvSocketTransMgr *)tcpserver->data;if (socketmgr == NULL){printf("%s:%d socketmgr is null\n", __FUNCTION__, __LINE__);return;}uv_stream_t *client = (uv_stream_t *)malloc(sizeof(uv_tcp_t));if (client == NULL){	printf("%s:%d client malloc faild\n", __FUNCTION__, __LINE__);return;}int iret = uv_tcp_init(socketmgr->loop, (uv_tcp_t*)client);if (iret){printf("%s: uv_tcp_init failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return;}/* associate server with stream */client->data = tcpserver;iret = uv_accept(tcpserver, client);if (iret == 0){struct sockaddr_in serveraddr;	int serverlen = sizeof(struct sockaddr);    char svrip[IPSTR_MAX_LEN+1] = {0};uv_tcp_getsockname((uv_tcp_t*)client, (struct sockaddr*)&serveraddr, &serverlen);  inet_ntop(AF_INET, (struct in_addr *)&serveraddr.sin_addr.s_addr, svrip, IPSTR_MAX_LEN);int svrport = ntohs(serveraddr.sin_port);struct sockaddr_in clientaddr;	int clientlen = sizeof(struct sockaddr);	uv_tcp_getpeername((uv_tcp_t*)client, (struct sockaddr*)&clientaddr, &clientlen); char clientip[IPSTR_MAX_LEN+1] = {0};inet_ntop(AF_INET, (struct in_addr *)&clientaddr.sin_addr.s_addr, clientip, IPSTR_MAX_LEN);  //使用线程安全函数int clientport = ntohs(clientaddr.sin_port);	// add socketitemsocketmgr->OpenAUvSocketItem(client->io_watcher.fd, SOCKETTYPE_TCPSERVER, svrip, svrport, clientip, clientport, client);iret= uv_read_start(client, echo_alloc, after_read);if (iret){printf("%s: uv_read_start failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return;}}else{socketmgr->RemoveAUvSocketItem(client->io_watcher.fd);//close socketuv_close((uv_handle_t*)client, on_close);}
}bool CUvSocketTransMgr::tcp4_echo_start(const char *ip, int port) 
{struct sockaddr_in addr;int iret;iret = uv_ip4_addr(ip, port, &addr);if (iret){printf("%s: uv_ip4_addr failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}//server = (uv_handle_t*)&tcpServer;iret = uv_tcp_init(loop, &tcpServer);if (iret) {printf("%s: uv_tcp_init failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}iret = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr, 0);if (iret) {printf("%s: uv_tcp_bind failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}tcpServer.data = this;iret = uv_listen((uv_stream_t*)&tcpServer, UV_TCPSVR_LISTEN_MAXCONN, on_connection);if (iret) {printf("%s: uv_listen failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}return true;
}void CUvSocketTransMgr::on_send(uv_udp_send_t* req, int status) 
{if (status) {printf("%s:Send error %s\n", __FUNCTION__, uv_strerror(status));return;}//if (status == 0)free(req);
}void CUvSocketTransMgr::on_recv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* rcvbuf, const struct sockaddr* addr, unsigned flags)
{if (nread > 0){CUvSocketTransMgr *socketmgr = (CUvSocketTransMgr *)handle->data;if (socketmgr == NULL){return;}struct sockaddr_in svraddr;	int clientlen = sizeof(struct sockaddr);char svrip[IPSTR_MAX_LEN + 1] = {0};uv_udp_getsockname(handle, (struct sockaddr*)&svraddr, &clientlen);inet_ntop(AF_INET, (struct in_addr *)&svraddr.sin_addr.s_addr, svrip, IPSTR_MAX_LEN);  //使用线程安全函数		int svrport = ntohs(svraddr.sin_port);struct sockaddr_in *clientaddr = (struct sockaddr_in *)addr;	char dstip[IPSTR_MAX_LEN+1] = {0};inet_ntop(AF_INET, (struct in_addr *)&clientaddr->sin_addr.s_addr, dstip, IPSTR_MAX_LEN); int dstport = ntohs(clientaddr->sin_port);//static int recvtimes = 0;//printf("1.[%d]on_recvxxxxxxxxxxxxsocketfd:%d, handle:%p, nread:%d src<%s:%d>, dst<%s:%d>\n", ++recvtimes, handle->io_watcher.fd, handle, nread, svrip, svrport, dstip, dstport);		//回调uv_mutex_lock(&socketmgr->mutex_data_);socketmgr->RecvPacketCallBack(handle->io_watcher.fd, rcvbuf->base, nread, svrip, svrport, dstip, dstport, false);uv_mutex_unlock(&socketmgr->mutex_data_);//free rcvbuffree(rcvbuf->base);}
}bool CUvSocketTransMgr::udp4_echo_start(const char *ip, int port) 
{int iret;struct sockaddr_in addr;iret = uv_ip4_addr(ip, port, &addr);if (iret){printf("%s: uv_ip4_addr failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}//server = (uv_handle_t*)&udpServer;iret = uv_udp_init(loop, &udpServer);if (iret) {printf("%s: uv_udp_init failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}//uv_udp_nodelay//UV_UDP_REUSEADDRiret = uv_udp_bind(&udpServer, (const struct sockaddr *)&addr, UV_UDP_REUSEADDR);if (iret) {printf("%s: uv_udp_bind failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}udpServer.data = this;iret = uv_udp_recv_start(&udpServer, echo_alloc, on_recv);if (iret) {printf("%s: uv_udp_recv_start failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}OpenAUvSocketItem(udpServer.io_watcher.fd, SOCKETTYPE_UDP, m_serverip, m_serverport, NULL, 0, &udpServer);return true;
}//bool CUvSocketTransMgr::Start(SOCKETTYPE_E socktype, const char* ip, int port)
bool CUvSocketTransMgr::BindSocket(const char _srcip[],int _srcport,SOCKETTYPE_E type, char* _destip,int _destport)
{if (strlen(_srcip) <= 0 || _srcport <= 0){return false;}if (loop == NULL){loop = uv_default_loop();}if (loop == NULL){printf("%s: uv_default_loop failed\n", __FUNCTION__);return false;}strncpy2(m_serverip, _srcip, IPSTR_MAX_LEN);m_serverport = _srcport;if (type == SOCKETTYPE_UDP){udp4_echo_start(_srcip, _srcport);}else if (type == SOCKETTYPE_TCPSERVER){tcp4_echo_start(_srcip, _srcport);}else{return false;}if (!m_runing){int iret = uv_thread_create(&start_threadhandle_, StartThread, this);//use thread to wait for start succeed.if (iret) {printf("%s: uv_thread_create failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}m_runing = true;}return true;
}int CUvSocketTransMgr::MoveSocket(const char _srcip[],int _srcport,SOCKETTYPE_E type,const char* _destip,int _destport)
{//close udp socketif (type == SOCKETTYPE_UDP){uv_close((uv_handle_t*)&udpServer, on_closeserver);return 0;}else if (type == SOCKETTYPE_TCPSERVER){if (m_UvSocketItemMap.size() > 0){uv_mutex_lock(&mutex_sockets_);//close all tcp clientTUvSocketItem *pSocketItem = NULL;UVSOCKETMAP_IT it;for (it=m_UvSocketItemMap.begin(); it != m_UvSocketItemMap.end(); ++it){pSocketItem = (TUvSocketItem *)it->second;if (pSocketItem != NULL){if (pSocketItem->socktype == SOCKETTYPE_TCPSERVER){uv_close((uv_handle_t*)pSocketItem->sockethandle, on_close);  if (pSocketItem->pTcpRecvData != NULL){SAFE_DELETE(pSocketItem->pTcpRecvData);}}}}uv_mutex_unlock(&mutex_sockets_);}printf(">>>>>tcpServer.io_watcher.fd:%d\n", tcpServer.io_watcher.fd);uv_close((uv_handle_t*)&tcpServer, on_closeserver);  //close(tcpServer.io_watcher.fd);}return 0;	
}bool CUvSocketTransMgr::run(int status)
{printf("server runing.\n");int iret = uv_run(loop, (uv_run_mode)status);if (iret) {printf("%s: uv_run failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return false;}return true;
}void CUvSocketTransMgr::StartThread(void* arg)
{CUvSocketTransMgr* socketmgr = (CUvSocketTransMgr*)arg;socketmgr->run();
}void CUvSocketTransMgr::sip_tcp_sticky_packet(TUvSocketItem * pSocketItem, const char *pData, int len, const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort)
{if(len > 0){char *end_sip = NULL;char *cl_header = NULL;int cl_size = 0;if(pSocketItem->pTcpRecvData != NULL){/* concat old data with new data */pSocketItem->pTcpRecvData = (char *)realloc(pSocketItem->pTcpRecvData, pSocketItem->nTcpRecvLen+len+1);if(NULL == pSocketItem->pTcpRecvData){printf("realloc pTcpRecvData failed!\n");pSocketItem->nTcpRecvLen = 0;return;}strncpy(pSocketItem->pTcpRecvData+pSocketItem->nTcpRecvLen, pData, len);pSocketItem->pTcpRecvData[pSocketItem->nTcpRecvLen+len] = '\0';pSocketItem->nTcpRecvLen = pSocketItem->nTcpRecvLen + len;}if(NULL == pSocketItem->pTcpRecvData){pSocketItem->pTcpRecvData = (char *)malloc(len+1);if(pSocketItem->pTcpRecvData != NULL){memset(pSocketItem->pTcpRecvData, 0, len+1); }strncpy(pSocketItem->pTcpRecvData, pData, len);pSocketItem->pTcpRecvData[len] = '\0';	pSocketItem->nTcpRecvLen = len;}end_sip = strstr(pSocketItem->pTcpRecvData, "\r\n\r\n");while(end_sip != NULL){cl_header = mystrcasestr(pSocketItem->pTcpRecvData, "\ncontent-length ");if (cl_header == NULL || cl_header > end_sip)cl_header = mystrcasestr(pSocketItem->pTcpRecvData,"\ncontent-length:");if (cl_header == NULL || cl_header > end_sip)cl_header =mystrcasestr(pSocketItem->pTcpRecvData,"\r\nl ");if (cl_header == NULL || cl_header > end_sip)cl_header =mystrcasestr(pSocketItem->pTcpRecvData,"\r\nl:");if (cl_header != NULL && cl_header < end_sip)cl_header = strchr(cl_header, ':');if (cl_header == NULL || cl_header >= end_sip) {/* remove data up to crlfcrlf and restart */memmove(pSocketItem->pTcpRecvData, end_sip+4,pSocketItem->nTcpRecvLen -(end_sip + 4 -pSocketItem->pTcpRecvData) + 1);pSocketItem->nTcpRecvLen = pSocketItem->nTcpRecvLen - (end_sip +4 -pSocketItem->pTcpRecvData);pSocketItem->pTcpRecvData = (char *)realloc(pSocketItem->pTcpRecvData, pSocketItem->nTcpRecvLen+1);if(pSocketItem->pTcpRecvData == NULL){pSocketItem->nTcpRecvLen = 0;break;}end_sip =strstr(pSocketItem->pTcpRecvData,"\r\n\r\n");continue;	/* and restart from new CRLFCRLF */}/* header content-length was found before CRLFCRLF -> all headers are available */cl_header++;	/* after ':' char */cl_size = atoi(cl_header);if (cl_size == 0|| (cl_size >0 && (end_sip + 4 + cl_size <=pSocketItem->pTcpRecvData+pSocketItem->nTcpRecvLen))){uv_mutex_lock(&mutex_data_);RecvPacketCallBack(pSocketItem->socketfd,pSocketItem->pTcpRecvData, (end_sip + 4 + cl_size -pSocketItem->pTcpRecvData),\pSrcIP,SrcPort,pDestIP,DestPort, true);	uv_mutex_unlock(&mutex_data_);if (pSocketItem->nTcpRecvLen -(end_sip + 4 + cl_size -pSocketItem->pTcpRecvData) == 0) {end_sip = NULL;pSocketItem->nTcpRecvLen = 0;free(pSocketItem->pTcpRecvData);pSocketItem->pTcpRecvData = NULL;continue;}memmove(pSocketItem->pTcpRecvData,end_sip + 4 + cl_size, pSocketItem->nTcpRecvLen -(end_sip + 4 + cl_size -pSocketItem->pTcpRecvData) + 1);pSocketItem->nTcpRecvLen = pSocketItem->nTcpRecvLen - (end_sip +4 +cl_size -pSocketItem->pTcpRecvData);pSocketItem->pTcpRecvData = (char *)realloc(pSocketItem->pTcpRecvData,pSocketItem->nTcpRecvLen + 1);if (pSocketItem->pTcpRecvData == NULL){printf("realloc  pSocketItem->pTcpRecvDatapSocketItem->pTcpRecvDatapSocketItem->pTcpRecvData!!\n");pSocketItem->nTcpRecvLen = 0;break;}end_sip =strstr(pSocketItem->pTcpRecvData,"\r\n\r\n");continue;	/* and restart from new CRLFCRLF */}/* uncomplete SIP message */end_sip = NULL;		}if (pSocketItem->nTcpRecvLen== 0) {/* all data consumed are reallocation error ? */return;}}else if(len< 0){printf("Could not read socket (%d)- close it\n",pSocketItem->socketfd);//DelSocketItem(pSocketItem);//close(pSocketItem->sock);}else if (len == 0){printf("End of stream (read 0 byte from %s:%i)\n", pSocketItem->srcip, pSocketItem->srcport);//DelSocketItem(pSocketItem);//close(pSocketItem->sock);}else{printf("Dummy SIP message received (size=%d)\n", len);}}int CUvSocketTransMgr::SendPacket(int sock,const char *pData, int Datalen,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpSend)
{if (sock <= 0){struct sockaddr_in to;    bzero(&to, sizeof(struct sockaddr_in) );to.sin_family = AF_INET;to.sin_addr.s_addr = inet_addr(pDestIP);to.sin_port = htons(DestPort);int len = sendto(udpServer.io_watcher.fd, (char*)pData, Datalen, 0, (struct sockaddr *)&to, sizeof(struct sockaddr_in) );if (len != Datalen){printf("sendto <%s:%d> error: len = %d, datalen = %d! \n", pDestIP, DestPort, len, Datalen);}return len;}int iret = -1;TUvSocketItem *pSocketItem = FindAUvSocketItem(sock);if (pSocketItem != NULL){if (pSocketItem->socktype == SOCKETTYPE_UDP)	{
#if 1	struct sockaddr_in to;bzero(&to, sizeof(struct sockaddr_in) );to.sin_family = AF_INET;to.sin_addr.s_addr = inet_addr(pDestIP);to.sin_port = htons(DestPort);int len = sendto(sock, (char*)pData, Datalen, 0, (struct sockaddr *)&to, sizeof(struct sockaddr_in) );if (len != Datalen){printf("sendto <%s:%d> error: len = %d, datalen = %d! \n", pDestIP, DestPort, len, Datalen);}return len;	
#else	//send udpstruct sockaddr_in addr;iret = uv_ip4_addr(pDestIP, DestPort, &addr);if (iret){printf("%s: uv_ip4_addr failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return -1;}uv_udp_send_t* req;req = (uv_udp_send_t *)malloc(sizeof(*req));if (req == NULL){return -1;}uv_buf_t sndbuf = uv_buf_init((char *)pData, Datalen);	iret = uv_udp_send(req, &udpServer, &sndbuf, 1, (struct sockaddr*)&addr, on_send);if (iret){printf("%s: uv_udp_send failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return -1;}
#endif	}else if (pSocketItem->socktype == SOCKETTYPE_TCPSERVER){
#if 1		int len = send(sock, pData, Datalen, 0);return len;
#else			write_req_t *wr;wr = (write_req_t*) malloc(sizeof *wr);if (wr == NULL){printf("%s:%d write_req_t malloc failed!\n", __FUNCTION__, __LINE__);return -1;}wr->buf.base = (char *)malloc(Datalen + 1);wr->buf.len = Datalen;memcpy(wr->buf.base, pData, Datalen);wr->buf.base[Datalen] = '\0';	//wr->buf = uv_buf_init((char *)pData, Datalen);iret = uv_write(&wr->req, (uv_stream_t *)pSocketItem->sockethandle, &wr->buf, 1, after_write);if (iret) {printf("%s: uv_write failed iret:%s\n", __FUNCTION__, uv_err_name(iret));return -1;}
#endif			}}return iret;
}TUvSocketItem *CUvSocketTransMgr::OpenAUvSocketItem(int socketfd, SOCKETTYPE_E type, char *srcip, int srcport, char *destip, int destport, void *handle)
{uv_mutex_lock(&mutex_sockets_);TUvSocketItem *pSocketItem = NULL;UVSOCKETMAP_IT it;it = m_UvSocketItemMap.find(socketfd);if (it != m_UvSocketItemMap.end()){pSocketItem = (TUvSocketItem *)it->second;pSocketItem->socketfd = socketfd;pSocketItem->socktype = type;if (srcip != NULL)strncpy2(pSocketItem->srcip, srcip, IPSTR_MAX_LEN); pSocketItem->srcport = srcport;if (destip != NULL)strncpy2(pSocketItem->destip, destip, IPSTR_MAX_LEN);  pSocketItem->destport = destport;pSocketItem->sockethandle = handle;printf("%s: find a uvsocket socket:%d, socktype:%d, srcip:%s, srcport:%d, destip:%s, destport:%d, sockethandle:%p", __FUNCTION__, \socketfd, type, srcip, srcport, destip, destport, handle);	uv_mutex_unlock(&mutex_sockets_);return pSocketItem;}pSocketItem = new TUvSocketItem();pSocketItem->socketfd = socketfd;pSocketItem->socktype = type;if (srcip != NULL)strncpy2(pSocketItem->srcip, srcip, IPSTR_MAX_LEN); pSocketItem->srcport = srcport;if (destip != NULL)strncpy2(pSocketItem->destip, destip, IPSTR_MAX_LEN);  pSocketItem->destport = destport;pSocketItem->sockethandle = handle;m_UvSocketItemMap.insert(UVSOCKETMAP_VT(socketfd, pSocketItem));printf("%s: open a uvsocket socket:%d, socktype:%d, srcip:%s, srcport:%d, destip:%s, destport:%d, sockethandle:%p", __FUNCTION__, \socketfd, type, srcip, srcport, destip, destport, handle);uv_mutex_unlock(&mutex_sockets_);return pSocketItem;
}TUvSocketItem *CUvSocketTransMgr::FindAUvSocketItem(int socketfd)
{uv_mutex_lock(&mutex_sockets_);UVSOCKETMAP_IT it;it = m_UvSocketItemMap.find(socketfd);if (it != m_UvSocketItemMap.end()){uv_mutex_unlock(&mutex_sockets_);return (TUvSocketItem *)it->second;}uv_mutex_unlock(&mutex_sockets_);return NULL;
}void CUvSocketTransMgr::RemoveAUvSocketItem(int socketfd)
{uv_mutex_lock(&mutex_sockets_);UVSOCKETMAP_IT it;it = m_UvSocketItemMap.find(socketfd);if (it == m_UvSocketItemMap.end()){uv_mutex_unlock(&mutex_sockets_);return;}TUvSocketItem *pSocketItem = it->second;if (pSocketItem->pTcpRecvData != NULL){SAFE_DELETE(pSocketItem->pTcpRecvData);}SAFE_DELETE(it->second);m_UvSocketItemMap.erase(it);uv_mutex_unlock(&mutex_sockets_);return;
}void CUvSocketTransMgr::RemoveAllUvSocketItem(void)
{printf(">>>>>remove all socket item:%d\n", (int)m_UvSocketItemMap.size());uv_mutex_lock(&mutex_sockets_);if (m_UvSocketItemMap.size() <= 0){uv_mutex_unlock(&mutex_sockets_);return;}UVSOCKETMAP_IT it;for (it=m_UvSocketItemMap.begin(); it != m_UvSocketItemMap.end();){SAFE_DELETE(it->second);m_UvSocketItemMap.erase(it++);}uv_mutex_unlock(&mutex_sockets_);return;
}

使用方法:

启动接口,支持udp和tcpserver:

bool BindSocket(const char _srcip[], int _srcport, SOCKETTYPE_E type, char* _destip = NULL, int _destport = 0);

关闭socket:
int MoveSocket(const char _srcip[],int _srcport,SOCKETTYPE_E type,const char* _destip,int _destport);
将数据回调出上层接口:
virtual int RecvPacketCallBack(int sock,const char *pData, int len,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpRecv) = 0;

发送数据:
int SendPacket(int sock,const char *pData, int Datalen,const char *pSrcIP, int SrcPort,const char* pDestIP,int DestPort, bool bTcpSend);




这篇关于视频监听安防平台-libuv库通信协议封装-支持udp和tcpserver同时使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/631601

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.