controller_patcher/source/network/UDPClient.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

/****************************************************************************
* Copyright (C) 2016,2017 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "UDPClient.hpp"
2021-09-19 21:16:03 +02:00
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
2017-05-07 14:44:09 +02:00
2017-10-29 09:34:47 +01:00
#include <utils/logger.h>
2017-05-07 14:44:09 +02:00
#define MAX_UDP_SIZE 0x578
UDPClient * UDPClient::instance = NULL;
2018-06-20 15:07:15 +02:00
UDPClient::UDPClient(uint32_t ip, int32_t port) {
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2018-06-19 17:46:37 +02:00
if (sockfd < 0)
return;
struct sockaddr_in connect_addr;
memset(&connect_addr, 0, sizeof(connect_addr));
connect_addr.sin_family = AF_INET;
connect_addr.sin_port = port;
connect_addr.sin_addr.s_addr = ip;
if(connect(sockfd, (struct sockaddr*)&connect_addr, sizeof(connect_addr)) < 0) {
2021-09-19 21:16:03 +02:00
close(sockfd);
2018-06-19 17:46:37 +02:00
sockfd = -1;
}
}
2018-06-19 17:46:37 +02:00
UDPClient::~UDPClient() {
if (this->sockfd != -1) {
2021-09-19 21:16:03 +02:00
close(sockfd);
}
2018-06-19 17:46:37 +02:00
if(HID_DEBUG) {
2020-12-16 02:04:31 +01:00
log_printf("UDPClient::~UDPClient(line %d): Thread has been closed",__LINE__);
2018-06-19 17:46:37 +02:00
}
}
2018-06-20 15:07:15 +02:00
BOOL UDPClient::sendData(char * data,int32_t length) {
2018-06-19 17:46:37 +02:00
if(sockfd < 0 || data == 0 || length < 0 || gUsedProtocolVersion < WIIU_CP_TCP_HANDSHAKE_VERSION_3) {
return false;
}
if(length > 1400) length = 1400;
2018-06-20 15:07:15 +02:00
int32_t ret = send(sockfd, data, length, 0);
return (ret >= 0);
}