mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-04 20:31:01 +01:00
fix(DatagramSocket): Socket::available does not always return correct value for UDP #3589
This commit is contained in:
@@ -610,6 +610,13 @@ int SocketImpl::available()
|
|||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
ioctl(FIONREAD, result);
|
ioctl(FIONREAD, result);
|
||||||
|
#if (POCO_OS != POCO_OS_LINUX)
|
||||||
|
if (type() == SOCKET_TYPE_DATAGRAM)
|
||||||
|
{
|
||||||
|
char buf[result];
|
||||||
|
result = recvfrom(sockfd(), buf, result, MSG_PEEK, NULL, NULL);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "Poco/Timespan.h"
|
#include "Poco/Timespan.h"
|
||||||
#include "Poco/Buffer.h"
|
#include "Poco/Buffer.h"
|
||||||
#include "Poco/Stopwatch.h"
|
#include "Poco/Stopwatch.h"
|
||||||
|
#include "Poco/Thread.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ using Poco::Stopwatch;
|
|||||||
using Poco::TimeoutException;
|
using Poco::TimeoutException;
|
||||||
using Poco::InvalidArgumentException;
|
using Poco::InvalidArgumentException;
|
||||||
using Poco::IOException;
|
using Poco::IOException;
|
||||||
|
using Poco::Thread;
|
||||||
|
|
||||||
|
|
||||||
DatagramSocketTest::DatagramSocketTest(const std::string& name): CppUnit::TestCase(name)
|
DatagramSocketTest::DatagramSocketTest(const std::string& name): CppUnit::TestCase(name)
|
||||||
@@ -120,6 +122,25 @@ void DatagramSocketTest::testEchoBuffer()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DatagramSocketTest::testReceiveFromAvailable()
|
||||||
|
{
|
||||||
|
UDPEchoServer echoServer(SocketAddress("127.0.0.1", 0));
|
||||||
|
DatagramSocket ss(SocketAddress::IPv4);
|
||||||
|
int n = ss.sendTo("hello", 5, SocketAddress("127.0.0.1", echoServer.port()));
|
||||||
|
assertTrue (n == 5);
|
||||||
|
Thread::sleep(100);
|
||||||
|
char buffer[256];
|
||||||
|
SocketAddress sa;
|
||||||
|
assertTrue (ss.available() == 5);
|
||||||
|
n = ss.receiveFrom(buffer, sizeof(buffer), sa);
|
||||||
|
assertTrue (sa.host() == echoServer.address().host());
|
||||||
|
assertTrue (sa.port() == echoServer.port());
|
||||||
|
assertTrue (n == 5);
|
||||||
|
assertTrue (std::string(buffer, n) == "hello");
|
||||||
|
ss.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DatagramSocketTest::testSendToReceiveFrom()
|
void DatagramSocketTest::testSendToReceiveFrom()
|
||||||
{
|
{
|
||||||
UDPEchoServer echoServer(SocketAddress("127.0.0.1", 0));
|
UDPEchoServer echoServer(SocketAddress("127.0.0.1", 0));
|
||||||
@@ -800,6 +821,7 @@ CppUnit::Test* DatagramSocketTest::suite()
|
|||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testEcho);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testEcho);
|
||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testMoveDatagramSocket);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testMoveDatagramSocket);
|
||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testEchoBuffer);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testEchoBuffer);
|
||||||
|
CppUnit_addTest(pSuite, DatagramSocketTest, testReceiveFromAvailable);
|
||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testSendToReceiveFrom);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testSendToReceiveFrom);
|
||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testUnbound);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testUnbound);
|
||||||
CppUnit_addTest(pSuite, DatagramSocketTest, testReuseAddressPortWildcard);
|
CppUnit_addTest(pSuite, DatagramSocketTest, testReuseAddressPortWildcard);
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public:
|
|||||||
void testEcho();
|
void testEcho();
|
||||||
void testMoveDatagramSocket();
|
void testMoveDatagramSocket();
|
||||||
void testEchoBuffer();
|
void testEchoBuffer();
|
||||||
|
void testReceiveFromAvailable();
|
||||||
void testSendToReceiveFrom();
|
void testSendToReceiveFrom();
|
||||||
void testUnbound();
|
void testUnbound();
|
||||||
void testReuseAddressPortWildcard();
|
void testReuseAddressPortWildcard();
|
||||||
|
|||||||
Reference in New Issue
Block a user