mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-12 18:20:26 +01:00
- SocketIOChannel and tests
- some gcc warning fixes
This commit is contained in:
parent
722009b8be
commit
c91bd3323c
@ -30,7 +30,7 @@ objects = \
|
||||
RawSocket RawSocketImpl ICMPClient ICMPEventArgs ICMPPacket ICMPPacketImpl \
|
||||
ICMPSocket ICMPSocketImpl ICMPv4PacketImpl \
|
||||
RemoteSyslogChannel RemoteSyslogListener SMTPChannel \
|
||||
AsyncSocketChannel
|
||||
AsyncSocketChannel SocketIOChannel
|
||||
|
||||
target = PocoNet
|
||||
target_version = $(LIBVERSION)
|
||||
|
@ -1227,6 +1227,26 @@
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="IO"
|
||||
>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Net\SocketIOChannel.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannel.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
@ -1208,6 +1208,26 @@
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="IO"
|
||||
>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\Poco\Net\SocketIOChannel.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannel.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
@ -44,7 +44,6 @@
|
||||
#include "Poco/Mutex.h"
|
||||
#include "Poco/URI.h"
|
||||
#include "Poco/SingletonHolder.h"
|
||||
#include "Poco/SharedPtr.h"
|
||||
#include <map>
|
||||
|
||||
|
||||
|
138
Net/include/Poco/Net/SocketIOChannel.h
Normal file
138
Net/include/Poco/Net/SocketIOChannel.h
Normal file
@ -0,0 +1,138 @@
|
||||
//
|
||||
// SocketIOChannel.h
|
||||
//
|
||||
// $Id: //poco/Main/Data/include/Poco/Net/SocketIOChannel.h#1 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: IO
|
||||
// Module: SocketIOChannel
|
||||
//
|
||||
// Definition of the SocketIOChannel class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef Net_SocketIOChannel_INCLUDED
|
||||
#define Net_SocketIOChannel_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Net/Net.h"
|
||||
#include "Poco/IOChannel.h"
|
||||
#include "Poco/IOChannelConfig.h"
|
||||
#include "Poco/Net/Socket.h"
|
||||
#include "Poco/Net/SocketImpl.h"
|
||||
#include "Poco/Net/StreamSocket.h"
|
||||
#include "Poco/Net/DatagramSocket.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
class Net_API SocketIOChannel: public Poco::IOChannel
|
||||
/// Wraps a socket into IO channel interface.
|
||||
/// Both stream and datagram sockets are supported.
|
||||
{
|
||||
public:
|
||||
SocketIOChannel(IOChannelConfig* pConfig);
|
||||
/// Creates SocketIOChannel.
|
||||
|
||||
~SocketIOChannel();
|
||||
/// Destroys SocketIOChannel.
|
||||
|
||||
void open();
|
||||
/// Opens SocketIOChannel.
|
||||
|
||||
void close();
|
||||
/// Closes SocketIOChannel.
|
||||
|
||||
bool isStream();
|
||||
/// Returns true if SocketIOChannel currently wraps stream socket.
|
||||
|
||||
bool isDatagram();
|
||||
/// Returns true if SocketIOChannel currently wraps datagram socket.
|
||||
|
||||
private:
|
||||
SocketIOChannel(const SocketIOChannel&);
|
||||
const SocketIOChannel& operator=(const SocketIOChannel&);
|
||||
|
||||
void init();
|
||||
|
||||
int readData(char* pReadBuf, int length);
|
||||
int readData(char*& pReadBuf);
|
||||
int writeData(const char* buffer, int length);
|
||||
|
||||
Poco::Net::Socket& socket();
|
||||
Poco::Net::SocketImpl* socketImpl();
|
||||
|
||||
Poco::Net::Socket* _pSocket;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// inlines
|
||||
//
|
||||
|
||||
|
||||
inline Poco::Net::Socket& SocketIOChannel::socket()
|
||||
{
|
||||
if (!_pSocket) init();
|
||||
|
||||
return *_pSocket;
|
||||
}
|
||||
|
||||
|
||||
inline Poco::Net::SocketImpl* SocketIOChannel::socketImpl()
|
||||
{
|
||||
return socket().impl();
|
||||
}
|
||||
|
||||
|
||||
inline void SocketIOChannel::close()
|
||||
{
|
||||
socketImpl()->close();
|
||||
delete _pSocket;
|
||||
_pSocket = 0;
|
||||
}
|
||||
|
||||
|
||||
inline bool SocketIOChannel::isStream()
|
||||
{
|
||||
return Poco::IOChannelConfig::CONNECTION_CHANNEL == config().getType();
|
||||
}
|
||||
|
||||
|
||||
inline bool SocketIOChannel::isDatagram()
|
||||
{
|
||||
return Poco::IOChannelConfig::CONNECTIONLESS_CHANNEL == config().getType();
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
|
||||
|
||||
#endif // Net_SocketIOChannel_INCLUDED
|
180
Net/src/SocketIOChannel.cpp
Normal file
180
Net/src/SocketIOChannel.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
//
|
||||
// SocketIOChannel.cpp
|
||||
//
|
||||
// $Id: //poco/Main/IO/src/SocketIOChannel.cpp#1 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: IO
|
||||
// Module: SocketIOChannel
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Net/SocketIOChannel.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Net/SocketImpl.h"
|
||||
|
||||
|
||||
using Poco::InvalidArgumentException;
|
||||
using Poco::Net::SocketImpl;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
SocketIOChannel::SocketIOChannel(IOChannelConfig* pConfig):
|
||||
IOChannel(pConfig),
|
||||
_pSocket(0)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
SocketIOChannel::~SocketIOChannel()
|
||||
{
|
||||
delete _pSocket;
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannel::init()
|
||||
{
|
||||
delete _pSocket;
|
||||
|
||||
if (isStream())
|
||||
_pSocket = new Poco::Net::StreamSocket();
|
||||
else if (isDatagram())
|
||||
_pSocket = new Poco::Net::DatagramSocket();
|
||||
else
|
||||
throw IllegalStateException();
|
||||
|
||||
poco_check_ptr (_pSocket);
|
||||
|
||||
open();
|
||||
if (!_pSocket) throw NullPointerException();
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannel::open()
|
||||
{
|
||||
Poco::Timespan timeout(config().getTimeout() * Timespan::MILLISECONDS);
|
||||
SocketAddress address(config().getName());
|
||||
try
|
||||
{
|
||||
if (isStream() && timeout != IOChannelConfig::INFINITE_TIMEOUT)
|
||||
socketImpl()->connect(address, timeout);
|
||||
else
|
||||
socketImpl()->connect(address);
|
||||
|
||||
socketImpl()->setBlocking(false);
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int SocketIOChannel::readData(char*& pReadBuf)
|
||||
{
|
||||
int bufSize = socketImpl()->getReceiveBufferSize();
|
||||
char* pBuffer = new char[bufSize];
|
||||
int size = 0;
|
||||
|
||||
try
|
||||
{
|
||||
int read = 0;
|
||||
do
|
||||
{
|
||||
read = readData(pBuffer, bufSize);
|
||||
|
||||
if (read > 0)
|
||||
{
|
||||
pReadBuf = static_cast<char*>(std::realloc(pReadBuf, size + read));
|
||||
std::memcpy(pReadBuf + size, pBuffer, read);
|
||||
size += read;
|
||||
}
|
||||
else break;
|
||||
} while (true);
|
||||
}
|
||||
catch (TimeoutException&)
|
||||
{
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
delete [] pBuffer;
|
||||
throw;
|
||||
}
|
||||
|
||||
delete [] pBuffer;
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
int SocketIOChannel::readData(char* pBuffer, int length)
|
||||
{
|
||||
int received = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (!socketImpl()->poll(config().getTimeout(), SocketImpl::SELECT_READ))
|
||||
throw TimeoutException("read timed out", socketImpl()->address().toString());
|
||||
received = socketImpl()->receiveBytes(pBuffer, (int) length);
|
||||
}
|
||||
catch (Poco::Exception&)
|
||||
{
|
||||
socketImpl()->setBlocking(true);
|
||||
throw;
|
||||
}
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
|
||||
int SocketIOChannel::writeData(const char* pBuffer, int length)
|
||||
{
|
||||
int sent = 0;
|
||||
socketImpl()->setBlocking(false);
|
||||
|
||||
try
|
||||
{
|
||||
if (!socketImpl()->poll(config().getTimeout(), SocketImpl::SELECT_WRITE))
|
||||
throw Poco::TimeoutException("write timed out", socketImpl()->address().toString());
|
||||
sent = socketImpl()->sendBytes(pBuffer, (int) length);
|
||||
socketImpl()->setBlocking(true);
|
||||
}
|
||||
catch (Poco::Exception&)
|
||||
{
|
||||
socketImpl()->setBlocking(true);
|
||||
throw;
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
@ -25,7 +25,7 @@ objects = \
|
||||
MailTestSuite MailMessageTest MailStreamTest \
|
||||
SMTPClientSessionTest POP3ClientSessionTest \
|
||||
RawSocketTest ICMPClientTest ICMPSocketTest ICMPClientTestSuite \
|
||||
SyslogTest
|
||||
SyslogTest SocketIOChannelTestSuite SocketIOChannelTest
|
||||
|
||||
target = testrunner
|
||||
target_version = 1
|
||||
|
@ -976,6 +976,34 @@
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="IO"
|
||||
>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTest.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTestSuite.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTest.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTestSuite.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
@ -965,6 +965,34 @@
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="IO"
|
||||
>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTest.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTestSuite.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTest.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SocketIOChannelTestSuite.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
@ -101,7 +101,7 @@ void DatagramSocketTest::testBroadcast()
|
||||
SocketAddress sa("255.255.255.255", echoServer.port());
|
||||
try
|
||||
{
|
||||
int n = ss.sendTo("hello", 5, sa);
|
||||
ss.sendTo("hello", 5, sa);
|
||||
// not all socket implementations fail if broadcast option is not set
|
||||
// fail ("broadcast option not set - must throw");
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ void HTTPClientSessionTest::testKeepAlive()
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr3;
|
||||
std::streamsize n = StreamCopier::copyStream(rs3, ostr3);
|
||||
StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPTestServer::LARGE_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_HEAD);
|
||||
|
@ -78,7 +78,7 @@ namespace
|
||||
|
||||
std::istream& istr = request.stream();
|
||||
std::ostream& ostr = response.send();
|
||||
std::streamsize n = StreamCopier::copyStream(istr, ostr);
|
||||
StreamCopier::copyStream(istr, ostr);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include "MailTestSuite.h"
|
||||
#include "ICMPClientTestSuite.h"
|
||||
#include "SyslogTest.h"
|
||||
#include "SocketIOChannelTest.h"
|
||||
|
||||
|
||||
CppUnit::Test* NetTestSuite::suite()
|
||||
@ -63,6 +64,7 @@ CppUnit::Test* NetTestSuite::suite()
|
||||
pSuite->addTest(MailTestSuite::suite());
|
||||
pSuite->addTest(ICMPClientTestSuite::suite());
|
||||
pSuite->addTest(SyslogTest::suite());
|
||||
pSuite->addTest(SocketIOChannelTest::suite());
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
465
Net/testsuite/src/SocketIOChannelTest.cpp
Normal file
465
Net/testsuite/src/SocketIOChannelTest.cpp
Normal file
@ -0,0 +1,465 @@
|
||||
//
|
||||
// SocketIOChannelTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/template/test.cpp#6 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "SocketIOChannelTest.h"
|
||||
#include "UDPEchoServer.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Types.h"
|
||||
#include "Poco/Format.h"
|
||||
#include "Poco/ActiveResult.h"
|
||||
#include "Poco/IOChannel.h"
|
||||
#include "Poco/IOChannelConfig.h"
|
||||
#include "Poco/Net/SocketIOChannel.h"
|
||||
#include "Poco/ActiveIOChannel.h"
|
||||
#include "Poco/Net/TCPServer.h"
|
||||
#include "Poco/Net/TCPServerConnection.h"
|
||||
#include "Poco/Net/TCPServerConnectionFactory.h"
|
||||
#include "Poco/Net/TCPServerParams.h"
|
||||
#include "Poco/Net/ServerSocket.h"
|
||||
#include "Poco/Net/StreamSocket.h"
|
||||
#include "Poco/Void.h"
|
||||
#include "Poco/Types.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/BinaryReader.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
using Poco::UInt32;
|
||||
using Poco::Int64;
|
||||
using Poco::UInt64;
|
||||
using Poco::ActiveResult;
|
||||
using Poco::Void;
|
||||
using Poco::Thread;
|
||||
using Poco::format;
|
||||
using Poco::BinaryReader;
|
||||
using Poco::BinaryWriter;
|
||||
using Poco::IOChannel;
|
||||
using Poco::ActiveIOChannel;
|
||||
using Poco::IOChannelInputStream;
|
||||
using Poco::IOChannelOutputStream;
|
||||
using Poco::IOChannelConfig;
|
||||
using Poco::Net::SocketIOChannel;
|
||||
using Poco::Net::TCPServer;
|
||||
using Poco::Net::TCPServerConnection;
|
||||
using Poco::Net::TCPServerConnectionFactory;
|
||||
using Poco::Net::TCPServerConnectionFactoryImpl;
|
||||
using Poco::Net::TCPServerParams;
|
||||
using Poco::Net::ServerSocket;
|
||||
using Poco::Net::StreamSocket;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
class EchoConnection: public TCPServerConnection
|
||||
{
|
||||
public:
|
||||
EchoConnection(const StreamSocket& s): TCPServerConnection(s)
|
||||
{
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
StreamSocket& ss = socket();
|
||||
try
|
||||
{
|
||||
char buffer[256];
|
||||
int n = ss.receiveBytes(buffer, sizeof(buffer));
|
||||
while (n > 0)
|
||||
{
|
||||
ss.sendBytes(buffer, n);
|
||||
n = ss.receiveBytes(buffer, sizeof(buffer));
|
||||
}
|
||||
}
|
||||
catch (Poco::Exception& exc)
|
||||
{
|
||||
std::cerr << "EchoConnection: " << exc.displayText() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
SocketIOChannelTest::SocketIOChannelTest(const std::string& name):
|
||||
CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SocketIOChannelTest::~SocketIOChannelTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testChannelStream()
|
||||
{
|
||||
ServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketIOChannel network(new IOChannelConfig(format("localhost:%hu", svs.address().port()),
|
||||
IOChannelConfig::CONNECTION_CHANNEL,
|
||||
1000));
|
||||
|
||||
std::string data("hello, world");
|
||||
network.write(data.data(), (int) data.size());
|
||||
char buffer[256];
|
||||
int n = network.read(buffer, sizeof(buffer));
|
||||
assert ((int) data.size() == n);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 1);
|
||||
network.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testActiveChannelStream()
|
||||
{
|
||||
ServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
|
||||
SocketIOChannel network(new IOChannelConfig(format("localhost:%hu", svs.address().port()),
|
||||
IOChannelConfig::CONNECTION_CHANNEL,
|
||||
1000));
|
||||
|
||||
std::string str1 = "1234567890";
|
||||
std::string str2 = "";
|
||||
|
||||
ActiveIOChannel<SocketIOChannel> activeChannel(network);
|
||||
ActiveResult<int> result1 = activeChannel.write(str1);
|
||||
result1.wait();
|
||||
ActiveResult<std::string> result2 = activeChannel.read(0);
|
||||
result2.wait();
|
||||
assert("1234567890" == result2.data());
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testStreamsStream()
|
||||
{
|
||||
ServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
|
||||
SocketIOChannel net(new IOChannelConfig(format("localhost:%hu", svs.address().port()),
|
||||
IOChannelConfig::CONNECTION_CHANNEL,
|
||||
1000));
|
||||
|
||||
IOChannelOutputStream sos(net);
|
||||
IOChannelInputStream sis(net);
|
||||
|
||||
sos << "1234567890\n";
|
||||
std::string str;
|
||||
sis >> str;
|
||||
assert("1234567890" == str);
|
||||
|
||||
sos << 1.5;
|
||||
sos << "\n";
|
||||
sis >> str;
|
||||
assert("1.5" == str);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testBinaryStream()
|
||||
{
|
||||
ServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
|
||||
SocketIOChannel net(new IOChannelConfig(format("localhost:%hu", svs.address().port()),
|
||||
IOChannelConfig::CONNECTION_CHANNEL,
|
||||
1000));
|
||||
|
||||
IOChannelOutputStream sos(net);
|
||||
IOChannelInputStream sis(net);
|
||||
|
||||
BinaryWriter bw(sos);
|
||||
BinaryReader br(sis);
|
||||
|
||||
writeBinary(bw);
|
||||
readBinary(br);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testChannelDatagram()
|
||||
{
|
||||
UDPEchoServer echoServer;
|
||||
|
||||
SocketIOChannel network(new IOChannelConfig(format("localhost:%hu", echoServer.address().port()),
|
||||
IOChannelConfig::CONNECTIONLESS_CHANNEL,
|
||||
1000));
|
||||
|
||||
std::string data("hello, world");
|
||||
network.write(data.data(), (int) data.size());
|
||||
char buffer[256];
|
||||
int n = network.read(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testActiveChannelDatagram()
|
||||
{
|
||||
UDPEchoServer echoServer;
|
||||
|
||||
SocketIOChannel network(new IOChannelConfig(format("localhost:%hu", echoServer.address().port()),
|
||||
IOChannelConfig::CONNECTIONLESS_CHANNEL,
|
||||
1000));
|
||||
|
||||
std::string str1 = "1234567890";
|
||||
std::string str2 = "";
|
||||
|
||||
ActiveIOChannel<SocketIOChannel> activeChannel(network);
|
||||
ActiveResult<int> result1 = activeChannel.write(str1);
|
||||
result1.wait();
|
||||
ActiveResult<std::string> result2 = activeChannel.read(0);
|
||||
result2.wait();
|
||||
assert("1234567890" == result2.data());
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testStreamsDatagram()
|
||||
{
|
||||
UDPEchoServer echoServer;
|
||||
|
||||
SocketIOChannel net(new IOChannelConfig(format("localhost:%hu", echoServer.address().port()),
|
||||
IOChannelConfig::CONNECTIONLESS_CHANNEL,
|
||||
1000));
|
||||
|
||||
IOChannelOutputStream sos(net);
|
||||
IOChannelInputStream sis(net);
|
||||
|
||||
sos << "1234567890\n";
|
||||
std::string str;
|
||||
sis >> str;
|
||||
assert("1234567890" == str);
|
||||
|
||||
sos << 1.5;
|
||||
sos << "\n";
|
||||
sis >> str;
|
||||
assert("1.5" == str);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::testBinaryDatagram()
|
||||
{
|
||||
UDPEchoServer echoServer;
|
||||
|
||||
SocketIOChannel net(new IOChannelConfig(format("localhost:%hu", echoServer.address().port()),
|
||||
IOChannelConfig::CONNECTIONLESS_CHANNEL,
|
||||
1000));
|
||||
|
||||
IOChannelOutputStream sos(net);
|
||||
IOChannelInputStream sis(net);
|
||||
|
||||
BinaryWriter bw(sos);
|
||||
BinaryReader br(sis);
|
||||
|
||||
writeBinary(bw);
|
||||
readBinary(br);
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::writeBinary(BinaryWriter& writer)
|
||||
{
|
||||
writer << true;
|
||||
writer << false;
|
||||
writer << 'a';
|
||||
|
||||
writer << (short) -100;
|
||||
writer << (unsigned short) 50000;
|
||||
writer << -123456;
|
||||
writer << (unsigned) 123456;
|
||||
writer << (long) -1234567890;
|
||||
writer << (unsigned long) 1234567890;
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
writer << (Int64) -1234567890;
|
||||
writer << (UInt64) 1234567890;
|
||||
#endif
|
||||
|
||||
writer << (float) 1.5;
|
||||
writer << (double) -1.5;
|
||||
|
||||
writer << "foo";
|
||||
writer << "";
|
||||
|
||||
writer << std::string("bar");
|
||||
writer << std::string();
|
||||
|
||||
writer.write7BitEncoded((UInt32) 100);
|
||||
writer.write7BitEncoded((UInt32) 1000);
|
||||
writer.write7BitEncoded((UInt32) 10000);
|
||||
writer.write7BitEncoded((UInt32) 100000);
|
||||
writer.write7BitEncoded((UInt32) 1000000);
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
writer.write7BitEncoded((UInt64) 100);
|
||||
writer.write7BitEncoded((UInt64) 1000);
|
||||
writer.write7BitEncoded((UInt64) 10000);
|
||||
writer.write7BitEncoded((UInt64) 100000);
|
||||
writer.write7BitEncoded((UInt64) 1000000);
|
||||
#endif
|
||||
|
||||
writer.writeRaw("RAW");
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::readBinary(BinaryReader& reader)
|
||||
{
|
||||
bool b = false;
|
||||
reader >> b;
|
||||
assert (b);
|
||||
reader >> b;
|
||||
assert (!b);
|
||||
|
||||
char c = ' ';
|
||||
reader >> c;
|
||||
assert (c == 'a');
|
||||
|
||||
short shortv = 0;
|
||||
reader >> shortv;
|
||||
assert (shortv == -100);
|
||||
|
||||
unsigned short ushortv = 0;
|
||||
reader >> ushortv;
|
||||
assert (ushortv == 50000);
|
||||
|
||||
int intv = 0;
|
||||
reader >> intv;
|
||||
assert (intv == -123456);
|
||||
|
||||
unsigned uintv = 0;
|
||||
reader >> uintv;
|
||||
assert (uintv == 123456);
|
||||
|
||||
long longv = 0;
|
||||
reader >> longv;
|
||||
assert (longv == -1234567890);
|
||||
|
||||
unsigned long ulongv = 0;
|
||||
reader >> ulongv;
|
||||
assert (ulongv == 1234567890);
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
Int64 int64v = 0;
|
||||
reader >> int64v;
|
||||
assert (int64v == -1234567890);
|
||||
|
||||
UInt64 uint64v = 0;
|
||||
reader >> uint64v;
|
||||
assert (uint64v == 1234567890);
|
||||
#endif
|
||||
|
||||
float floatv = 0.0;
|
||||
reader >> floatv;
|
||||
assert (floatv == 1.5);
|
||||
|
||||
double doublev = 0.0;
|
||||
reader >> doublev;
|
||||
assert (doublev == -1.5);
|
||||
|
||||
std::string str;
|
||||
reader >> str;
|
||||
assert (str == "foo");
|
||||
reader >> str;
|
||||
assert (str == "");
|
||||
|
||||
reader >> str;
|
||||
assert (str == "bar");
|
||||
reader >> str;
|
||||
assert (str == "");
|
||||
|
||||
UInt32 uint32v;
|
||||
reader.read7BitEncoded(uint32v);
|
||||
assert (uint32v == 100);
|
||||
reader.read7BitEncoded(uint32v);
|
||||
assert (uint32v == 1000);
|
||||
reader.read7BitEncoded(uint32v);
|
||||
assert (uint32v == 10000);
|
||||
reader.read7BitEncoded(uint32v);
|
||||
assert (uint32v == 100000);
|
||||
reader.read7BitEncoded(uint32v);
|
||||
assert (uint32v == 1000000);
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
reader.read7BitEncoded(uint64v);
|
||||
assert (uint64v == 100);
|
||||
reader.read7BitEncoded(uint64v);
|
||||
assert (uint64v == 1000);
|
||||
reader.read7BitEncoded(uint64v);
|
||||
assert (uint64v == 10000);
|
||||
reader.read7BitEncoded(uint64v);
|
||||
assert (uint64v == 100000);
|
||||
reader.read7BitEncoded(uint64v);
|
||||
assert (uint64v == 1000000);
|
||||
#endif
|
||||
|
||||
reader.readRaw(3, str);
|
||||
assert (str == "RAW");
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void SocketIOChannelTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* SocketIOChannelTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketIOChannelTest");
|
||||
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testChannelStream);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testActiveChannelStream);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testStreamsStream);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testBinaryStream);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testChannelDatagram);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testActiveChannelDatagram);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testStreamsDatagram);
|
||||
CppUnit_addTest(pSuite, SocketIOChannelTest, testBinaryDatagram);
|
||||
|
||||
return pSuite;
|
||||
}
|
75
Net/testsuite/src/SocketIOChannelTest.h
Normal file
75
Net/testsuite/src/SocketIOChannelTest.h
Normal file
@ -0,0 +1,75 @@
|
||||
//
|
||||
// SocketIOChannelTest.h
|
||||
//
|
||||
// $Id: //poco/Main/template/test.h#7 $
|
||||
//
|
||||
// Definition of the SocketIOChannelTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef SocketIOChannelTest_INCLUDED
|
||||
#define SocketIOChannelTest_INCLUDED
|
||||
|
||||
|
||||
#include "CppUnit/TestCase.h"
|
||||
#include "Poco/BinaryReader.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
#include "Poco/IOChannel.h"
|
||||
#include "Poco/IOChannelStream.h"
|
||||
#include "Poco/IOChannelConfig.h"
|
||||
#include "Poco/Net/SocketIOChannel.h"
|
||||
|
||||
|
||||
class SocketIOChannelTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
SocketIOChannelTest(const std::string& name);
|
||||
~SocketIOChannelTest();
|
||||
|
||||
void testChannelStream();
|
||||
void testActiveChannelStream();
|
||||
void testStreamsStream();
|
||||
void testBinaryStream();
|
||||
|
||||
void testChannelDatagram();
|
||||
void testActiveChannelDatagram();
|
||||
void testStreamsDatagram();
|
||||
void testBinaryDatagram();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
void writeBinary(Poco::BinaryWriter& writer);
|
||||
void readBinary(Poco::BinaryReader& reader);
|
||||
};
|
||||
|
||||
|
||||
#endif // SocketIOChannelTest_INCLUDED
|
44
Net/testsuite/src/SocketIOChannelTestSuite.cpp
Normal file
44
Net/testsuite/src/SocketIOChannelTestSuite.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// SocketIOChannelTestSuite.cpp
|
||||
//
|
||||
// $Id: //poco/Main/template/suite.cpp#6 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "SocketIOChannelTestSuite.h"
|
||||
#include "SocketIOChannelTest.h"
|
||||
|
||||
|
||||
CppUnit::Test* SocketIOChannelTestSuite::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketIOChannelTestSuite");
|
||||
|
||||
pSuite->addTest(SocketIOChannelTest::suite());
|
||||
|
||||
return pSuite;
|
||||
}
|
49
Net/testsuite/src/SocketIOChannelTestSuite.h
Normal file
49
Net/testsuite/src/SocketIOChannelTestSuite.h
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// NetTestSuite.h
|
||||
//
|
||||
// $Id: //poco/Main/template/suite.h#6 $
|
||||
//
|
||||
// Definition of the NetTestSuite class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef SocketIOChannelTestSuite_INCLUDED
|
||||
#define SocketIOChannelTestSuite_INCLUDED
|
||||
|
||||
|
||||
#include "CppUnit/TestSuite.h"
|
||||
|
||||
|
||||
class SocketIOChannelTestSuite
|
||||
{
|
||||
public:
|
||||
static CppUnit::Test* suite();
|
||||
};
|
||||
|
||||
|
||||
#endif // SocketIOChannelTestSuite_INCLUDED
|
Loading…
Reference in New Issue
Block a user