HTTP server networking side, also, fix the releasing of channels in driver

This commit is contained in:
2022-05-17 15:00:20 -04:00
parent 1ac1ab9420
commit cd3064eb05
4 changed files with 80 additions and 56 deletions

View File

@@ -25,7 +25,8 @@ public:
LuaConsole console_;
ClientVector clients_;
PrintChanneler print_channeler_;
HttpClientChannelMap http_client_channels_;
HttpChannelMap http_client_channels_;
HttpChannelVec http_server_channels_;
int64_t admin_id_;
Gui gui_;
@@ -43,9 +44,12 @@ public:
// Print out admin ID for debugging purposes.
stdostream() << "Admin actor id = " << admin_id_ << std::endl;
// Enable listening on port 8085.
// Enable listening on port 8085 (client connections)
listen_port(8085);
// Enable listening on port 8080 (http server connections)
listen_port(8080);
// Set the console prompt.
get_stdio_channel()->set_prompt(console_.get_prompt());
}
@@ -181,14 +185,21 @@ public:
while (true) {
SharedChannel chan = new_incoming_channel();
if (chan == nullptr) break;
Client *client = new Client;
client->actor_id_ = master_->create_login_actor();
client->channel_ = std::move(chan);
client->sync_.reset(new World(util::WORLD_TYPE_S_SYNC));
client->sync_->create_login_actor();
clients_.emplace_back(client);
stdostream() << "New client: actor id=" << client->actor_id_ << std::endl;
send_diffs(clients_.back());
if (chan->port() == 8085) {
Client *client = new Client;
client->actor_id_ = master_->create_login_actor();
client->channel_ = std::move(chan);
client->sync_.reset(new World(util::WORLD_TYPE_S_SYNC));
client->sync_->create_login_actor();
clients_.emplace_back(client);
stdostream() << "New client: actor id=" << client->actor_id_ << std::endl;
send_diffs(clients_.back());
} else if (chan->port() == 8080) {
HttpChannel htchan;
htchan.channel_ = chan;
http_server_channels_.push_back(htchan);
stdostream() << "Http Server got new client " << chan->chid() << std::endl;
}
}
// Traverse all existing channels, process any communication.
@@ -206,7 +217,7 @@ public:
// Look for new outgoing HTTP client requests.
for (const auto &pair : master_->http_requests()) {
const HttpClientRequest &request = pair.second;
HttpClientChannel &channel = http_client_channels_[request.request_id()];
HttpChannel &channel = http_client_channels_[request.request_id()];
if (channel.channel_ == nullptr) {
channel.channel_ = new_outgoing_channel(request.target());
channel.parsed_bytes_ = 0;
@@ -217,7 +228,7 @@ public:
// Maintain existing outgoing HTTP client requests.
HttpParserVec http_responses;
for (auto &pair : http_client_channels_) {
HttpClientChannel &htchan = pair.second;
HttpChannel &htchan = pair.second;
Channel &channel = *htchan.channel_;
if (channel.closed() || (channel.in()->fill() > htchan.parsed_bytes_)) {
HttpParser response;
@@ -237,6 +248,22 @@ public:
http_client_channels_.erase(response.request_id());
}
master_->http_responses(http_responses);
// Maintain incoming HTTP server channels.
for (HttpChannel &htchan : http_server_channels_) {
SharedChannel &chan = htchan.channel_;
if (chan->in()->fill() > htchan.parsed_bytes_) {
HttpParser parser;
parser.parse_request(chan->in()->view(), chan->closed());
htchan.parsed_bytes_ = chan->in()->fill();
if (parser.complete()) {
StreamBuffer *sb = chan->out();
sb->ostream() << "HTTP/1.1 200 OK\n\n";
htchan.channel_ = nullptr;
}
}
}
util::remove_marked_items(http_server_channels_);
}
};