2020-12-28 16:22:12 +00:00
|
|
|
/**
|
|
|
|
** Supermodel
|
|
|
|
** A Sega Model 3 Arcade Emulator.
|
|
|
|
** Copyright 2011-2020 Bart Trzynadlowski, Nik Henson, Ian Curtis,
|
|
|
|
** Harry Tuttle, and Spindizzi
|
|
|
|
**
|
|
|
|
** This file is part of Supermodel.
|
|
|
|
**
|
|
|
|
** Supermodel 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.
|
|
|
|
**
|
|
|
|
** Supermodel 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 Supermodel. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**/
|
|
|
|
|
2020-06-13 19:46:12 +00:00
|
|
|
#include "TCPSend.h"
|
2020-12-28 16:22:12 +00:00
|
|
|
#include "OSD/Logger.h"
|
2020-06-13 19:46:12 +00:00
|
|
|
|
2020-06-16 11:55:38 +00:00
|
|
|
#if defined(_DEBUG)
|
|
|
|
#include <stdio.h>
|
2020-12-28 16:22:12 +00:00
|
|
|
#define DPRINTF DebugLog
|
2020-06-16 11:55:38 +00:00
|
|
|
#else
|
|
|
|
#define DPRINTF(a, ...)
|
|
|
|
#endif
|
|
|
|
|
2020-06-13 19:46:12 +00:00
|
|
|
static const int RETRY_COUNT = 10; // shrugs
|
|
|
|
|
|
|
|
TCPSend::TCPSend(std::string& ip, int port) :
|
|
|
|
m_ip(ip),
|
|
|
|
m_port(port),
|
|
|
|
m_socket(nullptr)
|
|
|
|
{
|
|
|
|
SDLNet_Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSend::~TCPSend()
|
|
|
|
{
|
|
|
|
if (m_socket) {
|
|
|
|
SDLNet_TCP_Close(m_socket);
|
|
|
|
m_socket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLNet_Quit(); // unload lib (winsock dll for windows)
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TCPSend::Send(const void * data, int length)
|
|
|
|
{
|
|
|
|
// If we failed bail out
|
|
|
|
if (!Connected()) {
|
2020-06-16 11:55:38 +00:00
|
|
|
DPRINTF("Not connected\n");
|
2020-06-13 19:46:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:55:38 +00:00
|
|
|
DPRINTF("Sending %i bytes\n", length);
|
|
|
|
|
2022-07-17 18:29:25 +00:00
|
|
|
int sent = SDLNet_TCP_Send(m_socket, &length, sizeof(int)); // pack the length at the start of transmission.
|
2021-04-14 01:20:45 +00:00
|
|
|
|
|
|
|
if (!length)
|
|
|
|
return true; // 0 sized packet will blow our connex
|
|
|
|
|
2020-06-13 19:46:12 +00:00
|
|
|
sent = SDLNet_TCP_Send(m_socket, data, length);
|
|
|
|
|
|
|
|
if (sent < length) {
|
|
|
|
SDLNet_TCP_Close(m_socket);
|
|
|
|
m_socket = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:55:38 +00:00
|
|
|
return true;
|
2020-06-13 19:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TCPSend::Connected()
|
|
|
|
{
|
|
|
|
return m_socket != 0;
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:55:38 +00:00
|
|
|
bool TCPSend::Connect()
|
2020-06-13 19:46:12 +00:00
|
|
|
{
|
|
|
|
IPaddress ip;
|
|
|
|
int result = SDLNet_ResolveHost(&ip, m_ip.c_str(), m_port);
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
m_socket = SDLNet_TCP_Open(&ip);
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:55:38 +00:00
|
|
|
return Connected();
|
2020-06-13 19:46:12 +00:00
|
|
|
}
|