1
0
Fork 0

Lots of small changes.

ycs232-kbc
Jonathan Naylor 7 years ago
parent 4edd2ebdd1
commit 9719b1d269

@ -51,7 +51,6 @@ m_txFrequency(0U),
m_power(0U), m_power(0U),
m_latitude(0.0F), m_latitude(0.0F),
m_longitude(0.0F), m_longitude(0.0F),
m_locator(),
m_height(0), m_height(0),
m_name(), m_name(),
m_description(), m_description(),
@ -159,8 +158,6 @@ bool CConf::read()
m_latitude = float(::atof(value)); m_latitude = float(::atof(value));
else if (::strcmp(key, "Longitude") == 0) else if (::strcmp(key, "Longitude") == 0)
m_longitude = float(::atof(value)); m_longitude = float(::atof(value));
else if (::strcmp(key, "Locator") == 0)
m_locator = value;
else if (::strcmp(key, "Height") == 0) else if (::strcmp(key, "Height") == 0)
m_height = ::atoi(value); m_height = ::atoi(value);
else if (::strcmp(key, "Name") == 0) else if (::strcmp(key, "Name") == 0)
@ -292,11 +289,6 @@ float CConf::getLongitude() const
return m_longitude; return m_longitude;
} }
std::string CConf::getLocator() const
{
return m_locator;
}
int CConf::getHeight() const int CConf::getHeight() const
{ {
return m_height; return m_height;

@ -46,7 +46,6 @@ public:
unsigned int getPower() const; unsigned int getPower() const;
float getLatitude() const; float getLatitude() const;
float getLongitude() const; float getLongitude() const;
std::string getLocator() const;
int getHeight() const; int getHeight() const;
std::string getName() const; std::string getName() const;
std::string getDescription() const; std::string getDescription() const;
@ -99,7 +98,6 @@ private:
unsigned int m_power; unsigned int m_power;
float m_latitude; float m_latitude;
float m_longitude; float m_longitude;
std::string m_locator;
int m_height; int m_height;
std::string m_name; std::string m_name;
std::string m_description; std::string m_description;

@ -34,23 +34,23 @@ m_socket(port),
m_debug(debug), m_debug(debug),
m_address(), m_address(),
m_port(0U), m_port(0U),
m_ping(NULL),
m_info(NULL), m_info(NULL),
m_callsign(callsign),
m_reflector(), m_reflector(),
m_buffer(1000U, "FCS Network Buffer"), m_buffer(1000U, "FCS Network Buffer"),
m_n(0U) m_n(0U)
{ {
m_info = new unsigned char[100U]; m_info = new unsigned char[100U];
::memset(m_info, ' ', 100U);
m_callsign.resize(6U, ' ');
::sprintf((char*)m_info, "%9u%9u%-6.6s%-12.12s%7u", rxFrequency, txFrequency, locator.c_str(), FCS_VERSION, id); ::sprintf((char*)m_info, "%9u%9u%-6.6s%-12.12s%7u", rxFrequency, txFrequency, locator.c_str(), FCS_VERSION, id);
m_ping = new unsigned char[25U];
::sprintf((char*)m_ping, "PING%6.6s ", callsign.c_str());
} }
CFCSNetwork::~CFCSNetwork() CFCSNetwork::~CFCSNetwork()
{ {
delete[] m_info; delete[] m_info;
delete[] m_ping;
} }
bool CFCSNetwork::open() bool CFCSNetwork::open()
@ -73,12 +73,12 @@ void CFCSNetwork::clearDestination()
m_port = 0U; m_port = 0U;
} }
bool CFCSNetwork::write(const unsigned char* data) void CFCSNetwork::write(const unsigned char* data)
{ {
assert(data != NULL); assert(data != NULL);
if (m_port == 0U) if (m_port == 0U)
return true; return;
unsigned char buffer[130U]; unsigned char buffer[130U];
::memset(buffer + 0U, ' ', 130U); ::memset(buffer + 0U, ' ', 130U);
@ -88,55 +88,45 @@ bool CFCSNetwork::write(const unsigned char* data)
if (m_debug) if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U); CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U);
return m_socket.write(buffer, 130U, m_address, m_port); m_socket.write(buffer, 130U, m_address, m_port);
} }
bool CFCSNetwork::writeLink(const std::string& reflector) void CFCSNetwork::writeLink(const std::string& reflector)
{ {
if (m_port == 0U) { if (m_port == 0U) {
std::string name = reflector.substr(0U, 6U); std::string name = reflector.substr(0U, 6U);
if (m_addresses.count(name) == 0U) { if (m_addresses.count(name) == 0U) {
LogError("Unknown FCS reflector - %s", name.c_str()); LogError("Unknown FCS reflector - %s", name.c_str());
return false; return;
} }
m_address = m_addresses[name]; m_address = m_addresses[name];
if (m_address.s_addr == INADDR_NONE) { if (m_address.s_addr == INADDR_NONE) {
LogError("FCS reflector %s has no address", name.c_str()); LogError("FCS reflector %s has no address", name.c_str());
return false; return;
} }
} }
m_port = FCS_PORT; m_port = FCS_PORT;
m_reflector = reflector; m_reflector = reflector;
m_reflector.resize(8U, ' ');
unsigned char buffer[25U];
::memset(buffer + 0U, ' ', 25U);
::memcpy(buffer + 0U, "PING", 4U);
::memcpy(buffer + 4U, m_callsign.c_str(), 6U);
::memcpy(buffer + 10U, m_reflector.c_str(), 6U);
if (m_debug) ::memcpy(m_ping + 10U, m_reflector.c_str(), 8U);
CUtils::dump(1U, "FCS Network Data Sent", buffer, 25U);
return m_socket.write(buffer, 25U, m_address, m_port); writePing();
} }
bool CFCSNetwork::writeUnlink() void CFCSNetwork::writeUnlink(unsigned int count)
{ {
if (m_port == 0U) if (m_port == 0U)
return true; return;
return m_socket.write((unsigned char*)"CLOSE ", 11U, m_address, m_port); for (unsigned int i = 0U; i < count; i++)
m_socket.write((unsigned char*)"CLOSE ", 11U, m_address, m_port);
} }
void CFCSNetwork::clock(unsigned int ms) void CFCSNetwork::clock(unsigned int ms)
{ {
if (m_port == 0U)
return;
unsigned char buffer[BUFFER_LENGTH]; unsigned char buffer[BUFFER_LENGTH];
in_addr address; in_addr address;
@ -145,6 +135,9 @@ void CFCSNetwork::clock(unsigned int ms)
if (length <= 0) if (length <= 0)
return; return;
if (m_port == 0U)
return;
if (address.s_addr != m_address.s_addr || port != m_port) if (address.s_addr != m_address.s_addr || port != m_port)
return; return;
@ -155,7 +148,7 @@ void CFCSNetwork::clock(unsigned int ms)
writeInfo(); writeInfo();
if (length == 130) if (length == 130)
m_buffer.addData(buffer, 120U); m_buffer.addData(buffer, 130U);
} }
unsigned int CFCSNetwork::read(unsigned char* data) unsigned int CFCSNetwork::read(unsigned char* data)
@ -165,9 +158,14 @@ unsigned int CFCSNetwork::read(unsigned char* data)
if (m_buffer.isEmpty()) if (m_buffer.isEmpty())
return 0U; return 0U;
unsigned char buffer[130U];
m_buffer.getData(buffer, 130U);
::memcpy(data + 0U, "YSFDDB0SAT DB0SAT-RPTALL ", 35U); ::memcpy(data + 0U, "YSFDDB0SAT DB0SAT-RPTALL ", 35U);
::memcpy(data + 35U, buffer, 120U);
m_buffer.getData(data + 35U, 120U); // Put the reflector name as the via callsign.
::memcpy(data + 4U, buffer + 121U, 8U);
data[34U] = m_n; data[34U] = m_n;
m_n += 2U; m_n += 2U;
@ -192,3 +190,14 @@ void CFCSNetwork::writeInfo()
m_socket.write(m_info, 100U, m_address, m_port); m_socket.write(m_info, 100U, m_address, m_port);
} }
void CFCSNetwork::writePing()
{
if (m_port == 0U)
return;
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_ping, 25U);
m_socket.write(m_ping, 25U, m_address, m_port);
}

@ -36,11 +36,11 @@ public:
void clearDestination(); void clearDestination();
bool write(const unsigned char* data); void write(const unsigned char* data);
bool writeLink(const std::string& reflector); void writeLink(const std::string& reflector);
bool writeUnlink(); void writeUnlink(unsigned int count = 1U);
unsigned int read(unsigned char* data); unsigned int read(unsigned char* data);
@ -53,14 +53,15 @@ private:
bool m_debug; bool m_debug;
in_addr m_address; in_addr m_address;
unsigned int m_port; unsigned int m_port;
unsigned char* m_ping;
unsigned char* m_info; unsigned char* m_info;
std::string m_callsign;
std::string m_reflector; std::string m_reflector;
CRingBuffer<unsigned char> m_buffer; CRingBuffer<unsigned char> m_buffer;
std::map<std::string, in_addr> m_addresses; std::map<std::string, in_addr> m_addresses;
unsigned char m_n; unsigned char m_n;
void writeInfo(); void writeInfo();
void writePing();
}; };
#endif #endif

@ -46,6 +46,7 @@ const char* DEFAULT_INI_FILE = "/etc/YSFGateway.ini";
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <clocale> #include <clocale>
#include <cmath>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -86,8 +87,7 @@ m_fcsNetwork(NULL),
m_linkType(LINK_NONE), m_linkType(LINK_NONE),
m_exclude(false), m_exclude(false),
m_inactivityTimer(1000U), m_inactivityTimer(1000U),
m_lostTimer(1000U, 120U), m_lostTimer(1000U, 120U)
m_ysfPollTimer(1000U, 5U)
{ {
} }
@ -200,7 +200,7 @@ int CYSFGateway::run()
unsigned int txFrequency = m_conf.getTxFrequency(); unsigned int txFrequency = m_conf.getTxFrequency();
unsigned int rxFrequency = m_conf.getRxFrequency(); unsigned int rxFrequency = m_conf.getRxFrequency();
std::string locator = m_conf.getLocator(); std::string locator = calculateLocator();
unsigned int id = m_conf.getId(); unsigned int id = m_conf.getId();
unsigned int fcsPort = m_conf.getFCSNetworkPort(); unsigned int fcsPort = m_conf.getFCSNetworkPort();
@ -250,15 +250,12 @@ int CYSFGateway::run()
LogMessage("Automatic connection to %5.5s - \"%s\"", reflector->m_id.c_str(), reflector->m_name.c_str()); LogMessage("Automatic connection to %5.5s - \"%s\"", reflector->m_id.c_str(), reflector->m_name.c_str());
m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port);
m_ysfNetwork->writePoll(); m_ysfNetwork->writePoll(3U);
m_ysfNetwork->writePoll();
m_ysfNetwork->writePoll();
if (!revert) if (!revert)
m_inactivityTimer.start(); m_inactivityTimer.start();
m_lostTimer.start(); m_lostTimer.start();
m_ysfPollTimer.start();
m_linkType = LINK_YSF; m_linkType = LINK_YSF;
} }
@ -358,30 +355,22 @@ int CYSFGateway::run()
if (m_wiresX != NULL) if (m_wiresX != NULL)
m_wiresX->processConnect(reflector); m_wiresX->processConnect(reflector);
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port);
m_ysfNetwork->writePoll(); m_ysfNetwork->writePoll(3U);
m_ysfNetwork->writePoll();
m_ysfNetwork->writePoll();
m_lostTimer.start(); m_lostTimer.start();
m_ysfPollTimer.start();
} else { } else {
LogMessage("Disconnecting due to inactivity"); LogMessage("Disconnecting due to inactivity");
if (m_wiresX != NULL) if (m_wiresX != NULL)
m_wiresX->processDisconnect(); m_wiresX->processDisconnect();
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
m_ysfNetwork->clearDestination(); m_ysfNetwork->clearDestination();
m_lostTimer.stop(); m_lostTimer.stop();
m_ysfPollTimer.stop();
m_linkType = LINK_NONE; m_linkType = LINK_NONE;
} }
@ -405,17 +394,10 @@ int CYSFGateway::run()
m_inactivityTimer.stop(); m_inactivityTimer.stop();
m_lostTimer.stop(); m_lostTimer.stop();
m_ysfPollTimer.stop();
m_linkType = LINK_NONE; m_linkType = LINK_NONE;
} }
m_ysfPollTimer.clock(ms);
if (m_ysfPollTimer.isRunning() && m_ysfPollTimer.hasExpired()) {
m_ysfNetwork->writePoll();
m_ysfPollTimer.start();
}
if (ms < 5U) if (ms < 5U)
CThread::sleep(5U); CThread::sleep(5U);
} }
@ -472,15 +454,11 @@ void CYSFGateway::processWiresX(const unsigned char* buffer, unsigned char fi, u
WX_STATUS status = m_wiresX->process(buffer + 35U, buffer + 14U, fi, dt, fn, ft); WX_STATUS status = m_wiresX->process(buffer + 35U, buffer + 14U, fi, dt, fn, ft);
switch (status) { switch (status) {
case WXS_CONNECT_YSF: { case WXS_CONNECT_YSF: {
if (m_linkType == LINK_YSF) { if (m_linkType == LINK_YSF)
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
}
if (m_linkType == LINK_FCS) { if (m_linkType == LINK_FCS) {
m_fcsNetwork->writeUnlink(); m_fcsNetwork->writeUnlink(3U);
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
m_fcsNetwork->clearDestination(); m_fcsNetwork->clearDestination();
} }
@ -488,13 +466,10 @@ void CYSFGateway::processWiresX(const unsigned char* buffer, unsigned char fi, u
LogMessage("Connect to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); LogMessage("Connect to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U);
m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port);
m_ysfNetwork->writePoll(); m_ysfNetwork->writePoll(3U);
m_ysfNetwork->writePoll();
m_ysfNetwork->writePoll();
m_inactivityTimer.start(); m_inactivityTimer.start();
m_lostTimer.start(); m_lostTimer.start();
m_ysfPollTimer.start();
m_linkType = LINK_YSF; m_linkType = LINK_YSF;
} }
@ -503,23 +478,18 @@ void CYSFGateway::processWiresX(const unsigned char* buffer, unsigned char fi, u
if (m_linkType == LINK_YSF) { if (m_linkType == LINK_YSF) {
LogMessage("Disconnect has been requested by %10.10s", buffer + 14U); LogMessage("Disconnect has been requested by %10.10s", buffer + 14U);
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
m_ysfNetwork->clearDestination(); m_ysfNetwork->clearDestination();
m_inactivityTimer.stop(); m_inactivityTimer.stop();
m_lostTimer.stop(); m_lostTimer.stop();
m_ysfPollTimer.stop();
m_linkType = LINK_NONE; m_linkType = LINK_NONE;
} }
if (m_linkType == LINK_FCS) { if (m_linkType == LINK_FCS) {
LogMessage("Disconnect has been requested by %10.10s", buffer + 14U); LogMessage("Disconnect has been requested by %10.10s", buffer + 14U);
m_fcsNetwork->writeUnlink(); m_fcsNetwork->writeUnlink(3U);
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
m_fcsNetwork->clearDestination(); m_fcsNetwork->clearDestination();
m_inactivityTimer.stop(); m_inactivityTimer.stop();
@ -554,28 +524,21 @@ void CYSFGateway::processDTMF(const unsigned char* buffer, unsigned char dt)
if (m_wiresX != NULL) if (m_wiresX != NULL)
m_wiresX->processConnect(reflector); m_wiresX->processConnect(reflector);
if (m_linkType == LINK_YSF) { if (m_linkType == LINK_YSF)
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
}
if (m_linkType == LINK_FCS) { if (m_linkType == LINK_FCS) {
m_fcsNetwork->writeUnlink(); m_fcsNetwork->writeUnlink(3U);
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
m_fcsNetwork->clearDestination(); m_fcsNetwork->clearDestination();
} }
LogMessage("Connect via DTMF to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); LogMessage("Connect via DTMF to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U);
m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port);
m_ysfNetwork->writePoll(); m_ysfNetwork->writePoll(3U);
m_ysfNetwork->writePoll();
m_ysfNetwork->writePoll();
m_inactivityTimer.start(); m_inactivityTimer.start();
m_lostTimer.start(); m_lostTimer.start();
m_ysfPollTimer.start();
m_linkType = LINK_YSF; m_linkType = LINK_YSF;
} }
@ -589,22 +552,14 @@ void CYSFGateway::processDTMF(const unsigned char* buffer, unsigned char dt)
id = "FCS00" + id.at(0U) + id.at(1U) + id.at(2U); id = "FCS00" + id.at(0U) + id.at(1U) + id.at(2U);
if (m_linkType == LINK_YSF) { if (m_linkType == LINK_YSF) {
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
m_ysfNetwork->clearDestination(); m_ysfNetwork->clearDestination();
m_ysfPollTimer.stop();
}
if (m_linkType == LINK_FCS) {
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
} }
if (m_linkType == LINK_FCS)
m_fcsNetwork->writeUnlink(3U);
LogMessage("Connect via DTMF to %s has been requested by %10.10s", id.c_str(), buffer + 14U); LogMessage("Connect via DTMF to %s has been requested by %10.10s", id.c_str(), buffer + 14U);
m_fcsNetwork->writeLink(id);
m_fcsNetwork->writeLink(id);
m_fcsNetwork->writeLink(id); m_fcsNetwork->writeLink(id);
m_inactivityTimer.start(); m_inactivityTimer.start();
@ -620,23 +575,18 @@ void CYSFGateway::processDTMF(const unsigned char* buffer, unsigned char dt)
LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U); LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U);
m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(3U);
m_ysfNetwork->writeUnlink();
m_ysfNetwork->writeUnlink();
m_ysfNetwork->clearDestination(); m_ysfNetwork->clearDestination();
m_inactivityTimer.stop(); m_inactivityTimer.stop();
m_lostTimer.stop(); m_lostTimer.stop();
m_ysfPollTimer.stop();
m_linkType = LINK_NONE; m_linkType = LINK_NONE;
} }
if (m_linkType == LINK_FCS) { if (m_linkType == LINK_FCS) {
LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U); LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U);
m_fcsNetwork->writeUnlink(); m_fcsNetwork->writeUnlink(3U);
m_fcsNetwork->writeUnlink();
m_fcsNetwork->writeUnlink();
m_fcsNetwork->clearDestination(); m_fcsNetwork->clearDestination();
m_inactivityTimer.stop(); m_inactivityTimer.stop();
@ -649,3 +599,53 @@ void CYSFGateway::processDTMF(const unsigned char* buffer, unsigned char dt)
break; break;
} }
} }
std::string CYSFGateway::calculateLocator()
{
std::string locator;
float latitude = m_conf.getLatitude();
float longitude = m_conf.getLongitude();
if (latitude < -90.0F || latitude > 90.0F)
return "AA00AA";
if (longitude < -360.0F || longitude > 360.0F)
return "AA00AA";
latitude += 90.0F;
if (longitude > 180.0F)
longitude -= 360.0F;
if (longitude < -180.0F)
longitude += 360.0F;
longitude += 180.0F;
float lon = ::floor(longitude / 20.0F);
float lat = ::floor(latitude / 10.0F);
locator += 'A' + (unsigned int)lon;
locator += 'A' + (unsigned int)lat;
longitude -= lon * 20.0F;
latitude -= lat * 10.0F;
lon = ::floor(longitude / 2.0F);
lat = ::floor(latitude / 1.0F);
locator += '0' + (unsigned int)lon;
locator += '0' + (unsigned int)lat;
longitude -= lon * 2.0F;
latitude -= lat * 1.0F;
lon = ::floor(longitude / (2.0F / 24.0F));
lat = ::floor(latitude / (1.0F / 24.0F));
locator += 'A' + (unsigned int)lon;
locator += 'A' + (unsigned int)lat;
return locator;
}

@ -56,8 +56,8 @@ private:
bool m_exclude; bool m_exclude;
CTimer m_inactivityTimer; CTimer m_inactivityTimer;
CTimer m_lostTimer; CTimer m_lostTimer;
CTimer m_ysfPollTimer;
std::string calculateLocator();
void processWiresX(const unsigned char* buffer, unsigned char fi, unsigned char dt, unsigned char fn, unsigned char ft); void processWiresX(const unsigned char* buffer, unsigned char fi, unsigned char dt, unsigned char fn, unsigned char ft);
void processDTMF(const unsigned char* buffer, unsigned char dt); void processDTMF(const unsigned char* buffer, unsigned char dt);
void createGPS(); void createGPS();

@ -15,7 +15,6 @@ TXFrequency=439475000
Power=1 Power=1
Latitude=0.0 Latitude=0.0
Longitude=0.0 Longitude=0.0
Locator=IO90TT
Height=0 Height=0
Name=Nowhere Name=Nowhere
Description=Multi-Mode Repeater Description=Multi-Mode Repeater

@ -34,7 +34,8 @@ m_address(),
m_port(0U), m_port(0U),
m_poll(NULL), m_poll(NULL),
m_unlink(NULL), m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer") m_buffer(1000U, "YSF Network Buffer"),
m_pollTimer(1000U, 5U)
{ {
m_poll = new unsigned char[14U]; m_poll = new unsigned char[14U];
::memcpy(m_poll + 0U, "YSFP", 4U); ::memcpy(m_poll + 0U, "YSFP", 4U);
@ -58,7 +59,8 @@ m_address(),
m_port(0U), m_port(0U),
m_poll(NULL), m_poll(NULL),
m_unlink(NULL), m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer") m_buffer(1000U, "YSF Network Buffer"),
m_pollTimer(1000U, 5U)
{ {
m_poll = new unsigned char[14U]; m_poll = new unsigned char[14U];
::memcpy(m_poll + 0U, "YSFP", 4U); ::memcpy(m_poll + 0U, "YSFP", 4U);
@ -97,42 +99,47 @@ void CYSFNetwork::clearDestination()
{ {
m_address.s_addr = INADDR_NONE; m_address.s_addr = INADDR_NONE;
m_port = 0U; m_port = 0U;
m_pollTimer.stop();
} }
bool CYSFNetwork::write(const unsigned char* data) void CYSFNetwork::write(const unsigned char* data)
{ {
assert(data != NULL); assert(data != NULL);
if (m_port == 0U) if (m_port == 0U)
return true; return;
if (m_debug) if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", data, 155U); CUtils::dump(1U, "YSF Network Data Sent", data, 155U);
return m_socket.write(data, 155U, m_address, m_port); m_socket.write(data, 155U, m_address, m_port);
} }
bool CYSFNetwork::writePoll() void CYSFNetwork::writePoll(unsigned int count)
{ {
if (m_port == 0U) if (m_port == 0U)
return true; return;
m_pollTimer.start();
return m_socket.write(m_poll, 14U, m_address, m_port); for (unsigned int i = 0U; i < count; i++)
m_socket.write(m_poll, 14U, m_address, m_port);
} }
bool CYSFNetwork::writeUnlink() void CYSFNetwork::writeUnlink(unsigned int count)
{ {
m_pollTimer.stop();
if (m_port == 0U) if (m_port == 0U)
return true; return;
return m_socket.write(m_unlink, 14U, m_address, m_port); for (unsigned int i = 0U; i < count; i++)
m_socket.write(m_unlink, 14U, m_address, m_port);
} }
void CYSFNetwork::clock(unsigned int ms) void CYSFNetwork::clock(unsigned int ms)
{ {
if (m_port == 0U)
return;
unsigned char buffer[BUFFER_LENGTH]; unsigned char buffer[BUFFER_LENGTH];
in_addr address; in_addr address;
@ -141,6 +148,13 @@ void CYSFNetwork::clock(unsigned int ms)
if (length <= 0) if (length <= 0)
return; return;
if (m_port == 0U)
return;
m_pollTimer.clock(ms);
if (m_pollTimer.isRunning() && m_pollTimer.hasExpired())
writePoll();
if (address.s_addr != m_address.s_addr || port != m_port) if (address.s_addr != m_address.s_addr || port != m_port)
return; return;

@ -22,6 +22,7 @@
#include "YSFDefines.h" #include "YSFDefines.h"
#include "UDPSocket.h" #include "UDPSocket.h"
#include "RingBuffer.h" #include "RingBuffer.h"
#include "Timer.h"
#include <cstdint> #include <cstdint>
#include <string> #include <string>
@ -37,10 +38,10 @@ public:
void setDestination(const in_addr& address, unsigned int port); void setDestination(const in_addr& address, unsigned int port);
void clearDestination(); void clearDestination();
bool write(const unsigned char* data); void write(const unsigned char* data);
bool writePoll(); void writePoll(unsigned int count = 1U);
bool writeUnlink(); void writeUnlink(unsigned int count = 1U);
unsigned int read(unsigned char* data); unsigned int read(unsigned char* data);
@ -56,6 +57,7 @@ private:
unsigned char* m_poll; unsigned char* m_poll;
unsigned char* m_unlink; unsigned char* m_unlink;
CRingBuffer<unsigned char> m_buffer; CRingBuffer<unsigned char> m_buffer;
CTimer m_pollTimer;
}; };
#endif #endif

Loading…
Cancel
Save