socket initialization and other fixes

This commit is contained in:
Guenter Obiltschnig
2009-04-14 11:02:21 +00:00
parent 457e24748d
commit 9cb9229a4e
17 changed files with 92 additions and 49 deletions

View File

@@ -1,7 +1,7 @@
// //
// DatagramSocketImpl.h // DatagramSocketImpl.h
// //
// $Id: //poco/svn/Net/include/Poco/Net/DatagramSocketImpl.h#2 $ // $Id: //poco/Main/Net/include/Poco/Net/DatagramSocketImpl.h#3 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -59,7 +59,7 @@ public:
/// be an IPv6 socket. Otherwise, it will be /// be an IPv6 socket. Otherwise, it will be
/// an IPv4 socket. /// an IPv4 socket.
DatagramSocketImpl(IPAddress::Family family); explicit DatagramSocketImpl(IPAddress::Family family);
/// Creates an unconnected datagram socket. /// Creates an unconnected datagram socket.
/// ///
/// The socket will be created for the /// The socket will be created for the

View File

@@ -1,7 +1,7 @@
// //
// HTTPServerSession.h // HTTPServerSession.h
// //
// $Id: //poco/svn/Net/include/Poco/Net/HTTPServerSession.h#2 $ // $Id: //poco/Main/Net/include/Poco/Net/HTTPServerSession.h#5 $
// //
// Library: Net // Library: Net
// Package: HTTPServer // Package: HTTPServer
@@ -43,6 +43,8 @@
#include "Poco/Net/Net.h" #include "Poco/Net/Net.h"
#include "Poco/Net/HTTPSession.h" #include "Poco/Net/HTTPSession.h"
#include "Poco/Net/SocketAddress.h" #include "Poco/Net/SocketAddress.h"
#include "Poco/Net/HTTPServerSession.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Timespan.h" #include "Poco/Timespan.h"
@@ -50,16 +52,13 @@ namespace Poco {
namespace Net { namespace Net {
class HTTPServerParams;
class Net_API HTTPServerSession: public HTTPSession class Net_API HTTPServerSession: public HTTPSession
/// This class handles the server side of a /// This class handles the server side of a
/// HTTP session. It is used internally by /// HTTP session. It is used internally by
/// HTTPServer. /// HTTPServer.
{ {
public: public:
HTTPServerSession(const StreamSocket& socket, HTTPServerParams* pParams); HTTPServerSession(const StreamSocket& socket, HTTPServerParams::Ptr pParams);
/// Creates the HTTPServerSession. /// Creates the HTTPServerSession.
virtual ~HTTPServerSession(); virtual ~HTTPServerSession();

View File

@@ -1,7 +1,7 @@
// //
// SocketAddress.h // SocketAddress.h
// //
// $Id: //poco/svn/Net/include/Poco/Net/SocketAddress.h#2 $ // $Id: //poco/Main/Net/include/Poco/Net/SocketAddress.h#3 $
// //
// Library: Net // Library: Net
// Package: NetCore // Package: NetCore
@@ -126,6 +126,9 @@ public:
std::string toString() const; std::string toString() const;
/// Returns a string representation of the address. /// Returns a string representation of the address.
IPAddress::Family family() const;
/// Returns the address family of the host's address.
enum enum
{ {
MAX_ADDRESS_LENGTH = MAX_ADDRESS_LENGTH =
@@ -156,6 +159,12 @@ inline void swap(SocketAddress& a1, SocketAddress& a2)
} }
inline IPAddress::Family SocketAddress::family() const
{
return host().family();
}
} } // namespace Poco::Net } } // namespace Poco::Net

View File

@@ -1,7 +1,7 @@
// //
// StreamSocket.h // StreamSocket.h
// //
// $Id: //poco/Main/Net/include/Poco/Net/StreamSocket.h#3 $ // $Id: //poco/Main/Net/include/Poco/Net/StreamSocket.h#4 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -66,6 +66,15 @@ public:
/// Creates a stream socket and connects it to /// Creates a stream socket and connects it to
/// the socket specified by address. /// the socket specified by address.
explicit StreamSocket(IPAddress::Family family);
/// Creates an unconnected stream socket
/// for the given address family.
///
/// This is useful if certain socket options
/// (like send and receive buffer) sizes, that must
/// be set before connecting the socket, will be
/// set later on.
StreamSocket(const Socket& socket); StreamSocket(const Socket& socket);
/// Creates the StreamSocket with the SocketImpl /// Creates the StreamSocket with the SocketImpl
/// from another socket. The SocketImpl must be /// from another socket. The SocketImpl must be

View File

@@ -1,7 +1,7 @@
// //
// StreamSocketImpl.h // StreamSocketImpl.h
// //
// $Id: //poco/svn/Net/include/Poco/Net/StreamSocketImpl.h#2 $ // $Id: //poco/Main/Net/include/Poco/Net/StreamSocketImpl.h#3 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -55,6 +55,10 @@ public:
StreamSocketImpl(); StreamSocketImpl();
/// Creates a StreamSocketImpl. /// Creates a StreamSocketImpl.
explicit StreamSocketImpl(IPAddress::Family addressFamily);
/// Creates a SocketImpl, with the underlying
/// socket initialized for the given address family.
StreamSocketImpl(poco_socket_t sockfd); StreamSocketImpl(poco_socket_t sockfd);
/// Creates a StreamSocketImpl using the given native socket. /// Creates a StreamSocketImpl using the given native socket.

View File

@@ -1,7 +1,7 @@
// //
// DatagramSocketImpl.cpp // DatagramSocketImpl.cpp
// //
// $Id: //poco/svn/Net/src/DatagramSocketImpl.cpp#2 $ // $Id: //poco/Main/Net/src/DatagramSocketImpl.cpp#7 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -60,7 +60,6 @@ DatagramSocketImpl::DatagramSocketImpl(IPAddress::Family family)
init(AF_INET6); init(AF_INET6);
#endif #endif
else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl"); else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl");
} }

View File

@@ -1,7 +1,7 @@
// //
// HTTPClientSession.cpp // HTTPClientSession.cpp
// //
// $Id: //poco/Main/Net/src/HTTPClientSession.cpp#21 $ // $Id: //poco/Main/Net/src/HTTPClientSession.cpp#22 $
// //
// Library: Net // Library: Net
// Package: HTTPClient // Package: HTTPClient
@@ -175,7 +175,7 @@ std::ostream& HTTPClientSession::sendRequest(HTTPRequest& request)
_pResponseStream = 0; _pResponseStream = 0;
bool keepAlive = getKeepAlive(); bool keepAlive = getKeepAlive();
if (connected() && !keepAlive || mustReconnect()) if ((connected() && !keepAlive) || mustReconnect())
{ {
close(); close();
_mustReconnect = false; _mustReconnect = false;

View File

@@ -1,7 +1,7 @@
// //
// HTTPServerSession.cpp // HTTPServerSession.cpp
// //
// $Id: //poco/Main/Net/src/HTTPServerSession.cpp#9 $ // $Id: //poco/Main/Net/src/HTTPServerSession.cpp#11 $
// //
// Library: Net // Library: Net
// Package: HTTPServer // Package: HTTPServer
@@ -35,14 +35,13 @@
#include "Poco/Net/HTTPServerSession.h" #include "Poco/Net/HTTPServerSession.h"
#include "Poco/Net/HTTPServerParams.h"
namespace Poco { namespace Poco {
namespace Net { namespace Net {
HTTPServerSession::HTTPServerSession(const StreamSocket& socket, HTTPServerParams* pParams): HTTPServerSession::HTTPServerSession(const StreamSocket& socket, HTTPServerParams::Ptr pParams):
HTTPSession(socket, pParams->getKeepAlive()), HTTPSession(socket, pParams->getKeepAlive()),
_firstRequest(true), _firstRequest(true),
_keepAliveTimeout(pParams->getKeepAliveTimeout()), _keepAliveTimeout(pParams->getKeepAliveTimeout()),

View File

@@ -1,7 +1,7 @@
// //
// HostEntry.cpp // HostEntry.cpp
// //
// $Id: //poco/Main/Net/src/HostEntry.cpp#8 $ // $Id: //poco/Main/Net/src/HostEntry.cpp#9 $
// //
// Library: Net // Library: Net
// Package: NetCore // Package: NetCore
@@ -89,9 +89,21 @@ HostEntryImpl::HostEntryImpl(struct addrinfo* ainfo)
for (struct addrinfo* ai = ainfo; ai; ai = ai->ai_next) for (struct addrinfo* ai = ainfo; ai; ai = ai->ai_next)
{ {
if (ai->ai_canonname) if (ai->ai_canonname)
{
_name.assign(ai->ai_canonname); _name.assign(ai->ai_canonname);
}
else if (ai->ai_addrlen && ai->ai_addr) else if (ai->ai_addrlen && ai->ai_addr)
_addresses.push_back(IPAddress(ai->ai_addr, (poco_socklen_t) ai->ai_addrlen)); {
switch (ai->ai_addr->sa_family)
{
case AF_INET:
_addresses.push_back(IPAddress(&reinterpret_cast<struct sockaddr_in*>(&ai->ai_addr)->sin_addr, sizeof(in_addr)));
break;
case AF_INET6:
_addresses.push_back(IPAddress(&reinterpret_cast<struct sockaddr_in6*>(&ai->ai_addr)->sin6_addr, sizeof(in6_addr)));
break;
}
}
} }
} }

View File

@@ -1,7 +1,7 @@
// //
// ICMPPacket.cpp // ICMPPacket.cpp
// //
// $Id: //poco/svn/Net/src/ICMPPacket.cpp#2 $ // $Id: //poco/Main/Net/src/ICMPPacket.cpp#5 $
// //
// Library: Net // Library: Net
// Package: ICMP // Package: ICMP

View File

@@ -1,7 +1,7 @@
// //
// MultipartReader.cpp // MultipartReader.cpp
// //
// $Id: //poco/svn/Net/src/MultipartReader.cpp#2 $ // $Id: //poco/Main/Net/src/MultipartReader.cpp#13 $
// //
// Library: Net // Library: Net
// Package: Messages // Package: Messages
@@ -76,7 +76,7 @@ int MultipartStreamBuf::readFromDevice(char* buffer, std::streamsize length)
int ch = _istr.get(); int ch = _istr.get();
if (ch == eof) return -1; if (ch == eof) return -1;
*buffer++ = (char) ch; ++n; *buffer++ = (char) ch; ++n;
if (ch == '\n' || ch == '\r' && _istr.peek() == '\n') if (ch == '\n' || (ch == '\r' && _istr.peek() == '\n'))
{ {
if (ch == '\r') if (ch == '\r')
{ {
@@ -102,7 +102,7 @@ int MultipartStreamBuf::readFromDevice(char* buffer, std::streamsize length)
} }
if (it == end) if (it == end)
{ {
if (ch == '\n' || ch == '\r' && _istr.peek() == '\n') if (ch == '\n' || (ch == '\r' && _istr.peek() == '\n'))
{ {
if (ch == '\r') if (ch == '\r')
{ {

View File

@@ -1,7 +1,7 @@
// //
// QuotedPrintableEncoder.cpp // QuotedPrintableEncoder.cpp
// //
// $Id: //poco/svn/Net/src/QuotedPrintableEncoder.cpp#2 $ // $Id: //poco/Main/Net/src/QuotedPrintableEncoder.cpp#7 $
// //
// Library: Net // Library: Net
// Package: Messages // Package: Messages
@@ -83,7 +83,7 @@ int QuotedPrintableEncoderBuf::writeToDevice(char c)
_pending = charToInt(c); _pending = charToInt(c);
return _pending; return _pending;
} }
else if (c == '\r' || c == '\n' || c > 32 && c < 127 && c != '=') else if (c == '\r' || c == '\n' || (c > 32 && c < 127 && c != '='))
{ {
writeRaw(c); writeRaw(c);
} }

View File

@@ -1,7 +1,7 @@
// //
// RawSocketImpl.cpp // RawSocketImpl.cpp
// //
// $Id: //poco/svn/Net/src/RawSocketImpl.cpp#2 $ // $Id: //poco/Main/Net/src/RawSocketImpl.cpp#10 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets

View File

@@ -1,7 +1,7 @@
// //
// SocketImpl.cpp // SocketImpl.cpp
// //
// $Id: //poco/Main/Net/src/SocketImpl.cpp#26 $ // $Id: //poco/Main/Net/src/SocketImpl.cpp#28 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -120,9 +120,10 @@ void SocketImpl::connect(const SocketAddress& address)
void SocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout) void SocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
{ {
poco_assert (_sockfd == POCO_INVALID_SOCKET); if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af()); init(address.af());
}
setBlocking(false); setBlocking(false);
try try
{ {
@@ -346,10 +347,7 @@ int SocketImpl::available()
bool SocketImpl::poll(const Poco::Timespan& timeout, int mode) bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
{ {
poco_assert (_sockfd != POCO_INVALID_SOCKET);
#if defined(POCO_HAVE_FD_POLL) #if defined(POCO_HAVE_FD_POLL)
pollfd pollBuf; pollfd pollBuf;
memset(&pollBuf, 0, sizeof(pollfd)); memset(&pollBuf, 0, sizeof(pollfd));
@@ -373,10 +371,9 @@ bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
else else
remainingTime = 0; remainingTime = 0;
} }
} while (rc < 0 && lastError() == POCO_EINTR); }
while (rc < 0 && lastError() == POCO_EINTR);
#else #else
fd_set fdRead; fd_set fdRead;
fd_set fdWrite; fd_set fdWrite;
fd_set fdExcept; fd_set fdExcept;
@@ -415,9 +412,7 @@ bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
} }
} }
while (rc < 0 && lastError() == POCO_EINTR); while (rc < 0 && lastError() == POCO_EINTR);
#endif // POCO_HAVE_FD_POLL
#endif
if (rc < 0) error(); if (rc < 0) error();
return rc > 0; return rc > 0;
} }

View File

@@ -1,7 +1,7 @@
// //
// StreamSocket.cpp // StreamSocket.cpp
// //
// $Id: //poco/Main/Net/src/StreamSocket.cpp#9 $ // $Id: //poco/Main/Net/src/StreamSocket.cpp#10 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -57,6 +57,11 @@ StreamSocket::StreamSocket(const SocketAddress& address): Socket(new StreamSocke
} }
StreamSocket::StreamSocket(IPAddress::Family family): Socket(new StreamSocketImpl(family))
{
}
StreamSocket::StreamSocket(const Socket& socket): Socket(socket) StreamSocket::StreamSocket(const Socket& socket): Socket(socket)
{ {
if (!dynamic_cast<StreamSocketImpl*>(impl())) if (!dynamic_cast<StreamSocketImpl*>(impl()))

View File

@@ -1,7 +1,7 @@
// //
// StreamSocketImpl.cpp // StreamSocketImpl.cpp
// //
// $Id: //poco/Main/Net/src/StreamSocketImpl.cpp#8 $ // $Id: //poco/Main/Net/src/StreamSocketImpl.cpp#9 $
// //
// Library: Net // Library: Net
// Package: Sockets // Package: Sockets
@@ -46,6 +46,18 @@ StreamSocketImpl::StreamSocketImpl()
} }
StreamSocketImpl::StreamSocketImpl(IPAddress::Family family)
{
if (family == IPAddress::IPv4)
init(AF_INET);
#if defined(POCO_HAVE_IPv6)
else if (family == IPAddress::IPv6)
init(AF_INET6);
#endif
else throw InvalidArgumentException("Invalid or unsupported address family passed to StreamSocketImpl");
}
StreamSocketImpl::StreamSocketImpl(poco_socket_t sockfd): SocketImpl(sockfd) StreamSocketImpl::StreamSocketImpl(poco_socket_t sockfd): SocketImpl(sockfd)
{ {
} }

View File

@@ -1,7 +1,7 @@
// //
// TCPServerDispatcher.cpp // TCPServerDispatcher.cpp
// //
// $Id: //poco/Main/Net/src/TCPServerDispatcher.cpp#10 $ // $Id: //poco/Main/Net/src/TCPServerDispatcher.cpp#11 $
// //
// Library: Net // Library: Net
// Package: TCPServer // Package: TCPServer
@@ -139,7 +139,7 @@ void TCPServerDispatcher::run()
} }
FastMutex::ScopedLock lock(_mutex); FastMutex::ScopedLock lock(_mutex);
if (_stopped || _currentThreads > 1 && _queue.empty()) if (_stopped || (_currentThreads > 1 && _queue.empty()))
{ {
--_currentThreads; --_currentThreads;
break; break;