Skip to content

Commit

Permalink
First step to make Windows work again
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelscholle authored May 13, 2024
1 parent fa578e3 commit 21370e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/telemetry/connection/tcp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,33 @@
#define _WIN32_WINNT 0x0600 //TODO dirty
#include <winsock2.h>
#include <Ws2tcpip.h> // For InetPton
#include <Windows.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#endif

#include <qdebug.h>

#include "mavlinkchannel.h"

static int linux_tcp_socket_try_connect(const std::string remote_ip, const int remote_port,const int timeout_seconds){
//qDebug()<<"linux_tcp_socket_try_connect:"<<remote_ip.c_str()<<":"<<remote_port<<" timeout:"<<timeout_seconds<<"s";
static int tcp_socket_try_connect(const std::string remote_ip, const int remote_port,const int timeout_seconds){
//qDebug()<<"tcp_socket_try_connect:"<<remote_ip.c_str()<<":"<<remote_port<<" timeout:"<<timeout_seconds<<"s";
int sockfd=socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0){
qDebug()<<"Cannot create socket"<<strerror(errno);
return -1;
}
#ifdef __linux__
if(fcntl(sockfd, F_SETFL, O_NONBLOCK)<0){
qDebug()<<"Cannot set non-blocking";
close(sockfd);
return -1;
}
#endif
struct sockaddr_in remote_addr {};
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(remote_port);
Expand Down Expand Up @@ -60,12 +63,14 @@ static int linux_tcp_socket_try_connect(const std::string remote_ip, const int r
int so_error;
// data to read
socklen_t len = sizeof(so_error);
#ifdef __linux__
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
if (so_error != 0) {
qDebug()<<"Any socket error:"<<strerror(errno);
close(sockfd);
return -1;
}
#endif
qDebug()<<"Success";
return sockfd;
}
Expand Down Expand Up @@ -247,9 +252,10 @@ void TCPConnection::receive_until_stopped()

void TCPConnection::loop_connect_receive()
{
#ifdef __linux__
qDebug()<<"TCPConnection2::loop_connect_receive begin";
while(m_keep_receiving){
m_socket_fd=linux_tcp_socket_try_connect(m_remote_ip,m_remote_port,3);
m_socket_fd=tcp_socket_try_connect(m_remote_ip,m_remote_port,3);
if(m_socket_fd>0){
m_last_data_ms=QOpenHDMavlinkHelper::getTimeMilliseconds();
receive_until_stopped();
Expand All @@ -265,4 +271,5 @@ void TCPConnection::loop_connect_receive()
}
}
qDebug()<<"TCPConnection2::loop_connect_receive end";
#endif
}
4 changes: 4 additions & 0 deletions app/videostreaming/vscommon/custom/rawreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
RawReceiver::RawReceiver(int port,std::string ip,bool is_h265,bool unused):
is_h265(is_h265)
{
#ifdef __linux__
m_keyframe_finder=std::make_unique<CodecConfigFinder>();
auto udp_config=UDPReceiver::Configuration{std::nullopt,port};
udp_config.opt_os_receive_buff_size=UDPReceiver::BIG_UDP_RECEIVE_BUFFER_SIZE;
Expand All @@ -16,13 +17,16 @@ RawReceiver::RawReceiver(int port,std::string ip,bool is_h265,bool unused):
this->udp_raw_data_callback(payload,payloadSize);
});
m_udp_receiver->startReceiving();
#endif
}

RawReceiver::~RawReceiver()
{
#ifdef __linux__
if(m_udp_receiver){
m_udp_receiver->stopReceiving();
}
#endif
}

std::shared_ptr<NALUBuffer> RawReceiver::get_next_frame(std::optional<std::chrono::microseconds> timeout)
Expand Down
6 changes: 6 additions & 0 deletions app/videostreaming/vscommon/udp/UDPReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
#define FPV_VR_UDPRECEIVER_H

#include <stdio.h>
#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#include <Ws2tcpip.h> // For InetPton
#include <Windows.h>
#endif
#include <unistd.h>
#include <iostream>
#include <thread>
Expand Down

0 comments on commit 21370e0

Please sign in to comment.