diff --git a/net/base/net_errors.h b/net/base/net_errors.h index ee679fb1b631..c9e7fdf24fdd 100644 --- a/net/base/net_errors.h +++ b/net/base/net_errors.h @@ -12,7 +12,11 @@ #include "base/logging.h" #include "net/base/net_export.h" #if defined(STARBOARD) +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" +#else +#include +#endif #include "starboard/system.h" #endif @@ -57,6 +61,8 @@ NET_EXPORT bool IsDnsError(int error); NET_EXPORT Error MapSystemError(logging::SystemErrorCode os_error); #if defined(STARBOARD) + +#if SB_API_VERSION < 16 // Map socket error code to Error. NET_EXPORT Error MapSocketError(SbSocketError error); @@ -64,6 +70,7 @@ NET_EXPORT Error MapSocketError(SbSocketError error); static SB_C_INLINE Error MapLastSocketError(SbSocket socket) { return MapSocketError(SbSocketGetLastError(socket)); } +#endif // SB_API_VERSION < 16 // Gets the last system error as a net error. static SB_C_INLINE Error MapLastSystemError() { diff --git a/net/base/net_errors_starboard.cc b/net/base/net_errors_starboard.cc index 3eabf3b39360..99bc994fb58e 100644 --- a/net/base/net_errors_starboard.cc +++ b/net/base/net_errors_starboard.cc @@ -36,6 +36,7 @@ Error MapSystemError(logging::SystemErrorCode error) { return ERR_FAILED; } +#if SB_API_VERSION < 16 Error MapSocketError(SbSocketError error) { if (error != kSbSocketOk) DVLOG(2) << "Error " << error; @@ -55,5 +56,6 @@ Error MapSocketError(SbSocketError error) { return ERR_FAILED; } } +#endif // SB_API_VERSION < 16 } // namespace net diff --git a/net/socket/tcp_socket_starboard.cc b/net/socket/tcp_socket_starboard.cc index 21a0091d02e5..70f29b8aad3f 100644 --- a/net/socket/tcp_socket_starboard.cc +++ b/net/socket/tcp_socket_starboard.cc @@ -16,6 +16,8 @@ #include #include +#include +#include #include "base/callback_helpers.h" #include "base/time/time.h" @@ -24,6 +26,8 @@ #include "net/socket/socket_net_log_params.h" #include "net/socket/socket_options.h" #include "starboard/configuration_constants.h" +#include "starboard/socket.h" + namespace net { @@ -50,7 +54,16 @@ TCPSocketStarboard::~TCPSocketStarboard() { int TCPSocketStarboard::Open(AddressFamily family) { DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK(!SbSocketIsValid(socket_)); +#if SB_API_VERSION < 16 socket_ = SbSocketCreate(ConvertAddressFamily(family), kSbSocketProtocolTcp); +#else + int socket_fd = socket(family, SOCK_STREAM, IPPROTO_TCP); + if (socket_fd < 0) { + socket_= kSbSocketInvalid; + } else { + socket_ = new SbSocketWrapper(ConvertAddressFamily(family), kSbSocketProtocolTcp, socket_fd); + } +#endif if (!SbSocketIsValid(socket_)) { return MapLastSystemError(); } @@ -98,7 +111,11 @@ int TCPSocketStarboard::Bind(const IPEndPoint& address) { SbSocketError error = SbSocketBind(socket_, &storage); if (error != kSbSocketOk) { DLOG(ERROR) << "SbSocketBind() returned an error"; +#if SB_API_VERSION < 16 return MapLastSocketError(socket_); +#else + return errno; +#endif } local_address_.reset(new IPEndPoint(address)); @@ -115,7 +132,11 @@ int TCPSocketStarboard::Listen(int backlog) { SbSocketError error = SbSocketListen(socket_); if (error != kSbSocketOk) { DLOG(ERROR) << "SbSocketListen() returned an error"; +#if SB_API_VERSION < 16 int rv = MapLastSocketError(socket_); +#else + int rv = errno; +#endif Close(); return rv; } @@ -142,7 +163,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr* socket, socket_, true, base::MessageLoopCurrentForIO::WATCH_READ, &socket_watcher_, this)) { DLOG(ERROR) << "WatchSocket failed on read"; +#if SB_API_VERSION < 16 return MapLastSocketError(socket_); +#else + return errno; +#endif } accept_socket_ = socket; @@ -156,7 +181,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr* socket, int TCPSocketStarboard::SetDefaultOptionsForServer() { DCHECK(SbSocketIsValid(socket_)); if (!SbSocketSetReuseAddress(socket_, true)) { +#if SB_API_VERSION < 16 return MapLastSocketError(socket_); +#else + return errno; +#endif } return OK; } @@ -177,9 +206,20 @@ void TCPSocketStarboard::SetDefaultOptionsForClient() { int TCPSocketStarboard::AcceptInternal( std::unique_ptr* socket, IPEndPoint* address) { - SbSocket new_socket = SbSocketAccept(socket_); + SbSocket new_socket; +#if SB_API_VERSION < 16 + new_socket = SbSocketAccept(socket_); if (!SbSocketIsValid(new_socket)) { int net_error = MapLastSocketError(socket_); +#else + if(!SbSocketIsValid(socket_)) { + return errno; + } + new_socket->socket_fd = accept(socket_->socket_fd, NULL, NULL); + if (new_socket->socket_fd < 0) { + int net_error = errno; + socket_->error = TranslateSocketErrnoToSbError(errno); +#endif if (net_error != ERR_IO_PENDING) { net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error); } @@ -191,7 +231,11 @@ int TCPSocketStarboard::AcceptInternal( // We use ReceiveFrom to get peer address of the newly connected socket. int received = SbSocketReceiveFrom(new_socket, &unused_byte, 0, &sb_address); if (received != 0) { +#if SB_API_VERSION < 16 int net_error = MapLastSocketError(new_socket); +#else + int net_error = errno; +#endif if (net_error != OK && net_error != ERR_IO_PENDING) { SbSocketDestroy(new_socket); net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error); @@ -312,7 +356,9 @@ void TCPSocketStarboard::OnSocketReadyToWrite(SbSocket socket) { int TCPSocketStarboard::Connect(const IPEndPoint& address, CompletionOnceCallback callback) { DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); + DCHECK(SbSocketIsValid(socket_)); + DCHECK(!peer_address_); DCHECK(!connect_pending()); @@ -330,9 +376,35 @@ int TCPSocketStarboard::Connect(const IPEndPoint& address, peer_address_.reset(new IPEndPoint(address)); +#if SB_API_VERSION < 16 SbSocketError result = SbSocketConnect(socket_, &storage); - int rv = MapLastSocketError(socket_); +#else + SockAddr sock_addr; + if (!sock_addr.FromSbSocketAddress(&storage)) { + SB_DLOG(ERROR) << __FUNCTION__ << ": Invalid address"; + return errno; + } + SB_DCHECK(socket_->socket_fd >= 0); + if (storage.type != socket_->address_type) { + SB_DLOG(ERROR) << __FUNCTION__ << ": Incompatible addresses: " + << "socket type = " << socket_->address_type + << ", argument type = " << storage.type; + return (int)kSbSocketErrorFailed; + } + int result = HANDLE_EINTR_WRAPPER( + connect(socket_->socket_fd, sock_addr.sockaddr(), sock_addr.length)); + if (result != 0 && errno == EINPROGRESS) { + return errno; + } + + if (result != 0) { + SB_DLOG(ERROR) << __FUNCTION__ << ": connect failed: " << errno; + return errno; + } + + int rv = errno; +#endif if (rv != ERR_IO_PENDING) { return HandleConnectCompleted(rv); } @@ -502,10 +574,17 @@ int TCPSocketStarboard::DoRead(IOBuffer* buf, int buf_len) { return bytes_read; } else { // If |bytes_read| < 0, some kind of error occurred. +#if SB_API_VERSION < 16 SbSocketError starboard_error = SbSocketGetLastError(socket_); int rv = MapSocketError(starboard_error); net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR, CreateNetLogSocketErrorCallback(rv, starboard_error)); +#else + int rv = errno; + net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR, + CreateNetLogSocketErrorCallback(rv, errno)); +#endif + if (rv != ERR_IO_PENDING) { DLOG(ERROR) << __FUNCTION__ << "[" << this << "]: Error: " << rv; } @@ -559,10 +638,16 @@ int TCPSocketStarboard::DoWrite(IOBuffer* buf, int buf_len) { return bytes_sent; } else { +#if SB_API_VERSION < 16 SbSocketError starboard_error = SbSocketGetLastError(socket_); int rv = MapSocketError(starboard_error); net_log_.AddEvent(NetLogEventType::SOCKET_WRITE_ERROR, CreateNetLogSocketErrorCallback(rv, starboard_error)); +#else + int rv = errno; + net_log_.AddEvent(NetLogEventType::SOCKET_WRITE_ERROR, + CreateNetLogSocketErrorCallback(rv, errno)); +#endif if (rv != ERR_IO_PENDING) { DLOG(ERROR) << __FUNCTION__ << "[" << this << "]: Error: " << rv; } @@ -620,8 +705,12 @@ int TCPSocketStarboard::GetLocalAddress(IPEndPoint* address) const { SbSocketAddress sb_address; if (!SbSocketGetLocalAddress(socket_, &sb_address)) { +#if SB_API_VERSION < 16 SbSocketError starboard_error = SbSocketGetLastError(socket_); return MapSocketError(starboard_error); +#else + return errno; +#endif } if (!address->FromSbSocketAddress(&sb_address)) { diff --git a/net/socket/udp_socket_starboard.cc b/net/socket/udp_socket_starboard.cc index 3abdcef2824d..7bd880c3d339 100644 --- a/net/socket/udp_socket_starboard.cc +++ b/net/socket/udp_socket_starboard.cc @@ -122,7 +122,11 @@ int UDPSocketStarboard::GetLocalAddress(IPEndPoint* address) const { if (!local_address_.get()) { SbSocketAddress address; if (!SbSocketGetLocalAddress(socket_, &address)) +#if SB_API_VERSION < 16 return MapLastSocketError(socket_); +#else + return errno; +#endif std::unique_ptr endpoint(new IPEndPoint()); if (!endpoint->FromSbSocketAddress(&address)) return ERR_FAILED; @@ -158,7 +162,11 @@ int UDPSocketStarboard::RecvFrom(IOBuffer* buf, socket_, true, base::MessageLoopCurrentForIO::WATCH_READ, &socket_watcher_, this)) { PLOG(ERROR) << "WatchSocket failed on read"; +#if SB_API_VERSION < 16 Error result = MapLastSocketError(socket_); +#else + int result = errno; +#endif if (result == ERR_IO_PENDING) { // Watch(...) might call SbSocketWaiterAdd() which does not guarantee // setting system error on failure, but we need to treat this as an @@ -209,9 +217,15 @@ int UDPSocketStarboard::SendToOrWrite(IOBuffer* buf, if (!base::MessageLoopForIO::current()->Watch( socket_, true, base::MessageLoopCurrentForIO::WATCH_WRITE, &socket_watcher_, this)) { +#if SB_API_VERSION < 16 DVLOG(1) << "Watch failed on write, error " << SbSocketGetLastError(socket_); Error result = MapLastSocketError(socket_); +#else + DVLOG(1) << "Watch failed on write, error " + << errno; + int result = errno; +#endif LogWrite(result, NULL, NULL); return result; } @@ -284,10 +298,18 @@ int UDPSocketStarboard::SetReceiveBufferSize(int32_t size) { int result = OK; if (!SbSocketSetReceiveBufferSize(socket_, size)) { +#if SB_API_VERSION < 16 result = MapLastSocketError(socket_); +#else + result =errno; +#endif } DCHECK_EQ(result, OK) << "Could not " << __FUNCTION__ << ": " +#if SB_API_VERSION < 16 << SbSocketGetLastError(socket_); +#else + << errno; +#endif return result; } @@ -297,10 +319,18 @@ int UDPSocketStarboard::SetSendBufferSize(int32_t size) { int result = OK; if (!SbSocketSetSendBufferSize(socket_, size)) { +#if SB_API_VERSION < 16 result = MapLastSocketError(socket_); +#else + result = errno; +#endif } DCHECK_EQ(result, OK) << "Could not " << __FUNCTION__ << ": " +#if SB_API_VERSION < 16 << SbSocketGetLastError(socket_); +#else + << errno; +#endif return result; } @@ -435,7 +465,11 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf, result = ERR_ADDRESS_INVALID; } } else { +#if SB_API_VERSION < 16 result = MapLastSocketError(socket_); +#else + result = errno; +#endif } if (result != ERR_IO_PENDING) { @@ -463,8 +497,11 @@ int UDPSocketStarboard::InternalSendTo(IOBuffer* buf, int result = SbSocketSendTo(socket_, buf->data(), buf_len, &sb_address); if (result < 0) +#if SB_API_VERSION < 16 result = MapLastSocketError(socket_); - +#else + result = errno; +#endif if (result != ERR_IO_PENDING) LogWrite(result, buf->data(), address); @@ -497,7 +534,11 @@ int UDPSocketStarboard::JoinGroup(const IPAddress& group_address) const { if (!SbSocketJoinMulticastGroup(socket_, &sb_address)) { LOG(WARNING) << "SbSocketJoinMulticastGroup failed on UDP socket."; +#if SB_API_VERSION < 16 return MapLastSocketError(socket_); +#else + return errno; +#endif } return OK; } @@ -571,7 +612,11 @@ SendResult UDPSocketStarboardSender::InternalSendBuffers( for (auto& buffer : buffers) { int result = Send(socket, buffer->data(), buffer->length(), address); if (result < 0) { +#if SB_API_VERSION < 16 rv = MapLastSocketError(socket); +#else + rv = errno; +#endif break; } write_count++; @@ -774,7 +819,11 @@ void UDPSocketStarboard::DidSendBuffers(SendResult send_result) { if (last_async_result_ == ERR_IO_PENDING) { DVLOG(2) << __func__ << " WatchSocket start"; if (!WatchSocket()) { +#if SB_API_VERSION < 16 last_async_result_ = MapLastSocketError(socket_); +#else + last_async_result_ = errno; +#endif DVLOG(1) << "WatchSocket failed on write, error: " << last_async_result_; LogWrite(last_async_result_, NULL, NULL); } else { diff --git a/starboard/android/shared/BUILD.gn b/starboard/android/shared/BUILD.gn index 2cfd29c7db6a..c73ed596e190 100644 --- a/starboard/android/shared/BUILD.gn +++ b/starboard/android/shared/BUILD.gn @@ -115,7 +115,6 @@ static_library("starboard_platform") { "//starboard/shared/posix/set_non_blocking_internal.cc", "//starboard/shared/posix/socket_accept.cc", "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_clear_last_error.cc", "//starboard/shared/posix/socket_connect.cc", "//starboard/shared/posix/socket_create.cc", "//starboard/shared/posix/socket_destroy.cc", diff --git a/starboard/common/socket.cc b/starboard/common/socket.cc index ae861168357b..90432407a08c 100644 --- a/starboard/common/socket.cc +++ b/starboard/common/socket.cc @@ -14,10 +14,12 @@ #include "starboard/common/socket.h" +#include #include #include "starboard/common/log.h" #include "starboard/configuration.h" +#include "starboard/shared/modular/posix_socket_wrappers.h" namespace starboard { @@ -105,11 +107,19 @@ bool Socket::IsPending() { } SbSocketError Socket::GetLastError() { +#if SB_API_VERSION < 16 return SbSocketGetLastError(socket_); +#else + return socket_->error; +#endif } void Socket::ClearLastError() { +#if SB_API_VERSION < 16 SbSocketClearLastError(socket_); +#else + socket_->socket_fd = kSbSocketOk; +#endif } int Socket::ReceiveFrom(char* out_data, diff --git a/starboard/common/socket.h b/starboard/common/socket.h index 8efaadaf22cb..661a8fae1496 100644 --- a/starboard/common/socket.h +++ b/starboard/common/socket.h @@ -24,6 +24,9 @@ #include "starboard/socket.h" #include "starboard/types.h" +#if SB_API_VERSION >= 16 +#include "starboard/shared/modular/posix_socket_wrappers.h" +#endif namespace starboard { diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index c4a9740d8b84..ca9d10c5065a 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -268,13 +268,11 @@ ExportedSymbols::ExportedSymbols() { #endif // SB_API_VERSION >= 15 REGISTER_SYMBOL(SbSocketAccept); REGISTER_SYMBOL(SbSocketBind); - REGISTER_SYMBOL(SbSocketClearLastError); REGISTER_SYMBOL(SbSocketConnect); REGISTER_SYMBOL(SbSocketCreate); REGISTER_SYMBOL(SbSocketDestroy); REGISTER_SYMBOL(SbSocketFreeResolution); REGISTER_SYMBOL(SbSocketGetInterfaceAddress); - REGISTER_SYMBOL(SbSocketGetLastError); REGISTER_SYMBOL(SbSocketGetLocalAddress); REGISTER_SYMBOL(SbSocketIsConnected); REGISTER_SYMBOL(SbSocketIsConnectedAndIdle); @@ -314,6 +312,8 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbStringFormat); REGISTER_SYMBOL(SbStringFormatWide); REGISTER_SYMBOL(SbStringScan); + REGISTER_SYMBOL(SbSocketClearLastError); + REGISTER_SYMBOL(SbSocketGetLastError); #endif // SB_API_VERSION < 16 REGISTER_SYMBOL(SbSystemBreakIntoDebugger); REGISTER_SYMBOL(SbSystemClearLastError); diff --git a/starboard/linux/shared/BUILD.gn b/starboard/linux/shared/BUILD.gn index f46e0d1a5a9a..037cac380d0a 100644 --- a/starboard/linux/shared/BUILD.gn +++ b/starboard/linux/shared/BUILD.gn @@ -182,7 +182,6 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/set_non_blocking_internal.cc", "//starboard/shared/posix/socket_accept.cc", "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_clear_last_error.cc", "//starboard/shared/posix/socket_connect.cc", "//starboard/shared/posix/socket_create.cc", "//starboard/shared/posix/socket_destroy.cc", diff --git a/starboard/nplb/socket_accept_test.cc b/starboard/nplb/socket_accept_test.cc index be933db44e4f..e5277cf7421f 100644 --- a/starboard/nplb/socket_accept_test.cc +++ b/starboard/nplb/socket_accept_test.cc @@ -14,7 +14,7 @@ // The accept SunnyDay case is tested as a subset of at least one other test // case, so it is not included redundantly here. - +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" #include "starboard/nplb/socket_helpers.h" #include "starboard/time.h" @@ -92,3 +92,4 @@ INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_clear_last_error_test.cc b/starboard/nplb/socket_clear_last_error_test.cc index d21f8669d4ec..7c713d751c51 100644 --- a/starboard/nplb/socket_clear_last_error_test.cc +++ b/starboard/nplb/socket_clear_last_error_test.cc @@ -14,7 +14,7 @@ // The Sunny Day test is used in several other tests as the way to detect if the // error is correct, so it is not repeated here. - +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" #include "starboard/nplb/socket_helpers.h" #include "testing/gtest/include/gtest/gtest.h" @@ -46,3 +46,4 @@ TEST(SbSocketClearLastErrorTest, RainyDayInvalidSocket) { } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_get_last_error_test.cc b/starboard/nplb/socket_get_last_error_test.cc index afdd63a612eb..6d11c300927b 100644 --- a/starboard/nplb/socket_get_last_error_test.cc +++ b/starboard/nplb/socket_get_last_error_test.cc @@ -14,7 +14,7 @@ // The Sunny Day test is used in several other tests as the way to detect if the // error is correct, so it is not repeated here. - +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" #include "testing/gtest/include/gtest/gtest.h" @@ -29,3 +29,4 @@ TEST(SbSocketGetLastErrorTest, RainyDayInvalidSocket) { } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_helpers.cc b/starboard/nplb/socket_helpers.cc index 6efecd239fa7..550059ad3087 100644 --- a/starboard/nplb/socket_helpers.cc +++ b/starboard/nplb/socket_helpers.cc @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - +#if SB_API_VERSION < 16 #include "starboard/nplb/socket_helpers.h" #include @@ -637,3 +637,4 @@ std::string GetSbSocketAddressTypeProtocolPairName( } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_is_connected_test.cc b/starboard/nplb/socket_is_connected_test.cc index 3c2bfdd9fe4f..424b84ee7993 100644 --- a/starboard/nplb/socket_is_connected_test.cc +++ b/starboard/nplb/socket_is_connected_test.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 #include #include "starboard/common/log.h" @@ -72,6 +73,7 @@ TEST_P(SbSocketIsConnectedTest, SunnyDayNotConnected) { TEST_P(SbSocketIsConnectedTest, SunnyDayListeningNotConnected) { SbSocket server_socket = CreateListeningTcpSocket(GetAddressType(), GetPortNumberForTests()); + ASSERT_TRUE(SbSocketIsValid(server_socket)); EXPECT_FALSE(SbSocketIsConnected(server_socket)); EXPECT_TRUE(SbSocketDestroy(server_socket)); @@ -107,3 +109,5 @@ INSTANTIATE_TEST_CASE_P( } // namespace } // namespace nplb } // namespace starboard + +#endif diff --git a/starboard/nplb/socket_join_multicast_group_test.cc b/starboard/nplb/socket_join_multicast_group_test.cc index f31fd0fcfc54..1dca2a607195 100644 --- a/starboard/nplb/socket_join_multicast_group_test.cc +++ b/starboard/nplb/socket_join_multicast_group_test.cc @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" #include "starboard/nplb/socket_helpers.h" #include "starboard/thread.h" @@ -123,3 +123,4 @@ TEST(SbSocketJoinMulticastGroupTest, RainyDayInvalidAddress) { } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_receive_from_test.cc b/starboard/nplb/socket_receive_from_test.cc index b9deb062ac22..4a069be665fb 100644 --- a/starboard/nplb/socket_receive_from_test.cc +++ b/starboard/nplb/socket_receive_from_test.cc @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - +#if SB_API_VERSION < 16 #include #include "starboard/common/socket.h" @@ -146,3 +146,4 @@ INSTANTIATE_TEST_CASE_P( } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_send_to_test.cc b/starboard/nplb/socket_send_to_test.cc index f0be4d56d065..c8fbae2e785a 100644 --- a/starboard/nplb/socket_send_to_test.cc +++ b/starboard/nplb/socket_send_to_test.cc @@ -14,7 +14,7 @@ // SendTo is largely tested with ReceiveFrom, so look there for more involved // tests. - +#if SB_API_VERSION < 16 #include #include "starboard/common/socket.h" @@ -208,3 +208,4 @@ INSTANTIATE_TEST_CASE_P( } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/nplb/socket_waiter_remove_test.cc b/starboard/nplb/socket_waiter_remove_test.cc index 12c3627ec057..10e0c056c03e 100644 --- a/starboard/nplb/socket_waiter_remove_test.cc +++ b/starboard/nplb/socket_waiter_remove_test.cc @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 #include "starboard/common/socket.h" #include "starboard/nplb/socket_helpers.h" @@ -94,3 +95,4 @@ INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, } // namespace } // namespace nplb } // namespace starboard +#endif diff --git a/starboard/raspi/shared/BUILD.gn b/starboard/raspi/shared/BUILD.gn index b7d3a5287eb4..6dbec366cc57 100644 --- a/starboard/raspi/shared/BUILD.gn +++ b/starboard/raspi/shared/BUILD.gn @@ -150,7 +150,6 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/set_non_blocking_internal.cc", "//starboard/shared/posix/socket_accept.cc", "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_clear_last_error.cc", "//starboard/shared/posix/socket_connect.cc", "//starboard/shared/posix/socket_create.cc", "//starboard/shared/posix/socket_destroy.cc", diff --git a/starboard/shared/libevent/socket_waiter_internal.cc b/starboard/shared/libevent/socket_waiter_internal.cc index 7ab3a4b3ad3c..21761039dfe2 100644 --- a/starboard/shared/libevent/socket_waiter_internal.cc +++ b/starboard/shared/libevent/socket_waiter_internal.cc @@ -56,7 +56,7 @@ SbSocket AcceptBySpinning(SbSocket server_socket, SbTime timeout) { } // If we didn't get a socket, it should be pending. - SB_DCHECK(SbSocketGetLastError(server_socket) == kSbSocketPending); + SB_DCHECK(server_socket->error == kSbSocketPending); // Check if we have passed our timeout. if (SbTimeGetMonotonicNow() - start >= timeout) { diff --git a/starboard/shared/modular/BUILD.gn b/starboard/shared/modular/BUILD.gn index 62bea7c5da78..19f57b07ecdc 100644 --- a/starboard/shared/modular/BUILD.gn +++ b/starboard/shared/modular/BUILD.gn @@ -16,6 +16,8 @@ # symbol visibility issues for windows based modular platform builds. source_set("posix_time_wrappers") { sources = [ + "posix_socket_wrappers.cc", + "posix_socket_wrappers.h", "posix_time_wrappers.cc", "posix_time_wrappers.h", ] diff --git a/starboard/shared/modular/posix_socket_wrappers.cc b/starboard/shared/modular/posix_socket_wrappers.cc new file mode 100644 index 000000000000..adf9f2f5c54f --- /dev/null +++ b/starboard/shared/modular/posix_socket_wrappers.cc @@ -0,0 +1,37 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "starboard/shared/modular/posix_socket_wrappers.h" + +SbSocketError TranslateSocketErrnoToSbError(int error) { + switch (error) { + case 0: + return kSbSocketOk; + case EINPROGRESS: + case EAGAIN: +#if EWOULDBLOCK != EAGAIN + case EWOULDBLOCK: +#endif + return kSbSocketPending; + case ECONNRESET: + case ENETRESET: + case EPIPE: + return kSbSocketErrorConnectionReset; + } + + SB_LOG(ERROR) << "Unknown posix socket error: " << error; + + // Here's where we would be more nuanced if we need to be. + return kSbSocketErrorFailed; +} diff --git a/starboard/shared/modular/posix_socket_wrappers.h b/starboard/shared/modular/posix_socket_wrappers.h new file mode 100644 index 000000000000..119c9a7e5c0d --- /dev/null +++ b/starboard/shared/modular/posix_socket_wrappers.h @@ -0,0 +1,107 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef STARBOARD_SHARED_MODULAR_POSIX_SOCKET_WRAPPERS_H_ +#define STARBOARD_SHARED_MODULAR_POSIX_SOCKET_WRAPPERS_H_ + +#include +#include +#include +#include + +#include "starboard/common/log.h" +#include "starboard/configuration.h" +#include "starboard/export.h" +#include "starboard/socket.h" +#include "starboard/types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct SbSocketWrapper; +typedef struct SbSocketWrapper SbSocketWrapper; + +// A handle to a socket. +typedef SbSocketWrapper* SbSocket; + +extern SbSocketError TranslateSocketErrnoToSbError(int error); + +// A helper class for converting back and forth from sockaddrs, ugh. +class SockAddr { + public: + SockAddr() : length(sizeof(storage_)) {} + ~SockAddr() {} + + // Initializes this SockAddr with the given SbSocketAddress, overwriting + // anything any address previously held. + bool FromSbSocketAddress(const SbSocketAddress* address); + + // Initializes the given SbSocketAddress with this SockAddr, which must have + // been previously initialized. + bool ToSbSocketAddress(SbSocketAddress* out_address) const; + + // Initializes this SockAddr with |sock_addr|, assuming it is appropriately + // sized for its type. + bool FromSockaddr(const struct sockaddr* sock_addr); + + // The sockaddr family. We only support INET and INET6. + sa_family_t family() const { return sockaddr()->sa_family; } + + struct sockaddr* sockaddr() { + return reinterpret_cast(&storage_); + } + + const struct sockaddr* sockaddr() const { + return reinterpret_cast(&storage_); + } + + struct sockaddr_in* sockaddr_in() { + return reinterpret_cast(&storage_); + } + + const struct sockaddr_in* sockaddr_in() const { + return reinterpret_cast(&storage_); + } + + struct sockaddr_in6* sockaddr_in6() { + return reinterpret_cast(&storage_); + } + + const struct sockaddr_in6* sockaddr_in6() const { + return reinterpret_cast(&storage_); + } + + // Public on purpose, because it will be handy to be passed directly by + // reference into other functions. + socklen_t length; + + private: + struct sockaddr_storage storage_; +}; + +#define HANDLE_EINTR_WRAPPER(x) \ + ({ \ + decltype(x) __eintr_result__; \ + do { \ + __eintr_result__ = (x); \ + } while (__eintr_result__ == -1 && errno == EINTR); \ + __eintr_result__; \ + }) + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // STARBOARD_SHARED_MODULAR_POSIX_SOCKET_WRAPPERS_H_ diff --git a/starboard/shared/posix/socket_clear_last_error.cc b/starboard/shared/posix/socket_clear_last_error.cc deleted file mode 100644 index 1610e80c7da1..000000000000 --- a/starboard/shared/posix/socket_clear_last_error.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include "starboard/shared/posix/socket_internal.h" - -bool SbSocketClearLastError(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - return false; - } - - socket->error = kSbSocketOk; - return true; -} diff --git a/starboard/shared/starboard/net_log.cc b/starboard/shared/starboard/net_log.cc index 9ca41928fc36..c0c1331b9736 100644 --- a/starboard/shared/starboard/net_log.cc +++ b/starboard/shared/starboard/net_log.cc @@ -189,7 +189,7 @@ class BufferedSocketWriter { bytes_to_write, NULL); if (result < 0) { - SbSocketError err = SbSocketGetLastError(dest_socket); + SbSocketError err = dest_socket->error; SbSocketClearLastError(dest_socket); if (err == kSbSocketPending) { blocked_counts_.increment(); diff --git a/starboard/shared/stub/socket_clear_last_error.cc b/starboard/shared/stub/socket_clear_last_error.cc deleted file mode 100644 index 03419f576ec4..000000000000 --- a/starboard/shared/stub/socket_clear_last_error.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -bool SbSocketClearLastError(SbSocket socket) { - return false; -} diff --git a/starboard/shared/win32/socket_clear_last_error.cc b/starboard/shared/win32/socket_clear_last_error.cc deleted file mode 100644 index 06f4a7deaed8..000000000000 --- a/starboard/shared/win32/socket_clear_last_error.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2017 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include "starboard/shared/win32/socket_internal.h" - -bool SbSocketClearLastError(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - return false; - } - - socket->error = kSbSocketOk; - return true; -} diff --git a/starboard/socket.h b/starboard/socket.h index adcd32b3c072..f96d685e650d 100644 --- a/starboard/socket.h +++ b/starboard/socket.h @@ -41,12 +41,14 @@ extern "C" { #endif +#if SB_API_VERSION < 16 // Private structure representing a socket, which may or may not be connected or // listening. typedef struct SbSocketPrivate SbSocketPrivate; // A handle to a socket. typedef SbSocketPrivate* SbSocket; +#endif // Enumeration of all Starboard socket operation results. Despite the enum // name, note that the value actually describes the outcome of an operation, @@ -126,6 +128,40 @@ typedef struct SbSocketResolution { // Well-defined value for an invalid socket handle. #define kSbSocketInvalid ((SbSocket)NULL) +#if SB_API_VERSION >= 16 +#include "starboard/shared/modular/posix_socket_wrappers.h" +#include "starboard/socket_waiter.h" +// Private structure representing a socket, which may or may not be connected or +// listening. +struct SbSocketWrapper { + SbSocketWrapper(SbSocketAddressType address_type, + SbSocketProtocol protocol, + int fd) + : address_type(address_type), + protocol(protocol), + socket_fd(fd), + error(kSbSocketOk), + waiter(kSbSocketWaiterInvalid) {} + ~SbSocketWrapper() {} + + // The address domain of this socket, IPv4 or IPv6. + SbSocketAddressType address_type; + + // The protocol of this socket, UDP or TCP. + SbSocketProtocol protocol; + + // The file descriptor for this socket. + int socket_fd; + + // The last error that occurred on this socket, or kSbSocketOk. + SbSocketError error; + + // The waiter this socket is registered with, or kSbSocketWaiterInvalid. + SbSocketWaiter waiter; +}; + +#endif + // Returns whether the given socket handle is valid. static SB_C_INLINE bool SbSocketIsValid(SbSocket socket) { return socket != kSbSocketInvalid; @@ -208,6 +244,7 @@ SB_EXPORT bool SbSocketIsConnected(SbSocket socket); // |socket|: The SbSocket to be checked. SB_EXPORT bool SbSocketIsConnectedAndIdle(SbSocket socket); +#if SB_API_VERSION < 16 // Returns the last error set on |socket|. If |socket| is not valid, this // function returns |kSbSocketErrorFailed|. // @@ -217,6 +254,7 @@ SB_EXPORT SbSocketError SbSocketGetLastError(SbSocket socket); // Clears the last error set on |socket|. The return value indicates whether // the socket error was cleared. SB_EXPORT bool SbSocketClearLastError(SbSocket socket); +#endif // Gets the address that this socket is bound to locally, if the socket is // connected. The return value indicates whether the address was retrieved diff --git a/starboard/socket_waiter.h b/starboard/socket_waiter.h index 18bbfbd9879c..00fe5679e116 100644 --- a/starboard/socket_waiter.h +++ b/starboard/socket_waiter.h @@ -39,6 +39,10 @@ #include "starboard/time.h" #include "starboard/types.h" +#if SB_API_VERSION >= 16 +#include "starboard/shared/modular/posix_socket_wrappers.h" +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/starboard/stub/BUILD.gn b/starboard/stub/BUILD.gn index d85bef3de7be..99fcf5cde3cf 100644 --- a/starboard/stub/BUILD.gn +++ b/starboard/stub/BUILD.gn @@ -159,7 +159,6 @@ static_library("stub_sources") { "//starboard/shared/stub/player_write_samples.cc", "//starboard/shared/stub/socket_accept.cc", "//starboard/shared/stub/socket_bind.cc", - "//starboard/shared/stub/socket_clear_last_error.cc", "//starboard/shared/stub/socket_connect.cc", "//starboard/shared/stub/socket_create.cc", "//starboard/shared/stub/socket_destroy.cc", diff --git a/starboard/win/shared/BUILD.gn b/starboard/win/shared/BUILD.gn index 97376937da70..6129a34c2063 100644 --- a/starboard/win/shared/BUILD.gn +++ b/starboard/win/shared/BUILD.gn @@ -239,7 +239,6 @@ static_library("starboard_platform") { "//starboard/shared/win32/set_non_blocking_internal.h", "//starboard/shared/win32/socket_accept.cc", "//starboard/shared/win32/socket_bind.cc", - "//starboard/shared/win32/socket_clear_last_error.cc", "//starboard/shared/win32/socket_connect.cc", "//starboard/shared/win32/socket_create.cc", "//starboard/shared/win32/socket_destroy.cc",