diff --git a/starboard/shared/libevent/socket_waiter_internal.cc b/starboard/shared/libevent/socket_waiter_internal.cc index 1720adc20d1b..53c2660f7117 100644 --- a/starboard/shared/libevent/socket_waiter_internal.cc +++ b/starboard/shared/libevent/socket_waiter_internal.cc @@ -33,16 +33,6 @@ namespace sbposix = starboard::shared::posix; -#if defined(_GNU_SOURCE) || defined(_POSIX_VERSION) -#if defined(PLAYSTATION_GENERATION) && (PLAYSTATION_GENERATION <= 5) -#define USE_POSIX_PIPE 0 -#else -#define USE_POSIX_PIPE 1 -#endif -#else -#define USE_POSIX_PIPE 0 -#endif - namespace { // We do this because it's our style to use explicitly-sized ints when not just // using int, but libevent uses shorts explicitly in its interface. @@ -57,68 +47,6 @@ SbSocketAddress GetIpv4Localhost() { address.address[3] = 1; return address; } - -SbSocket AcceptBySpinning(SbSocket server_socket, int64_t timeout) { - int64_t start = starboard::CurrentMonotonicTime(); - while (true) { - SbSocket accepted_socket = SbSocketAccept(server_socket); - if (SbSocketIsValid(accepted_socket)) { - return accepted_socket; - } - - // If we didn't get a socket, it should be pending. - SB_DCHECK(SbSocketGetLastError(server_socket) == kSbSocketPending); - - // Check if we have passed our timeout. - if (starboard::CurrentMonotonicTime() - start >= timeout) { - break; - } - - // Just being polite. - sched_yield(); - } - - return kSbSocketInvalid; -} - -#if !USE_POSIX_PIPE -void GetSocketPipe(SbSocket* client_socket, SbSocket* server_socket) { - int result; - SbSocketError sb_socket_result; - const int64_t kTimeoutUsec = 1'000'000 / 15; - SbSocketAddress address = GetIpv4Localhost(); - - // Setup a listening socket. - SbSocket listen_socket = - SbSocketCreate(kSbSocketAddressTypeIpv4, kSbSocketProtocolTcp); - SB_DCHECK(SbSocketIsValid(listen_socket)); - result = SbSocketSetReuseAddress(listen_socket, true); - SB_DCHECK(result); - sb_socket_result = SbSocketBind(listen_socket, &address); - SB_DCHECK(sb_socket_result == kSbSocketOk); - sb_socket_result = SbSocketListen(listen_socket); - SB_DCHECK(sb_socket_result == kSbSocketOk); - // Update the address after a free port has been assigned. - SbSocketGetLocalAddress(listen_socket, &address); - - // Create a new socket to connect to the listening socket. - *client_socket = - SbSocketCreate(kSbSocketAddressTypeIpv4, kSbSocketProtocolTcp); - SB_DCHECK(SbSocketIsValid(*client_socket)); - // This connect will probably return pending, but we'll assume it will connect - // eventually. - sb_socket_result = SbSocketConnect(*client_socket, &address); - SB_DCHECK(sb_socket_result == kSbSocketOk || - sb_socket_result == kSbSocketPending); - - // Spin until the accept happens (or we get impatient). - *server_socket = AcceptBySpinning(listen_socket, kTimeoutUsec); - SB_DCHECK(SbSocketIsValid(*server_socket)); - - result = SbSocketDestroy(listen_socket); - SB_DCHECK(result); -} -#endif } // namespace SbSocketWaiterPrivate::SbSocketWaiterPrivate() @@ -126,7 +54,6 @@ SbSocketWaiterPrivate::SbSocketWaiterPrivate() base_(event_base_new()), waiting_(false), woken_up_(false) { -#if USE_POSIX_PIPE int fds[2]; int result = pipe(fds); SB_DCHECK(result == 0); @@ -138,16 +65,6 @@ SbSocketWaiterPrivate::SbSocketWaiterPrivate() wakeup_write_fd_ = fds[1]; result = sbposix::SetNonBlocking(wakeup_write_fd_); SB_DCHECK(result); -#else - GetSocketPipe(&client_socket_, &server_socket_); - - // Set TCP_NODELAY on the server socket, so it immediately sends its tiny - // payload without waiting for more data. - SbSocketSetTcpNoDelay(server_socket_, true); - - wakeup_read_fd_ = client_socket_->socket_fd; - wakeup_write_fd_ = server_socket_->socket_fd; -#endif event_set(&wakeup_event_, wakeup_read_fd_, EV_READ | EV_PERSIST, &SbSocketWaiterPrivate::LibeventWakeUpCallback, this); @@ -173,13 +90,8 @@ SbSocketWaiterPrivate::~SbSocketWaiterPrivate() { event_del(&wakeup_event_); event_base_free(base_); -#if USE_POSIX_PIPE close(wakeup_read_fd_); close(wakeup_write_fd_); -#else - SbSocketDestroy(server_socket_); - SbSocketDestroy(client_socket_); -#endif } bool SbSocketWaiterPrivate::Add(int socket, @@ -547,5 +459,3 @@ SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::RemoveWaitee( sb_waitees_.erase(it); return result; } - -#undef USE_POSIX_PIPE diff --git a/starboard/shared/starboard/net_args.cc b/starboard/shared/starboard/net_args.cc deleted file mode 100644 index afd1f2d4e542..000000000000 --- a/starboard/shared/starboard/net_args.cc +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright 2018 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/starboard/net_args.h" - -#include - -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/common/socket.h" -#include "starboard/common/time.h" -#include "starboard/socket_waiter.h" -#include "starboard/thread.h" - -#ifndef NET_ARGS_PORT -#define NET_ARGS_PORT 49355 -#endif - -// Controls whether using IPv4 or IPv6. -#ifndef NET_ARGS_IP_VERSION -#define NET_ARGS_IP_VERSION kSbSocketAddressTypeIpv4 -#endif - -namespace starboard { -namespace shared { -namespace starboard { -namespace { - -std::unique_ptr CreateListenSocket() { - std::unique_ptr socket( - new Socket(NET_ARGS_IP_VERSION, kSbSocketProtocolTcp)); - socket->SetReuseAddress(true); - SbSocketAddress sock_addr; - // Ip address will be set to 0.0.0.0 so that it will bind to all sockets. - memset(&sock_addr, 0, sizeof(SbSocketAddress)); - sock_addr.type = NET_ARGS_IP_VERSION; - sock_addr.port = NET_ARGS_PORT; - SbSocketError sock_err = socket->Bind(&sock_addr); - - const char kErrFmt[] = "Socket error while attempting to bind, error = %d\n"; - if (sock_err != kSbSocketOk) { - SbLogRawFormatF(kErrFmt, sock_err); - } - sock_err = socket->Listen(); - if (sock_err != kSbSocketOk) { - SbLogRawFormatF(kErrFmt, sock_err); - } - return socket; -} - -void WaitUntilReadableOrConnectionReset(SbSocket sock) { - SbSocketWaiter waiter = SbSocketWaiterCreate(); - - struct F { - static void WakeUp(SbSocketWaiter waiter, SbSocket, void*, int) { - SbSocketWaiterWakeUp(waiter); - } - }; - - SbSocketWaiterAdd(waiter, sock, NULL, &F::WakeUp, kSbSocketWaiterInterestRead, - false); // false means one shot. - - SbSocketWaiterWait(waiter); - SbSocketWaiterRemove(waiter, sock); - SbSocketWaiterDestroy(waiter); -} - -std::unique_ptr WaitForClientConnection(Socket* listen_sock, - int64_t timeout) { - int64_t expire_time = (timeout >= 0) && (timeout < kSbInt64Max) - ? CurrentMonotonicTime() + timeout - : kSbInt64Max; - while (true) { - std::unique_ptr client_connection(listen_sock->Accept()); - if (client_connection) { - return client_connection; - } - if (CurrentMonotonicTime() > expire_time) { - return std::unique_ptr(); - } - usleep(1000); - } -} - -std::vector SplitStringByLines(const std::string& string_buff) { - std::vector lines; - std::stringstream ss; - ss << string_buff; - for (std::string line; std::getline(ss, line);) { - if (!line.empty()) { - lines.push_back(line); - } - } - return lines; -} - -} // namespace. - -// Command line switch useful for determining if NetArgsWaitForConnection() -// should be called. -const char kNetArgsCommandSwitchWait[] = "net_args_wait_for_connection"; - -std::vector NetArgsWaitForPayload(int64_t timeout) { - std::unique_ptr listen = CreateListenSocket(); - std::unique_ptr client_connection = - WaitForClientConnection(listen.get(), timeout); - if (!client_connection) { - SB_LOG(ERROR) << "Timed out waiting for net args."; - return std::vector(); - } - - std::string str_buff; - - while (true) { - char buff[128]; - int result = client_connection->ReceiveFrom(buff, sizeof(buff), NULL); - if (result > 0) { - str_buff.append(buff, static_cast(result)); - continue; - } else if (result == 0) { - // Socket has closed. - break; - } else if (result < 0) { // Handle error condition. - SbSocketError err = client_connection->GetLastError(); - client_connection->ClearLastError(); - - switch (err) { - case kSbSocketOk: { - SB_NOTREACHED() << "Expected error condition when return val " - << "is < 0."; - continue; - } - case kSbSocketPending: { - WaitUntilReadableOrConnectionReset(client_connection->socket()); - continue; - } - default: { - break; - } - } - } - } - return SplitStringByLines(str_buff); -} - -} // namespace starboard -} // namespace shared -} // namespace starboard diff --git a/starboard/shared/starboard/net_args.h b/starboard/shared/starboard/net_args.h deleted file mode 100644 index fcee5cd24170..000000000000 --- a/starboard/shared/starboard/net_args.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2018 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. -// -// NetArgs is an optional component that allows socket access to tell an -// executable what it's command args are. -// -// ****** // -// TODO: Remove the following: -// It's recommended to call NetLogWaitForClientConnected() to ensure that the -// NetLog is running a predictable time from launch. It's also STRONGLY -// recommended that NetLogFlushThenClose() is called during shutdown to -// guarantee that any pending data is flushed out through the network layer. -// Failure to do this will often result in the netlog being truncated as the -// process shuts down. -// -// The netlog create a server that will bind to ip 0.0.0.0 (and similar in -// ipv6) and will listen for client connections. See net_log.cc for which -// port net_log will listen to. -// -// See also net_log.py for an example script that can connect to a running -// netlog instance. - -#ifndef STARBOARD_SHARED_STARBOARD_NET_ARGS_H_ -#define STARBOARD_SHARED_STARBOARD_NET_ARGS_H_ - -#include -#include - -namespace starboard { -namespace shared { -namespace starboard { - -// Command line switch useful for determining if NetArgsWaitForConnection() -// should be called. -extern const char kNetArgsCommandSwitchWait[]; - -// Waits until args stream in with a socket connection and data reception. -// Timeout is in microseconds. A timeout value of < 0 will signal infinite -// timeout. -std::vector NetArgsWaitForPayload(int64_t timeout); - -} // namespace starboard -} // namespace shared -} // namespace starboard - -#endif // STARBOARD_SHARED_STARBOARD_NET_ARGS_H_