diff --git a/YSFGateway/Network.cpp b/YSFGateway/Network.cpp index b7ebf96..80ed7c3 100644 --- a/YSFGateway/Network.cpp +++ b/YSFGateway/Network.cpp @@ -112,9 +112,6 @@ bool CNetwork::writePoll() buffer[12U] = ' '; buffer[13U] = ' '; - if (m_debug) - CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U); - return m_socket.write(buffer, 14U, m_address, m_port); } diff --git a/YSFParrot/Network.cpp b/YSFParrot/Network.cpp index 5fbd2cc..3f5c785 100644 --- a/YSFParrot/Network.cpp +++ b/YSFParrot/Network.cpp @@ -31,8 +31,7 @@ m_socket(port), m_address(), m_port(0U), m_debug(debug), -m_buffer(1000U, "YSF Network"), -m_pollTimer(1000U, 5U) +m_buffer(1000U, "YSF Network") { } @@ -44,16 +43,14 @@ bool CNetwork::open() { ::fprintf(stdout, "Opening YSF network connection\n"); - if (m_address.s_addr == INADDR_NONE) - return false; - - m_pollTimer.start(); - return m_socket.open(); } bool CNetwork::write(const unsigned char* data) { + if (m_port == 0U) + return true; + assert(data != NULL); if (m_debug) @@ -62,7 +59,7 @@ bool CNetwork::write(const unsigned char* data) return m_socket.write(data, 155U, m_address, m_port); } -bool CNetwork::writePoll() +bool CNetwork::writePoll(const in_addr& address, unsigned int port) { unsigned char buffer[20U]; @@ -82,20 +79,11 @@ bool CNetwork::writePoll() buffer[12U] = ' '; buffer[13U] = ' '; - if (m_debug) - CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U); - - return m_socket.write(buffer, 14U, m_address, m_port); + return m_socket.write(buffer, 14U, address, port); } void CNetwork::clock(unsigned int ms) { - m_pollTimer.clock(ms); - if (m_pollTimer.hasExpired()) { - writePoll(); - m_pollTimer.start(); - } - unsigned char buffer[BUFFER_LENGTH]; in_addr address; @@ -104,12 +92,11 @@ void CNetwork::clock(unsigned int ms) if (length <= 0) return; - m_address.s_addr = address.s_addr; - m_port = port; - - // Ignore incoming polls - if (::memcmp(buffer, "YSFP", 4U) == 0) + // Handle incoming polls + if (::memcmp(buffer, "YSFP", 4U) == 0) { + writePoll(address, port); return; + } // Handle the status command if (::memcmp(buffer, "YSFS", 4U) == 0) { @@ -123,6 +110,9 @@ void CNetwork::clock(unsigned int ms) if (::memcmp(buffer, "YSFD", 4U) != 0) return; + m_address.s_addr = address.s_addr; + m_port = port; + if (m_debug) CUtils::dump(1U, "YSF Network Data Received", buffer, length); @@ -141,6 +131,11 @@ unsigned int CNetwork::read(unsigned char* data) return 155U; } +void CNetwork::end() +{ + m_port = 0U; +} + void CNetwork::close() { m_socket.close(); diff --git a/YSFParrot/Network.h b/YSFParrot/Network.h index 8e062eb..16501ea 100644 --- a/YSFParrot/Network.h +++ b/YSFParrot/Network.h @@ -22,7 +22,6 @@ #include "YSFDefines.h" #include "RingBuffer.h" #include "UDPSocket.h" -#include "Timer.h" #include #include @@ -38,6 +37,8 @@ public: unsigned int read(unsigned char* data); + void end(); + void close(); void clock(unsigned int ms); @@ -48,9 +49,8 @@ private: unsigned int m_port; bool m_debug; CRingBuffer m_buffer; - CTimer m_pollTimer; - bool writePoll(); + bool writePoll(const in_addr& address, unsigned int port); }; #endif diff --git a/YSFParrot/Parrot.cpp b/YSFParrot/Parrot.cpp index ce0a3b5..865eb36 100644 --- a/YSFParrot/Parrot.cpp +++ b/YSFParrot/Parrot.cpp @@ -57,6 +57,12 @@ void CParrot::end() m_ptr = 0U; } +void CParrot::clear() +{ + m_used = 0U; + m_ptr = 0U; +} + unsigned int CParrot::read(unsigned char* data) { assert(data != NULL); diff --git a/YSFParrot/Parrot.h b/YSFParrot/Parrot.h index 6cceb81..5d2a267 100644 --- a/YSFParrot/Parrot.h +++ b/YSFParrot/Parrot.h @@ -31,6 +31,8 @@ public: void end(); + void clear(); + private: unsigned char* m_data; unsigned int m_length; diff --git a/YSFParrot/YSFParrot.cpp b/YSFParrot/YSFParrot.cpp index ba7d47a..08420d3 100644 --- a/YSFParrot/YSFParrot.cpp +++ b/YSFParrot/YSFParrot.cpp @@ -21,6 +21,7 @@ #include "Parrot.h" #include "Network.h" #include "Version.h" +#include "Timer.h" #include #include @@ -105,6 +106,8 @@ void CYSFParrot::run() network.write(buffer); count++; } else { + parrot.clear(); + network.end(); turnaroundTimer.stop(); playing = false; count = wanted;