Merge pull request #21823 from hrydgard/more-misc-stuff

More networking cleanups and misc stuff
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2026-06-13 18:15:21 +02:00
commit 72fdcb25d9
12 files changed
+64 -50

No files matched your search

+30 -22
View File
@@ -54,11 +54,11 @@ std::string Connection::GetLocalIpAsString() const {
bool Connection::Resolve(const char *host, int port, DNSType type) {
if ((intptr_t)sock_ != -1) {
ERROR_LOG(Log::IO, "Resolve: Already have a socket");
ERROR_LOG(Log::Net, "Resolve: Already have a socket");
return false;
}
if (!host || port < 1 || port > 65535) {
ERROR_LOG(Log::IO, "Resolve: Invalid host or port (%d)", port);
ERROR_LOG(Log::Net, "Resolve: Invalid host or port (%d)", port);
return false;
}
@@ -76,7 +76,7 @@ bool Connection::Resolve(const char *host, int port, DNSType type) {
std::string err;
if (!net::DNSResolve(processedHostname.c_str(), port_str, &resolved_, err, type)) {
WARN_LOG(Log::IO, "Failed to resolve host '%s': '%s' (%s)", host, err.c_str(), DNSTypeAsString(type));
WARN_LOG(Log::Net, "Failed to resolve host '%s': '%s' (%s)", host, err.c_str(), DNSTypeAsString(type));
// Zero port so that future calls fail.
port_ = 0;
return false;
@@ -99,7 +99,7 @@ static void FormatAddr(char *addrbuf, size_t bufsize, const addrinfo *info) {
bool Connection::Connect(int maxTries, double timeout, bool *cancelConnect) {
if (port_ <= 0) {
ERROR_LOG(Log::IO, "Bad port");
ERROR_LOG(Log::Net, "Bad port");
return false;
}
sock_ = -1;
@@ -115,13 +115,13 @@ bool Connection::Connect(int maxTries, double timeout, bool *cancelConnect) {
int sock = socket(possible->ai_family, SOCK_STREAM, IPPROTO_TCP);
if ((intptr_t)sock == -1) {
ERROR_LOG(Log::IO, "Bad socket");
ERROR_LOG(Log::Net, "Bad socket");
continue;
}
// Windows sockets aren't limited by socket number, just by count, so checking FD_SETSIZE there is wrong.
#if !PPSSPP_PLATFORM(WINDOWS)
if (sock >= FD_SETSIZE) {
ERROR_LOG(Log::IO, "Socket doesn't fit in FD_SET: %d We probably have a leak.", sock);
ERROR_LOG(Log::Net, "Socket doesn't fit in FD_SET: %d We probably have a leak.", sock);
closesocket(sock);
continue;
}
@@ -194,7 +194,7 @@ bool Connection::Connect(int maxTries, double timeout, bool *cancelConnect) {
}
if (cancelConnect && *cancelConnect) {
WARN_LOG(Log::HTTP, "connect: cancelled (2): %s:%d", host_.c_str(), port_);
WARN_LOG(Log::Net, "connect: cancelled (2): %s:%d", host_.c_str(), port_);
break;
}
@@ -229,8 +229,6 @@ Client::~Client() {
Disconnect();
}
// Ignores line folding (deprecated), but respects field combining.
// Don't use for Set-Cookie, which is a special header per RFC 7230.
bool GetHeaderValue(const std::vector<std::string> &responseHeaders, std::string_view header, std::string *value) {
std::string search(header);
search.push_back(':');
@@ -317,7 +315,7 @@ int Client::POST(const RequestParams &req, std::string_view data, std::string_vi
if (mime.empty()) {
snprintf(otherHeaders, sizeof(otherHeaders), "Content-Length: %lld\r\n", (long long)data.size());
} else {
snprintf(otherHeaders, sizeof(otherHeaders), "Content-Length: %lld\r\nContent-Type: %.*s\r\n", (long long)data.size(), (int)mime.size(), mime.data());
snprintf(otherHeaders, sizeof(otherHeaders), "Content-Length: %lld\r\nContent-Type: %.*s\r\n", (long long)data.size(), STR_VIEW(mime));
}
int err = SendRequestWithData("POST", req, data, otherHeaders, progress);
@@ -348,7 +346,9 @@ int Client::SendRequest(const char *method, const RequestParams &req, const char
}
int Client::SendRequestWithData(const char *method, const RequestParams &req, std::string_view data, const char *otherHeaders, net::RequestProgress *progress) {
progress->Update(0, 0, false);
if (progress) {
progress->Update(0, 0, false);
}
net::Buffer buffer;
const char *tpl =
@@ -367,7 +367,7 @@ int Client::SendRequestWithData(const char *method, const RequestParams &req, st
req.acceptMime,
otherHeaders ? otherHeaders : "");
buffer.Append(data);
bool flushed = buffer.FlushSocket(sock(), dataTimeout_, progress->cancelled);
bool flushed = buffer.FlushSocket(sock(), headerTimeout_, progress ? progress->cancelled : nullptr);
if (!flushed) {
return -1; // TODO error code.
}
@@ -378,9 +378,9 @@ int Client::ReadResponseHeaders(net::Buffer *readbuf, std::vector<std::string> &
// Snarf all the data we can into RAM. A little unsafe but hey.
static constexpr float CANCEL_INTERVAL = 0.25f;
bool ready = false;
double endTimeout = time_now_d() + dataTimeout_;
double endTimeout = time_now_d() + headerTimeout_;
while (!ready) {
if (progress->cancelled && *progress->cancelled)
if (progress && progress->cancelled && *progress->cancelled)
return -1;
ready = fd_util::WaitUntilReady(sock(), CANCEL_INTERVAL, false);
if (!ready && time_now_d() > endTimeout) {
@@ -417,15 +417,16 @@ int Client::ReadResponseHeaders(net::Buffer *readbuf, std::vector<std::string> &
return -1;
}
if (statusLine)
*statusLine = line;
if (statusLine) {
*statusLine = std::move(line);
}
while (true) {
int sz = readbuf->TakeLineCRLF(&line);
if (!sz || sz < 0)
break;
VERBOSE_LOG(Log::HTTP, "Header line: %s", line.c_str());
responseHeaders.push_back(line);
responseHeaders.emplace_back(line);
}
if (responseHeaders.size() == 0) {
@@ -479,7 +480,9 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
if (chunked) {
if (!DeChunk(readbuf, output, contentLength)) {
ERROR_LOG(Log::HTTP, "Bad chunked data, couldn't read chunk size");
progress->Update(0, 0, true);
if (progress) {
progress->Update(0, 0, true);
}
return -1;
}
} else {
@@ -493,20 +496,23 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
bool result = decompress_string(compressed, &decompressed);
if (!result) {
ERROR_LOG(Log::HTTP, "Error decompressing using zlib");
progress->Update(0, 0, true);
if (progress) {
progress->Update(0, 0, true);
}
return -1;
}
output->Append(decompressed);
}
}
progress->Update(contentLength, contentLength, true);
if (progress) {
progress->Update(contentLength, contentLength, true);
}
return 0;
}
HTTPRequest::HTTPRequest(RequestMethod method, std::string_view url, std::string_view postData, std::string_view postMime, const Path &outfile, RequestFlags flags, net::ResolveFunc customResolve, std::string_view name)
: Request(method, url, name, &cancelled_, flags), postData_(postData), postMime_(postMime), customResolve_(customResolve) {
outfile_ = outfile;
: Request(method, url, name, outfile, &cancelled_, flags), postData_(postData), postMime_(postMime), customResolve_(customResolve) {
}
HTTPRequest::~HTTPRequest() {
@@ -530,6 +536,8 @@ void HTTPRequest::Join() {
}
void HTTPRequest::SetFailed(int code) {
// TODO: Why are we not using code here?
failed_ = true;
progress_.Update(0, 0, true);
completed_ = true;
+6 -4
View File
@@ -27,7 +27,7 @@ public:
bool Connect(int maxTries = 2, double timeout = 20.0f, bool *cancelConnect = nullptr);
void Disconnect();
// Only to be used for bring-up and debugging.
// TODO: Try to expose this less.
uintptr_t sock() const { return sock_; }
std::string GetLocalIpAsString() const;
@@ -49,6 +49,8 @@ private:
namespace http {
// Ignores line folding (deprecated), but respects field combining.
// Don't use for Set-Cookie, which is a special header per RFC 7230.
bool GetHeaderValue(const std::vector<std::string> &responseHeaders, std::string_view header, std::string *value);
class RequestParams {
@@ -83,7 +85,7 @@ public:
int ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::string> &responseHeaders, Buffer *output, net::RequestProgress *progress);
void SetDataTimeout(double t) {
dataTimeout_ = t;
headerTimeout_ = t;
}
void SetUserAgent(std::string_view value) {
@@ -97,7 +99,7 @@ public:
protected:
std::string userAgent_;
const char* httpVersion_;
double dataTimeout_ = 900.0;
double headerTimeout_ = 900.0;
};
// Really an asynchronous request.
@@ -132,7 +134,7 @@ private:
class CachedRequest : public Request {
public:
CachedRequest(RequestMethod method, std::string_view url, std::string_view name, bool *cancelled, RequestFlags flags, std::string_view responseData)
: Request(method, url, name, cancelled, flags)
: Request(method, url, name, Path(), cancelled, flags)
{
buffer_.Append(responseData);
}
+5 -5
View File
@@ -31,7 +31,7 @@ bool RequestHeader::GetParamValue(const char *param_name, std::string *value) co
for (size_t i = 0; i < v.size(); i++) {
std::vector<std::string_view> parts;
SplitString(v[i], '=', parts);
DEBUG_LOG(Log::IO, "Param: %.*s Value: %.*s", (int)parts[0].size(), parts[0].data(), (int)parts[1].size(), parts[1].data());
DEBUG_LOG(Log::HTTP, "Param: %.*s Value: %.*s", (int)parts[0].size(), parts[0].data(), (int)parts[1].size(), parts[1].data());
if (parts[0] == param_name) {
*value = parts[1];
return true;
@@ -119,13 +119,13 @@ int RequestHeader::ParseHttpHeader(const char *buffer) {
if (!strncasecmp(key, "User-Agent", key_len)) {
user_agent = new char[value_len + 1];
memcpy(user_agent, buffer, value_len + 1);
VERBOSE_LOG(Log::IO, "user-agent: %s", user_agent);
VERBOSE_LOG(Log::HTTP, "user-agent: %s", user_agent);
} else if (!strncasecmp(key, "Referer", key_len)) {
referer = new char[value_len + 1];
memcpy(referer, buffer, value_len + 1);
} else if (!strncasecmp(key, "Content-Length", key_len)) {
content_length = atoi(buffer);
VERBOSE_LOG(Log::IO, "Content-Length: %i", (int)content_length);
VERBOSE_LOG(Log::HTTP, "Content-Length: %i", (int)content_length);
} else {
std::string key_str(key, key_len);
std::transform(key_str.begin(), key_str.end(), key_str.begin(), tolower);
@@ -148,12 +148,12 @@ void RequestHeader::ParseHeaders(net::InputSink *sink) {
line_count++;
if (type == SIMPLE) {
// Done!
VERBOSE_LOG(Log::IO, "Simple: Done parsing http request.");
VERBOSE_LOG(Log::HTTP, "Simple: Done parsing http request.");
break;
}
}
VERBOSE_LOG(Log::IO, "finished parsing request.");
VERBOSE_LOG(Log::HTTP, "finished parsing request.");
ok = line_count > 1 && resource != nullptr;
}
+10 -11
View File
@@ -13,8 +13,7 @@
namespace http {
HTTPSRequest::HTTPSRequest(RequestMethod method, std::string_view url, std::string_view postData, std::string_view postMime, const Path &outfile, RequestFlags flags, std::string_view name)
: Request(method, url, name, &cancelled_, flags), method_(method), postData_(postData), postMime_(postMime) {
outfile_ = outfile;
: Request(method, url, name, outfile, &cancelled_, flags), postData_(postData), postMime_(postMime) {
}
HTTPSRequest::~HTTPSRequest() {
@@ -61,7 +60,7 @@ void HTTPSRequest::Join() {
res_ = nullptr;
req_ = nullptr;
} else {
ERROR_LOG(Log::IO, "HTTPSDownload::Join not implemented");
ERROR_LOG(Log::HTTP, "HTTPSRequest::Join called before completion");
}
}
@@ -88,22 +87,22 @@ bool HTTPSRequest::Done() {
// It's a naett error. Translate and handle.
switch (resultCode_) {
case naettConnectionError: // -1
ERROR_LOG(Log::IO, "Connection error");
ERROR_LOG(Log::HTTP, "Connection error");
break;
case naettProtocolError: // -2
ERROR_LOG(Log::IO, "Protocol error");
ERROR_LOG(Log::HTTP, "Protocol error");
break;
case naettReadError: // -3
ERROR_LOG(Log::IO, "Read error");
ERROR_LOG(Log::HTTP, "Read error");
break;
case naettWriteError: // -4
ERROR_LOG(Log::IO, "Write error");
ERROR_LOG(Log::HTTP, "Write error");
break;
case naettGenericError: // -5
ERROR_LOG(Log::IO, "Generic error");
ERROR_LOG(Log::HTTP, "Generic error");
break;
default:
ERROR_LOG(Log::IO, "Unhandled naett error %d", resultCode_);
ERROR_LOG(Log::HTTP, "Unhandled naett error %d", resultCode_);
break;
}
failed_ = true;
@@ -111,11 +110,11 @@ bool HTTPSRequest::Done() {
} else if (resultCode_ == 200) {
bool clear = !(flags_ & RequestFlags::KeepInMemory);
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_, clear)) {
ERROR_LOG(Log::IO, "Failed writing download to '%s'", outfile_.c_str());
ERROR_LOG(Log::HTTP, "Failed writing download to '%s'", outfile_.c_str());
}
progress_.Update(bodyLength, bodyLength, true);
} else {
WARN_LOG(Log::IO, "Naett request failed: %d", resultCode_);
WARN_LOG(Log::HTTP, "Naett request failed: %d", resultCode_);
failed_ = true;
progress_.Update(0, 0, true);
}
-1
View File
@@ -25,7 +25,6 @@ public:
bool Failed() const override { return failed_; }
private:
RequestMethod method_;
std::string postData_;
std::string postMime_;
bool completed_ = false;
+2 -2
View File
@@ -10,8 +10,8 @@
namespace http {
Request::Request(RequestMethod method, std::string_view url, std::string_view name, bool *cancelled, RequestFlags flags)
: method_(method), url_(url), name_(name), progress_(cancelled), flags_(flags) {
Request::Request(RequestMethod method, std::string_view url, std::string_view name, const Path &outFile, bool *cancelled, RequestFlags flags)
: method_(method), url_(url), name_(name), outfile_(outFile), progress_(cancelled), flags_(flags) {
INFO_LOG(Log::HTTP, "HTTP %s request: %.*s (%.*s)", RequestMethodToString(method), (int)url.size(), url.data(), (int)name.size(), name.data());
progress_.callback = [this](int64_t bytes, int64_t contentLength, bool done) {
+1 -1
View File
@@ -31,7 +31,7 @@ using RequestCompletionCallback = std::function<void(Request &)>;
// Abstract request.
class Request {
public:
Request(RequestMethod method, std::string_view url, std::string_view name, bool *cancelled, RequestFlags mode);
Request(RequestMethod method, std::string_view url, std::string_view name, const Path &outFile, bool *cancelled, RequestFlags mode);
virtual ~Request() {}
void SetAccept(const char *mime) {
+1 -1
View File
@@ -209,7 +209,7 @@ void InputSink::AccountFill(int bytes) {
int err = socket_errno;
if (err == EWOULDBLOCK || err == EAGAIN)
return;
ERROR_LOG(Log::IO, "Error reading from socket: %d", err);
ERROR_LOG(Log::Net, "Error reading from socket: %d", err);
return;
}
+1 -1
View File
@@ -34,7 +34,7 @@ void UrlEncoder::AppendEscaped(const std::string &value)
void Url::Split() {
size_t colonSlashSlash = url_.find("://");
if (colonSlashSlash == std::string::npos) {
ERROR_LOG(Log::IO, "Invalid URL: %s", url_.c_str());
ERROR_LOG(Log::Net, "Invalid URL: %s", url_.c_str());
return;
}
+1 -1
View File
@@ -212,7 +212,7 @@ bool LoadDNSForGameID(std::string_view gameID, std::string_view jsonStr, InfraDN
json::JsonReader reader(jsonStr.data(), jsonStr.length());
if (!reader.ok() || !reader.root()) {
ERROR_LOG(Log::IO, "Error parsing DNS JSON");
ERROR_LOG(Log::sceNet, "Error parsing DNS JSON");
return false;
}
+3 -1
View File
@@ -2234,7 +2234,9 @@ void TextureCacheCommon::ApplyTexture(bool doBind, bool flatZ) {
if (entry->status & TexStatus::CLUT_GPU) {
_dbg_assert_(entry->status & TexStatus::CLUT8_INDEXED);
// Special process.
ApplyTextureDepalFramebufferCLUT(entry);
if (doBind) {
ApplyTextureDepalFramebufferCLUT(entry);
}
gstate_c.SetTextureSolidAlpha(false);
gstate_c.SetTextureIs3D(false);
gstate_c.SetTextureIsArray(false);
+4
View File
@@ -184,7 +184,11 @@ void VtxDec_Tu8_C5551_Ps16(const u8 *srcp, u8 *dstp, int numVerts, const UVScale
alpha &= col0;
// Combined 5551 -> 8888 RGBA. Nasty.
#if PPSSPP_ARCH(AMD64)
__m128i col = _mm_cvtsi64_si128(col0);
#else
__m128i col = _mm_set1_epi64x(col0);
#endif
__m128i r = _mm_slli_epi32(_mm_and_si128(col, rmask), 8 - 5);
__m128i g = _mm_slli_epi32(_mm_and_si128(col, gmask), 16 - 10);
__m128i b = _mm_slli_epi32(_mm_and_si128(col, bmask), 24 - 15);