bsnes/nall/http/server.hpp

137 lines
3.4 KiB
C++
Raw Normal View History

Update to v094r09 release. byuu says: This will easily be the biggest diff in the history of higan. And not in a good way. * target-higan and target-loki have been blown away completely * nall and ruby massively updated * phoenix replaced with hiro (pretty near a total rewrite) * target-higan restarted using hiro (just a window for now) * all emulation cores updated to compile again * installation changed to not require root privileges (installs locally) For the foreseeable future (maybe even permanently?), the new higan UI will only build under Linux/BSD with GTK+ 2.20+. Probably the most likely route for Windows/OS X will be to try and figure out how to build hiro/GTK on those platforms, as awful as that would be. The other alternative would be to produce new UIs for those platforms ... which would actually be a good opportunity to make something much more user friendly. Being that I just started on this a few hours ago, that means that for at least a few weeks, don't expect to be able to actually play any games. Right now, you can pretty much just compile the binary and that's it. It's quite possible that some nall changes didn't produce compilation errors, but will produce runtime errors. So until the UI can actually load games, we won't know if anything is broken. But we should mostly be okay. It was mostly just trim<1> -> trim changes, moving to Hash::SHA256 (much cleaner), and patching some reckless memory copy functions enough to compile. Progress isn't going to be like it was before: I'm now dividing my time much thinner between studying and other hobbies. My aim this time is not to produce a binary for everyone to play games on. Rather, it's to keep the emulator alive. I want to be able to apply critical patches again. And I would also like the base of the emulator to live on, for use in other emulator frontends that utilize higan.
2015-02-26 21:10:46 +11:00
#ifndef NALL_HTTP_SERVER_HPP
#define NALL_HTTP_SERVER_HPP
#include <poll.h>
#include <atomic>
#include <nall/service.hpp>
#include <nall/http/role.hpp>
namespace nall {
struct httpServer : httpRole, service {
inline auto open(unsigned port = 8080, const string& serviceName = "", const string& command = "") -> bool;
inline auto main(const function<httpResponse (httpRequest&)>& function = {}) -> void;
inline auto scan() -> string;
inline auto close() -> void;
~httpServer() { close(); }
private:
signed fd = -1;
function<httpResponse (httpRequest&)> callback;
struct sockaddr_in addrin = {0};
std::atomic<signed> connections{0};
};
auto httpServer::open(unsigned port, const string& serviceName, const string& command) -> bool {
if(serviceName) {
if(!service::command(serviceName, command)) return false;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0) return false;
{
#if defined(SO_RCVTIMEO)
if(settings.timeoutReceive) {
struct timeval rcvtimeo;
rcvtimeo.tv_sec = settings.timeoutReceive / 1000;
rcvtimeo.tv_usec = settings.timeoutReceive % 1000 * 1000;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &rcvtimeo, sizeof(struct timeval));
}
#endif
#if defined(SO_SNDTIMEO)
if(settings.timeoutSend) {
struct timeval sndtimeo;
sndtimeo.tv_sec = settings.timeoutSend / 1000;
sndtimeo.tv_usec = settings.timeoutSend % 1000 * 1000;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &sndtimeo, sizeof(struct timeval));
}
#endif
#if defined(SO_NOSIGPIPE) //BSD, OSX
signed nosigpipe = 1;
setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(signed));
#endif
#if defined(SO_REUSEADDR) //BSD, Linux, OSX
signed reuseaddr = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(signed));
#endif
#if defined(SO_REUSEPORT) //BSD, OSX
signed reuseport = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(signed));
#endif
}
addrin.sin_family = AF_INET;
addrin.sin_addr.s_addr = htonl(INADDR_ANY);
addrin.sin_port = htons(port);
signed result = bind(fd, (struct sockaddr*)&addrin, sizeof(addrin));
if(result < 0) return close(), false;
result = listen(fd, SOMAXCONN); //system-wide limit (per port)
if(result < 0) return close(), false;
return true;
}
auto httpServer::main(const function<httpResponse (httpRequest&)>& function) -> void {
callback = function;
}
auto httpServer::scan() -> string {
if(auto command = service::receive()) return command;
if(connections >= settings.connectionLimit) return "busy";
struct pollfd query = {0};
query.fd = fd;
query.events = POLLIN;
poll(&query, 1, 0);
if(query.fd == fd && query.revents & POLLIN) {
++connections;
thread::create([&](uintptr_t) {
thread::detach();
signed clientfd = -1;
struct sockaddr_in settings = {0};
socklen_t socklen = sizeof(sockaddr_in);
clientfd = accept(fd, (struct sockaddr*)&settings, &socklen);
if(clientfd < 0) return;
httpRequest request;
request._ip = ntohl(settings.sin_addr.s_addr);
if(download(clientfd, request) && callback) {
auto response = callback(request);
upload(clientfd, response);
} else {
upload(clientfd, httpResponse()); //"501 Not Implemented"
}
::close(clientfd);
--connections;
}, 0, settings.threadStackSize);
}
return "ok";
}
auto httpServer::close() -> void {
if(fd) {
::close(fd);
fd = -1;
}
}
}
#endif