Files
vision-anti-pinch/send.hpp
T

109 lines
2.0 KiB
C++

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
struct DebugInfo
{
int64_t TimeStampMs;
uint8_t Img[1920 * 1536 * 2];
};
/*******************线程发送检测图片与结果 ***********************************/
pthread_mutex_t g_send_mutex;
std::map<int64_t, DebugInfo *> g_map_debuginfo;
/*******************线程发送检测图片与结果***********************************/
int g_sockfd = -1;
std::string g_server_ip = "192.168.195.110";
int g_server_port = 8002;
void disconnect_server()
{
if (g_sockfd >= 0)
{
close(g_sockfd);
g_sockfd = -1;
}
}
bool reconnect_server()
{
disconnect_server();
struct sockaddr_in s_addr;
/*建立socket*/
g_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (g_sockfd < 0)
{
g_sockfd = -1;
return false;
}
/*设置服务器ip*/
bzero(&s_addr, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(g_server_port);
if (inet_aton(g_server_ip.c_str(), (struct in_addr *)&s_addr.sin_addr.s_addr) <= 0)
{
close(g_sockfd);
g_sockfd = -1;
return false;
}
/*开始连接服务器*/
if (connect(g_sockfd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr)) < 0)
{
close(g_sockfd);
g_sockfd = -1;
return false;
}
return true;
}
bool send_debuginfo(DebugInfo *pData)
{
bool res = false;
if (g_sockfd >= 0)
{
// tcp 发送数据
int len = 0;
if (pData)
{
len = send(g_sockfd, pData, sizeof(DebugInfo), MSG_NOSIGNAL);
}
if (len < 0)
{
reconnect_server();
sleep(1);
}
else
{
res = true;
}
}
else
{
reconnect_server();
sleep(1);
}
return res;
}