Open ghost opened 8 years ago
This is awesome. Thanks for sharing the work you did on this. I look forward to firing up a Windows VM and trying this out on my copy of Visual Studio 2013. If any other Windows experts can test this out and report on the results, it would be greatly appreciated.
Edit: My copy of Visual Studio Community 2013 is complaining about licensing, and after half an hour of research and experimentation, I haven't yet gotten it to work. I feel quite lost in the Microsoft world. I better step back from this and let someone who knows what they are doing test these changes with Visual Studio. All we need is an independent review and confirmation that the changes work and then we can merge the changes into the repository.
Hi @jakeally, I got an email notice saying you had left a comment in this thread, and I tried to respond by email, but it bounced. So, if I've dropped the ball on one of your comments, please open a new issue or add a new comment here. Cheers.
I'm working on making the Webserver work on Windows. I like working inside the Visual C++ 2015 environment. Here's some initial code if you can use it. Still needs more testing.
/ webserver.cpp -- embedded web server for the GUI for the neural2d program. David R. Miller, 2014, 2015 For more information, see neural2d.h and https://github.com/davidrmiller/neural2d . /
// C Standard includes
include
include
include
// C++ Standard includes
include
include
include
include
include
include
// POSIX Includes
ifndef WIN32
include // POSIX, for read(), write(), close()
include <sys/types.h> // For setsockopt() and SO_REUSEADDR
endif
include "webserver.h"
int readSocket(int fd,void _buf,sizet count) { return recv(fd, (char) buf, count, 0); }
int writeSocket(int fd,const char *buf,size_t count) { return send(fd, buf,count,0); }
void closeSocket(SOCKET socketFd) {
ifdef WIN32
else
endif
}
void closeIt(SOCKET socketFd) {
ifdef WIN32
else
endif
}
namespace NNet {
// *** Web server ***
WebServer::WebServer(void) {
ifdef WIN32
endif
}
WebServer::~WebServer(void) { stopServer(); }
void WebServer::stopServer(void) { if (socketFd != -1) { //std::cout << "Closing webserver socket..." << std::endl; closeIt(socketFd); socketFd = -1; } }
// The web server runs in a separate thread. It receives a GET from the browser, // extracts the message (e.g., "eta=0.1") and appends it to a shared message // queue. Periodically, the Net object will pop messages and respond to them // by constructing a parameter block (a string) and calling a function to // send the http response. // void WebServer::initializeHttpResponse(void) { // Keep appending lines to firstPart until we get the line with the sentinel: std::ifstream httpTemplate("http-response-template.txt"); if (!httpTemplate.is_open()) { firstPart = "Cannot open file \"http-response-template.txt\"; web server is disabled.\r\n"; return; }
}
// This sends a 404 Not Found reply // void WebServer::replyToUnknownRequest(int httpConnectionFd) { std::string response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
/ shutdown(httpConnectionFd, SHUT_RDWR); close(httpConnectionFd); / }
// Look for "POST /" or "GET /?" followed by a message or POST. // If this is the first GET or POST we receive, we'll treat it as if it were // "GET / " of the root document. // void WebServer::extractAndQueueMessage(std::string s, int httpConnectionFd, MessageQueue &messages) { struct Message_t msg; size_t pos;
}
void WebServer::start(int portNumber, MessageQueue &messages) { portNumber = portNumber; std::thread webThread(&WebServer::webServerThread, this, portNumber, std::ref(messages)); webThread.detach(); }
// The argument parameterBlock needs to be passed by value because it comes from // a different thread: // void WebServer::sendHttpResponse(std::string parameterBlock, int httpResponseFileDes) { std::string response = firstPart + parameterBlock + secondPart; const char *buf = response.c_str();
/ shutdown(httpResponseFileDes, SHUT_RDWR); close(httpResponseFileDes); / }
void WebServer::webServerThread(int portNumber, MessageQueue &messages) { static const struct sockaddr_in zero_sockaddr_in = { 0 }; struct sockaddr_in stSockAddr = zero_sockaddr_in; char buff[2048];
ifndef WIN32
else
endif
}
} /* end namespace NNet */