Skip to content

Commit

Permalink
Solved many TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Feb 22, 2024
1 parent 200d367 commit eff8a8f
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 67 deletions.
5 changes: 0 additions & 5 deletions udpcap/include/udpcap/udpcap_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ namespace Udpcap
*/
UDPCAP_EXPORT bool setReceiveBufferSize(int receive_buffer_size);

/**
* @brief Returns whether there are datagrams ready to be read
*/
UDPCAP_EXPORT bool hasPendingDatagrams() const;

/**
* @brief Blocks for the given time until a packet arives and copies it to the given memory
*
Expand Down
2 changes: 0 additions & 2 deletions udpcap/src/udpcap_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ namespace Udpcap

bool UdpcapSocket::setReceiveBufferSize (int receive_buffer_size) { return udpcap_socket_private_->setReceiveBufferSize(receive_buffer_size); }

bool UdpcapSocket::hasPendingDatagrams () const { return udpcap_socket_private_->hasPendingDatagrams(); }

size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, long long timeout_ms, HostAddress* source_address, uint16_t* source_port, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, source_address, source_port, error); }
size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, long long timeout_ms, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, nullptr, nullptr, error); }
size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, -1, nullptr, nullptr, error); }
Expand Down
59 changes: 2 additions & 57 deletions udpcap/src/udpcap_socket_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ namespace Udpcap
{
close();
}
//UdpcapSocketPrivate::~UdpcapSocketPrivate()
//{
// // @todo: reinvestigate why it crashes on close. (Maybe check if i have implemented copy / move constructors properly)
// //close();
//}

bool UdpcapSocketPrivate::isValid() const
{
Expand Down Expand Up @@ -231,56 +226,6 @@ namespace Udpcap
return true;
}

bool UdpcapSocketPrivate::hasPendingDatagrams() const
{
if (!is_valid_)
{
// Invalid socket, cannot bind => fail!
LOG_DEBUG("Has Pending Datagrams error: Socket is invalid");
return false;
}

if (!bound_state_)
{
// Not bound => fail!
LOG_DEBUG("Has Pending Datagrams error: Socket is not bound");
return false;
}

// Lock the lists of open pcap devices in read-mode. We may use the handles, but not modify the lists themselfes.
const std::shared_lock<std::shared_mutex> pcap_devices_lists_lock(pcap_devices_lists_mutex_);

{
const std::lock_guard<std::mutex> pcap_callback_lock(pcap_devices_callback_mutex_);
if (pcap_devices_closed_)
{
// No open devices => fail!
LOG_DEBUG("Has Pending Datagrams error: Socket has been closed.");
return false;
}
}

if (pcap_win32_handles_.empty())
{
// No open devices => fail!
LOG_DEBUG("Has Pending Datagrams error: No open devices");
return false;
}

// Wait 0 ms for data
DWORD num_handles = static_cast<DWORD>(pcap_win32_handles_.size());
if (num_handles > MAXIMUM_WAIT_OBJECTS)
{
LOG_DEBUG("WARNING: Too many open Adapters. " + std::to_string(num_handles) + " adapters are open, only " + std::to_string(MAXIMUM_WAIT_OBJECTS) + " are supported.");
num_handles = MAXIMUM_WAIT_OBJECTS;
}

const DWORD wait_result = WaitForMultipleObjects(num_handles, pcap_win32_handles_.data(), static_cast<BOOL>(false), 0);

// Check if any HADNLE was in signaled state
return((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1));
}

size_t UdpcapSocketPrivate::receiveDatagram(char* data
, size_t max_len
, long long timeout_ms
Expand Down Expand Up @@ -319,7 +264,7 @@ namespace Udpcap
// Check for data on pcap devices until we are either out of time or have
// received a datagaram. A datagram may consist of multiple packaets in
// case of IP Fragmentation.
while (true) // TODO: respect the timeout parameter
while (true)
{
bool received_any_data = false;

Expand Down Expand Up @@ -724,7 +669,7 @@ namespace Udpcap

if (receive_buffer_size_ > 0)
{
pcap_set_buffer_size(pcap_handle, receive_buffer_size_); // TODO: the buffer size should probably not be zero by default. Currently (2024-01-31) it is.
pcap_set_buffer_size(pcap_handle, receive_buffer_size_);
}

const int errorcode = pcap_activate(pcap_handle); // TODO : If pcap_activate() fails, the pcap_t * is not closed and freed; it should be closed using pcap_close(3PCAP).
Expand Down
3 changes: 0 additions & 3 deletions udpcap/src/udpcap_socket_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ namespace Udpcap

bool setReceiveBufferSize(int buffer_size);

// TODO: Re-implement or remove. This is currently (2024-02-06) implemented faulty.
bool hasPendingDatagrams() const;

size_t receiveDatagram(char* data
, size_t max_len
, long long timeout_ms
Expand Down

0 comments on commit eff8a8f

Please sign in to comment.