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

View File

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

View File

@ -1,7 +1,7 @@
//
// SocketAddress.h
//
// $Id: //poco/svn/Net/include/Poco/Net/SocketAddress.h#2 $
// $Id: //poco/Main/Net/include/Poco/Net/SocketAddress.h#3 $
//
// Library: Net
// Package: NetCore
@ -126,6 +126,9 @@ public:
std::string toString() const;
/// Returns a string representation of the address.
IPAddress::Family family() const;
/// Returns the address family of the host's address.
enum
{
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

View File

@ -1,7 +1,7 @@
//
// StreamSocket.h
//
// $Id: //poco/Main/Net/include/Poco/Net/StreamSocket.h#3 $
// $Id: //poco/Main/Net/include/Poco/Net/StreamSocket.h#4 $
//
// Library: Net
// Package: Sockets
@ -66,6 +66,15 @@ public:
/// Creates a stream socket and connects it to
/// 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);
/// Creates the StreamSocket with the SocketImpl
/// from another socket. The SocketImpl must be

View File

@ -1,7 +1,7 @@
//
// StreamSocketImpl.h
//
// $Id: //poco/svn/Net/include/Poco/Net/StreamSocketImpl.h#2 $
// $Id: //poco/Main/Net/include/Poco/Net/StreamSocketImpl.h#3 $
//
// Library: Net
// Package: Sockets
@ -54,6 +54,10 @@ class Net_API StreamSocketImpl: public SocketImpl
public:
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);
/// Creates a StreamSocketImpl using the given native socket.

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
//
// HostEntry.cpp
//
// $Id: //poco/Main/Net/src/HostEntry.cpp#8 $
// $Id: //poco/Main/Net/src/HostEntry.cpp#9 $
//
// Library: Net
// Package: NetCore
@ -89,9 +89,21 @@ HostEntryImpl::HostEntryImpl(struct addrinfo* ainfo)
for (struct addrinfo* ai = ainfo; ai; ai = ai->ai_next)
{
if (ai->ai_canonname)
{
_name.assign(ai->ai_canonname);
}
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
//
// $Id: //poco/svn/Net/src/ICMPPacket.cpp#2 $
// $Id: //poco/Main/Net/src/ICMPPacket.cpp#5 $
//
// Library: Net
// Package: ICMP

View File

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

View File

@ -1,7 +1,7 @@
//
// QuotedPrintableEncoder.cpp
//
// $Id: //poco/svn/Net/src/QuotedPrintableEncoder.cpp#2 $
// $Id: //poco/Main/Net/src/QuotedPrintableEncoder.cpp#7 $
//
// Library: Net
// Package: Messages
@ -83,7 +83,7 @@ int QuotedPrintableEncoderBuf::writeToDevice(char c)
_pending = charToInt(c);
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);
}

View File

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

View File

@ -1,7 +1,7 @@
//
// SocketImpl.cpp
//
// $Id: //poco/Main/Net/src/SocketImpl.cpp#26 $
// $Id: //poco/Main/Net/src/SocketImpl.cpp#28 $
//
// Library: Net
// Package: Sockets
@ -41,12 +41,12 @@
#include "Poco/Timestamp.h"
#include <string.h> // FD_SET needs memset on some platforms, so we can't use <cstring>
#if defined(POCO_HAVE_FD_POLL)
#include <poll.h>
#include <poll.h>
#endif
#if defined(sun) || defined(__sun) || defined(__sun__)
#include <unistd.h>
#include <stropts.h>
#endif
#endif
using Poco::IOException;
using Poco::TimeoutException;
@ -120,9 +120,10 @@ void SocketImpl::connect(const SocketAddress& address)
void SocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
{
poco_assert (_sockfd == POCO_INVALID_SOCKET);
init(address.af());
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
setBlocking(false);
try
{
@ -330,7 +331,7 @@ int SocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, in
void SocketImpl::sendUrgent(unsigned char data)
{
poco_assert (_sockfd != POCO_INVALID_SOCKET);
int rc = ::send(_sockfd, reinterpret_cast<const char*>(&data), sizeof(data), MSG_OOB);
if (rc < 0) error();
}
@ -346,24 +347,21 @@ int SocketImpl::available()
bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
{
poco_assert (_sockfd != POCO_INVALID_SOCKET);
#if defined(POCO_HAVE_FD_POLL)
pollfd pollBuf;
memset(&pollBuf, 0, sizeof(pollfd));
pollBuf.fd = _sockfd;
if (mode & SELECT_READ) pollBuf.events |= POLLIN;
if (mode & SELECT_WRITE) pollBuf.events |= POLLOUT;
Poco::Timespan remainingTime(timeout);
int rc;
do
{
Poco::Timestamp start;
rc = ::poll(&pollBuf, 1, remainingTime.totalMilliseconds());
if (rc < 0 && lastError() == POCO_EINTR)
{
Poco::Timestamp end;
@ -373,10 +371,9 @@ bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
else
remainingTime = 0;
}
} while (rc < 0 && lastError() == POCO_EINTR);
}
while (rc < 0 && lastError() == POCO_EINTR);
#else
fd_set fdRead;
fd_set fdWrite;
fd_set fdExcept;
@ -415,9 +412,7 @@ bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
}
}
while (rc < 0 && lastError() == POCO_EINTR);
#endif
#endif // POCO_HAVE_FD_POLL
if (rc < 0) error();
return rc > 0;
}

View File

@ -1,7 +1,7 @@
//
// StreamSocket.cpp
//
// $Id: //poco/Main/Net/src/StreamSocket.cpp#9 $
// $Id: //poco/Main/Net/src/StreamSocket.cpp#10 $
//
// Library: Net
// 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)
{
if (!dynamic_cast<StreamSocketImpl*>(impl()))

View File

@ -1,7 +1,7 @@
//
// StreamSocketImpl.cpp
//
// $Id: //poco/Main/Net/src/StreamSocketImpl.cpp#8 $
// $Id: //poco/Main/Net/src/StreamSocketImpl.cpp#9 $
//
// Library: Net
// 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)
{
}

View File

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