2020-08-15 13:51:41 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
2016-05-26 02:02:38 +00:00
|
|
|
|
2017-02-24 23:25:46 +00:00
|
|
|
#ifndef NOMINMAX
|
2016-05-26 02:02:38 +00:00
|
|
|
#define NOMINMAX
|
2017-02-24 23:25:46 +00:00
|
|
|
#endif
|
2016-05-26 02:02:38 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <sys/socket.h> /* socket definitions */
|
|
|
|
#include <sys/types.h> /* socket types */
|
|
|
|
#include <sys/wait.h> /* for waitpid() */
|
|
|
|
#include <netinet/in.h> /* struct sockaddr_in */
|
|
|
|
#include <arpa/inet.h> /* inet (3) funtions */
|
|
|
|
#include <unistd.h> /* misc. UNIX functions */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-08-24 04:32:06 +00:00
|
|
|
#include <cerrno>
|
2016-05-26 02:02:38 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/Net/Sinks.h"
|
2016-05-26 02:02:38 +00:00
|
|
|
|
2020-08-15 13:51:41 +00:00
|
|
|
#include "Common/Log.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileDescriptor.h"
|
2020-08-15 13:51:41 +00:00
|
|
|
|
2019-03-01 07:35:35 +00:00
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
// Default value to 0x00 (do nothing) in systems where it's not supported
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2016-05-26 02:02:38 +00:00
|
|
|
namespace net {
|
|
|
|
|
|
|
|
InputSink::InputSink(size_t fd) : fd_(fd), read_(0), write_(0), valid_(0) {
|
|
|
|
fd_util::SetNonBlocking((int)fd_, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::ReadLineWithEnding(std::string &s) {
|
|
|
|
size_t newline = FindNewline();
|
|
|
|
if (newline == BUFFER_SIZE) {
|
|
|
|
Block();
|
|
|
|
newline = FindNewline();
|
|
|
|
}
|
|
|
|
if (newline == BUFFER_SIZE) {
|
|
|
|
// Timed out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.resize(newline + 1);
|
|
|
|
if (read_ + newline + 1 > BUFFER_SIZE) {
|
|
|
|
// Need to do two reads.
|
|
|
|
size_t chunk1 = BUFFER_SIZE - read_;
|
|
|
|
size_t chunk2 = read_ + newline + 1 - BUFFER_SIZE;
|
|
|
|
memcpy(&s[0], buf_ + read_, chunk1);
|
|
|
|
memcpy(&s[chunk1], buf_, chunk2);
|
|
|
|
} else {
|
|
|
|
memcpy(&s[0], buf_ + read_, newline + 1);
|
|
|
|
}
|
|
|
|
AccountDrain(newline + 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputSink::ReadLineWithEnding() {
|
|
|
|
std::string s;
|
|
|
|
ReadLineWithEnding(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::ReadLine(std::string &s) {
|
|
|
|
bool result = ReadLineWithEnding(s);
|
|
|
|
if (result) {
|
|
|
|
size_t l = s.length();
|
|
|
|
if (l >= 2 && s[l - 2] == '\r' && s[l - 1] == '\n') {
|
|
|
|
s.resize(l - 2);
|
|
|
|
} else if (l >= 1 && s[l - 1] == '\n') {
|
|
|
|
s.resize(l - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputSink::ReadLine() {
|
|
|
|
std::string s;
|
|
|
|
ReadLine(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t InputSink::FindNewline() const {
|
|
|
|
// Technically, \r\n, but most parsers are lax... let's follow suit.
|
|
|
|
size_t until_end = std::min(valid_, BUFFER_SIZE - read_);
|
|
|
|
for (const char *p = buf_ + read_, *end = buf_ + read_ + until_end; p < end; ++p) {
|
|
|
|
if (*p == '\n') {
|
|
|
|
return p - (buf_ + read_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Were there more bytes wrapped around?
|
|
|
|
if (read_ + valid_ > BUFFER_SIZE) {
|
|
|
|
size_t wrapped = read_ + valid_ - BUFFER_SIZE;
|
|
|
|
for (const char *p = buf_, *end = buf_ + wrapped; p < end; ++p) {
|
|
|
|
if (*p == '\n') {
|
|
|
|
// Offset by the skipped portion before wrapping.
|
|
|
|
return (p - buf_) + until_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never found, return an invalid position to indicate.
|
|
|
|
return BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::TakeExact(char *buf, size_t bytes) {
|
|
|
|
while (bytes > 0) {
|
|
|
|
size_t drained = TakeAtMost(buf, bytes);
|
|
|
|
buf += drained;
|
|
|
|
bytes -= drained;
|
|
|
|
|
|
|
|
if (drained == 0) {
|
|
|
|
if (!Block()) {
|
|
|
|
// Timed out reading more bytes.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t InputSink::TakeAtMost(char *buf, size_t bytes) {
|
|
|
|
Fill();
|
|
|
|
|
|
|
|
// The least of: contiguous to read, actually populated in buffer, and wanted.
|
|
|
|
size_t avail = std::min(std::min(BUFFER_SIZE - read_, valid_), bytes);
|
|
|
|
|
|
|
|
if (avail != 0) {
|
|
|
|
memcpy(buf, buf_ + read_, avail);
|
|
|
|
AccountDrain(avail);
|
|
|
|
}
|
|
|
|
|
|
|
|
return avail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::Skip(size_t bytes) {
|
|
|
|
while (bytes > 0) {
|
|
|
|
size_t drained = std::min(valid_, bytes);
|
|
|
|
AccountDrain(drained);
|
|
|
|
bytes -= drained;
|
|
|
|
|
|
|
|
// Nothing left to read? Get more.
|
|
|
|
if (drained == 0) {
|
|
|
|
if (!Block()) {
|
|
|
|
// Timed out reading more bytes.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSink::Fill() {
|
|
|
|
// Avoid small reads if possible.
|
|
|
|
if (BUFFER_SIZE - valid_ > PRESSURE) {
|
|
|
|
// Whatever isn't valid and follows write_ is what's available.
|
|
|
|
size_t avail = BUFFER_SIZE - std::max(write_, valid_);
|
|
|
|
|
|
|
|
int bytes = recv(fd_, buf_ + write_, (int)avail, 0);
|
|
|
|
AccountFill(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::Block() {
|
|
|
|
if (!fd_util::WaitUntilReady((int)fd_, 5.0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fill();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSink::AccountFill(int bytes) {
|
|
|
|
if (bytes < 0) {
|
2020-08-24 04:32:06 +00:00
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
if (err == WSAEWOULDBLOCK)
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
#endif
|
2020-08-15 13:51:41 +00:00
|
|
|
ERROR_LOG(IO, "Error reading from socket");
|
2016-05-26 02:02:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, move forward (might be by zero.)
|
|
|
|
valid_ += bytes;
|
|
|
|
write_ += bytes;
|
|
|
|
if (write_ >= BUFFER_SIZE) {
|
|
|
|
write_ -= BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSink::AccountDrain(size_t bytes) {
|
|
|
|
valid_ -= bytes;
|
|
|
|
read_ += bytes;
|
|
|
|
if (read_ >= BUFFER_SIZE) {
|
|
|
|
read_ -= BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputSink::Empty() {
|
|
|
|
return valid_ == 0;
|
|
|
|
}
|
|
|
|
|
2018-04-12 04:33:52 +00:00
|
|
|
bool InputSink::TryFill() {
|
|
|
|
Fill();
|
|
|
|
return !Empty();
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:02:38 +00:00
|
|
|
OutputSink::OutputSink(size_t fd) : fd_(fd), read_(0), write_(0), valid_(0) {
|
|
|
|
fd_util::SetNonBlocking((int)fd_, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSink::Push(const std::string &s) {
|
|
|
|
return Push(&s[0], s.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSink::Push(const char *buf, size_t bytes) {
|
|
|
|
while (bytes > 0) {
|
|
|
|
size_t pushed = PushAtMost(buf, bytes);
|
|
|
|
buf += pushed;
|
|
|
|
bytes -= pushed;
|
|
|
|
|
|
|
|
if (pushed == 0) {
|
|
|
|
if (!Block()) {
|
|
|
|
// We couldn't write all the bytes.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSink::PushCRLF(const std::string &s) {
|
|
|
|
if (Push(s)) {
|
|
|
|
return Push("r\n", 2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OutputSink::PushAtMost(const char *buf, size_t bytes) {
|
|
|
|
Drain();
|
|
|
|
|
2016-05-27 01:06:03 +00:00
|
|
|
if (valid_ == 0 && bytes > PRESSURE) {
|
|
|
|
// Special case for pushing larger buffers: let's try to send directly.
|
2019-03-01 07:35:35 +00:00
|
|
|
int sentBytes = send(fd_, buf, (int)bytes, MSG_NOSIGNAL);
|
2016-05-27 01:06:03 +00:00
|
|
|
// If it was 0 or EWOULDBLOCK, that's fine, we'll enqueue as we can.
|
|
|
|
if (sentBytes > 0) {
|
|
|
|
return sentBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:02:38 +00:00
|
|
|
// Look for contiguous free space after write_ that's valid.
|
|
|
|
size_t avail = std::min(BUFFER_SIZE - std::max(write_, valid_), bytes);
|
|
|
|
|
|
|
|
if (avail != 0) {
|
|
|
|
memcpy(buf_ + write_, buf, avail);
|
|
|
|
AccountPush(avail);
|
|
|
|
}
|
|
|
|
|
|
|
|
return avail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OutputSink::Printf(const char *fmt, ...) {
|
|
|
|
// Let's start by checking how much space we have.
|
|
|
|
size_t avail = BUFFER_SIZE - std::max(write_, valid_);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
// Make a backup in case we don't have sufficient space.
|
|
|
|
va_list backup;
|
|
|
|
va_copy(backup, args);
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
int result = vsnprintf(buf_ + write_, avail, fmt, args);
|
2016-08-05 17:47:45 +00:00
|
|
|
if (result >= (int)avail) {
|
2016-05-26 02:02:38 +00:00
|
|
|
// There wasn't enough space. Let's use a buffer instead.
|
|
|
|
// This could be caused by wraparound.
|
|
|
|
char temp[BUFFER_SIZE];
|
|
|
|
result = vsnprintf(temp, BUFFER_SIZE, fmt, args);
|
|
|
|
|
2016-08-05 17:47:45 +00:00
|
|
|
if ((size_t)result < BUFFER_SIZE && result > 0) {
|
2016-05-26 02:02:38 +00:00
|
|
|
// In case it did return the null terminator.
|
|
|
|
if (temp[result - 1] == '\0') {
|
|
|
|
result--;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = Push(temp, result);
|
|
|
|
// We've written so there's nothing more.
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
va_end(backup);
|
|
|
|
|
|
|
|
// Okay, did we actually write?
|
2016-08-05 17:47:45 +00:00
|
|
|
if (result >= (int)avail) {
|
2016-05-26 02:02:38 +00:00
|
|
|
// This means the result string was too big for the buffer.
|
2020-08-15 13:51:41 +00:00
|
|
|
ERROR_LOG(IO, "Not enough space to format output.");
|
2016-05-26 02:02:38 +00:00
|
|
|
return false;
|
|
|
|
} else if (result < 0) {
|
2020-08-15 13:51:41 +00:00
|
|
|
ERROR_LOG(IO, "vsnprintf failed.");
|
2016-05-26 02:02:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
AccountPush(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSink::Block() {
|
|
|
|
if (!fd_util::WaitUntilReady((int)fd_, 5.0, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Drain();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-12 04:33:52 +00:00
|
|
|
bool OutputSink::Flush(bool allowBlock) {
|
2016-05-26 02:02:38 +00:00
|
|
|
while (valid_ > 0) {
|
|
|
|
size_t avail = std::min(BUFFER_SIZE - read_, valid_);
|
|
|
|
|
2019-03-01 07:35:35 +00:00
|
|
|
int bytes = send(fd_, buf_ + read_, (int)avail, MSG_NOSIGNAL);
|
2016-05-26 02:02:38 +00:00
|
|
|
AccountDrain(bytes);
|
|
|
|
|
|
|
|
if (bytes == 0) {
|
|
|
|
// This may also drain. Either way, keep looping.
|
2018-04-12 04:33:52 +00:00
|
|
|
if (!allowBlock || !Block()) {
|
2016-05-26 02:02:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-10 01:26:41 +00:00
|
|
|
} else if (bytes < 0) {
|
|
|
|
return false;
|
2016-05-26 02:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-16 20:29:44 +00:00
|
|
|
void OutputSink::Discard() {
|
|
|
|
read_ = 0;
|
|
|
|
write_ = 0;
|
|
|
|
valid_ = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:02:38 +00:00
|
|
|
void OutputSink::Drain() {
|
|
|
|
// Avoid small reads if possible.
|
|
|
|
if (valid_ > PRESSURE) {
|
|
|
|
// Let's just do contiguous valid.
|
|
|
|
size_t avail = std::min(BUFFER_SIZE - read_, valid_);
|
|
|
|
|
2019-03-01 07:35:35 +00:00
|
|
|
int bytes = send(fd_, buf_ + read_, (int)avail, MSG_NOSIGNAL);
|
2016-05-26 02:02:38 +00:00
|
|
|
AccountDrain(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSink::AccountPush(size_t bytes) {
|
|
|
|
valid_ += bytes;
|
|
|
|
write_ += bytes;
|
|
|
|
if (write_ >= BUFFER_SIZE) {
|
|
|
|
write_ -= BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSink::AccountDrain(int bytes) {
|
|
|
|
if (bytes < 0) {
|
2020-08-15 13:51:41 +00:00
|
|
|
ERROR_LOG(IO, "Error writing to socket");
|
2016-05-26 02:02:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid_ -= bytes;
|
|
|
|
read_ += bytes;
|
|
|
|
if (read_ >= BUFFER_SIZE) {
|
|
|
|
read_ -= BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSink::Empty() {
|
|
|
|
return valid_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|