Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Deprecate SbSocket and migrate to socket APIs #2198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions net/base/net_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <errno.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please file a bug to make <errno.h> work!

#endif
#include "starboard/system.h"
#endif

Expand Down Expand Up @@ -57,13 +61,16 @@ 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);

// Gets the last socket error as a net 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() {
Expand Down
2 changes: 2 additions & 0 deletions net/base/net_errors_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,5 +56,6 @@ Error MapSocketError(SbSocketError error) {
return ERR_FAILED;
}
}
#endif // SB_API_VERSION < 16

} // namespace net
110 changes: 108 additions & 2 deletions net/socket/tcp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include <memory>
#include <utility>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include "base/callback_helpers.h"
#include "base/time/time.h"
Expand All @@ -24,6 +27,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 {

Expand All @@ -50,7 +55,32 @@ 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(ConvertSocketAddressTypeToDomain(ConvertAddressFamily(family)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just temporary till you get all the APIs adjusted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

SOCK_STREAM, IPPROTO_TCP);
if (socket_fd < 0) {
socket_= kSbSocketInvalid;
} else if (!SocketSetNonBlocking(socket_fd)) {
// Something went wrong, we'll clean up (preserving errno) and return
// failure.
int save_errno = errno;
HANDLE_EINTR_WRAPPER(close(socket_fd));
errno = save_errno;
socket_ = kSbSocketInvalid;
} else {
socket_ = new SbSocketWrapper(ConvertAddressFamily(family), kSbSocketProtocolTcp, socket_fd);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a SbSocketWrapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structure SbSocket members are associated to each other, like the address, the type, the error, and socket_waiter. It is convenient to keep them in a group.

}
#if !defined(MSG_NOSIGNAL) && defined(SO_NOSIGPIPE)
// Use SO_NOSIGPIPE to mute SIGPIPE on darwin systems.
int optval_set = 1;
setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE,
reinterpret_cast<void*>(&optval_set), sizeof(int));
#endif

#endif // SB_API_VERSION < 16

if (!SbSocketIsValid(socket_)) {
return MapLastSystemError();
}
Expand Down Expand Up @@ -98,7 +128,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));
Expand All @@ -115,7 +149,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;
}
Expand All @@ -142,7 +180,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr<TCPSocketStarboard>* 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;
Expand All @@ -156,7 +198,11 @@ int TCPSocketStarboard::Accept(std::unique_ptr<TCPSocketStarboard>* 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;
}
Expand All @@ -177,9 +223,20 @@ void TCPSocketStarboard::SetDefaultOptionsForClient() {
int TCPSocketStarboard::AcceptInternal(
std::unique_ptr<TCPSocketStarboard>* 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);
}
Expand All @@ -191,7 +248,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);
Expand Down Expand Up @@ -312,7 +373,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());

Expand All @@ -330,9 +393,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);
}
Expand Down Expand Up @@ -502,10 +591,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;
}
Expand Down Expand Up @@ -559,10 +655,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;
}
Expand Down Expand Up @@ -620,8 +722,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)) {
Expand Down
Loading