| 1 | // |
|---|---|
| 2 | // StreamSocketImpl.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: Sockets |
| 6 | // Module: StreamSocketImpl |
| 7 | // |
| 8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/StreamSocketImpl.h" |
| 16 | #include "Poco/Exception.h" |
| 17 | #include "Poco/Thread.h" |
| 18 | |
| 19 | |
| 20 | namespace Poco { |
| 21 | namespace Net { |
| 22 | |
| 23 | |
| 24 | StreamSocketImpl::StreamSocketImpl() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | |
| 29 | StreamSocketImpl::StreamSocketImpl(SocketAddress::Family family) |
| 30 | { |
| 31 | if (family == SocketAddress::IPv4) |
| 32 | init(AF_INET); |
| 33 | #if defined(POCO_HAVE_IPv6) |
| 34 | else if (family == SocketAddress::IPv6) |
| 35 | init(AF_INET6); |
| 36 | #endif |
| 37 | #if defined(POCO_OS_FAMILY_UNIX) |
| 38 | else if (family == SocketAddress::UNIX_LOCAL) |
| 39 | init(AF_UNIX); |
| 40 | #endif |
| 41 | else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to StreamSocketImpl"); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | StreamSocketImpl::StreamSocketImpl(poco_socket_t sockfd): SocketImpl(sockfd) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | |
| 50 | StreamSocketImpl::~StreamSocketImpl() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int StreamSocketImpl::sendBytes(const void* buffer, int length, int flags) |
| 56 | { |
| 57 | const char* p = reinterpret_cast<const char*>(buffer); |
| 58 | int remaining = length; |
| 59 | int sent = 0; |
| 60 | bool blocking = getBlocking(); |
| 61 | while (remaining > 0) |
| 62 | { |
| 63 | int n = SocketImpl::sendBytes(p, remaining, flags); |
| 64 | poco_assert_dbg (n >= 0); |
| 65 | p += n; |
| 66 | sent += n; |
| 67 | remaining -= n; |
| 68 | if (blocking && remaining > 0) |
| 69 | Poco::Thread::yield(); |
| 70 | else |
| 71 | break; |
| 72 | } |
| 73 | return sent; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | } } // namespace Poco::Net |
| 78 |