1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
YSFClients/YSFReflector/Network.cpp

149 lines
3.4 KiB

9 years ago
/*
* Copyright (C) 2009-2014,2016,2020 by Jonathan Naylor G4KLX
9 years ago
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "YSFDefines.h"
#include "Network.h"
#include "Utils.h"
4 years ago
#include "Log.h"
9 years ago
#include <cstdio>
#include <cassert>
#include <cstring>
CNetwork::CNetwork(unsigned int port, unsigned int id, const std::string& name, const std::string& description, bool debug) :
m_socket(),
m_port(port),
m_id(id),
m_name(name),
m_description(description),
9 years ago
m_callsign(),
m_debug(debug),
m_status(NULL)
9 years ago
{
m_name.resize(16U, ' ');
m_description.resize(14U, ' ');
m_status = new unsigned char[50U];
9 years ago
}
CNetwork::~CNetwork()
{
delete[] m_status;
9 years ago
}
4 years ago
bool CNetwork::open()
9 years ago
{
4 years ago
LogInfo("Opening YSF network connection");
9 years ago
bool status = false;
unsigned int af[] = {AF_INET, AF_INET6};
for (int i = 0; i < UDP_SOCKET_MAX &&
i < (sizeof(af) / sizeof(unsigned int)); i++)
status |= m_socket.open(i, af[i], "", m_port);
return status;
9 years ago
}
4 years ago
bool CNetwork::writeData(const unsigned char* data, const sockaddr_storage& addr, unsigned int addrLen)
9 years ago
{
assert(data != NULL);
if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", data, 155U);
4 years ago
return m_socket.write(data, 155U, addr, addrLen);
9 years ago
}
4 years ago
bool CNetwork::writePoll(const sockaddr_storage& addr, unsigned int addrLen)
9 years ago
{
unsigned char buffer[20U];
buffer[0] = 'Y';
buffer[1] = 'S';
buffer[2] = 'F';
buffer[3] = 'P';
buffer[4U] = 'R';
buffer[5U] = 'E';
buffer[6U] = 'F';
buffer[7U] = 'L';
buffer[8U] = 'E';
buffer[9U] = 'C';
buffer[10U] = 'T';
buffer[11U] = 'O';
buffer[12U] = 'R';
buffer[13U] = ' ';
if (m_debug)
CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U);
4 years ago
return m_socket.write(buffer, 14U, addr, addrLen);
9 years ago
}
4 years ago
unsigned int CNetwork::readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen)
9 years ago
{
assert(data != NULL);
assert(length > 0U);
4 years ago
int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0)
return 0U;
9 years ago
// Handle incoming status requests
if (::memcmp(data, "YSFS", 4U) == 0) {
4 years ago
m_socket.write(m_status, 42U, addr, addrLen);
return 0U;
}
9 years ago
if (m_debug)
CUtils::dump(1U, "YSF Network Data Received", data, len);
9 years ago
return len;
9 years ago
}
void CNetwork::setCount(unsigned int count)
{
if (count > 999U)
count = 999U;
unsigned int hash = m_id;
if (hash == 0U) {
for (unsigned int i = 0U; i < m_name.size(); i++) {
hash += m_name.at(i);
hash += (hash << 10);
hash ^= (hash >> 6);
}
// Final avalanche
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
}
::sprintf((char*)m_status, "YSFS%05u%16.16s%14.14s%03u", hash % 100000U, m_name.c_str(), m_description.c_str(), count);
}
9 years ago
void CNetwork::close()
{
m_socket.close();
4 years ago
LogInfo("Closing YSF network connection");
9 years ago
}