视频监听安防平台-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

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意