21 BILLION SLEEP STATEMENTS

This commit is contained in:
malloc 2018-11-29 17:07:15 -06:00
parent 9efe7e14ec
commit 45c471c143
11 changed files with 189 additions and 35 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ connectionStrings.config
cmake-build-*/
*.db-journal
scape.db
memory.db

View file

@ -240,15 +240,20 @@ Communication between the master server and clients will be done over a WebSocke
<td colspan="2">Iterated over <i>n</i> (0 &leq; <i>i</i> &leq; <i>n - 1</i>)</td>
</tr>
<tr>
<td class="center">2 + 2<i>i</i></td>
<td class="center">2 + 3<i>i</i></td>
<td>Server Id</td>
<td>Packed Unsigned Short</td>
</tr>
<tr>
<td class="center">3 + 2<i>i</i></td>
<td class="center">3 + 3<i>i</i></td>
<td>Server Name</td>
<td>Text</td>
<tr>
<td class="center">4 + 3<i>i</i></td>
<td>User Count</td>
<td>Packed Unsigned Short</td>
</tr>
</tr>
</table>
#### Client to Master [Encrypted]

View file

@ -3,7 +3,7 @@ Run Master = true
Master Host = localhost
Master Port = 8008
[Defaults]
[Pool Defaults]
; amount of threads to start with (never close)
Initial Count = 3
; starting limit of clients per thread
@ -30,11 +30,14 @@ Port = 8008
[Slave]
Port = 9000
Name = Test Server 1
Max Users = 100
[Slave]
Port = 9001
Name = Test Server 2
Max Users = 100
[Slave]
Port = 9002
Name = Test Server 3
Max Users = 100

View file

@ -13,7 +13,11 @@ bool sosc::db::init_databases(std::string* error) {
return true;
_ctx.ready = true;
#ifdef SOSC_DEBUG
sqlite3_open(SOSC_RESC("memory.db").c_str(), &_ctx.mem_db);
#else
sqlite3_open(":memory:", &_ctx.mem_db);
#endif
for(const auto& query : _mem_db_sql)
sqlite3_exec(_ctx.mem_db, query, nullptr, nullptr, nullptr);

View file

@ -16,7 +16,7 @@ void sosc::MasterClientPool::SetupQueries(db::Queries *queries) {
#define QRY_USER_MAIL_REG_CHECK 1
queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `USERS` "
"WHERE `USERNAME` = ?"
"WHERE `EMAIL` = ?"
));
#define QRY_USER_REGISTER 2
@ -171,7 +171,7 @@ bool sosc::MasterClient::ProcessRegistration(Packet &pck) {
query->BindText(pck[2], 3);
query->NonQuery();
this->sock.Send(Packet(kRegisterResponse, {"\1", 0x000}));
this->sock.Send(Packet(kRegisterResponse, {"\1", HTONUS(0x000)}));
return true;
}

View file

@ -1,5 +1,7 @@
#include "slave.hpp"
/** SLAVE -> CLIENT **/
sosc::SlaveClient::SlaveClient(const ScapeConnection& client) {
this->sock = client;
}
@ -13,3 +15,6 @@ bool sosc::SlaveClient::Close(const Packet& message) {
this->sock.Send(message);
return this->Close();
}
/** SLAVE -> MASTER **/

View file

@ -1,6 +1,7 @@
#ifndef SOSC_HOST_SLAVE_H
#define SOSC_HOST_SLAVE_H
#include "sock/intrasock.hpp"
#include "sock/scapesock.hpp"
#include "sock/pool.hpp"
#include "ctx/slave.hpp"
@ -32,6 +33,20 @@ protected:
return true;
}
};
/** SLAVE -> MASTER **/
class SlaveMaster {
public:
SlaveMaster();
bool Close();
bool Close(const Packet& message);
private:
IntraClient sock;
};
}
#endif

View file

@ -74,7 +74,7 @@ int main(int argc, char **argv) {
ini::Field("master host", ini::Field::STRING),
ini::Field("master port", ini::Field::UINT32),
}),
ini::Rule("defaults", true, false, {
ini::Rule("pool defaults", true, false, {
ini::Field("initial count", ini::Field::UINT32),
ini::Field("initial size", ini::Field::UINT32),
ini::Field("size growth", ini::Field::UINT32),
@ -93,6 +93,7 @@ int main(int argc, char **argv) {
ini::Rule("slave", false, true, {
ini::Field("port", ini::Field::UINT32),
ini::Field("name", ini::Field::STRING),
ini::Field("max users", ini::Field::STRING),
})
});
} catch(const std::exception& e) {
@ -101,7 +102,7 @@ int main(int argc, char **argv) {
}
poolinfo_t info;
configure_poolinfo(&_ctx.default_info, (*config)["defaults"][0]);
configure_poolinfo(&_ctx.default_info, (*config)["pool defaults"][0]);
if((*config)["general"]["run master"]) {
if(!config->HasSection("master to client") ||
@ -152,7 +153,7 @@ int main(int argc, char **argv) {
}
}
std::cout << "Server threads started. Type STOP to cancel." << std::endl;
std::cout << "Server threads started. Type STOP to quit." << std::endl;
std::string input;
while(true) {
@ -181,7 +182,7 @@ bool master_intra_start(uint16_t port, const sosc::poolinfo_t& info) {
_ctx.master_intra->thread = std::thread([&] {
sosc::IntraClient client;
while (_ctx.master_intra->server.Accept(&client))
while(_ctx.master_intra->server.Accept(&client))
_ctx.master_intra->pool.AddClient(new sosc::MasterIntra(client));
});
@ -213,7 +214,7 @@ bool slave_start(uint16_t port, const sosc::poolinfo_t& info, slave_ctx* ctx) {
ctx->thread = std::thread([&] {
sosc::ScapeConnection client;
while (ctx->server.Accept(&client))
while(ctx->server.Accept(&client))
ctx->pool.AddClient(new sosc::SlaveClient(client));
});

View file

@ -46,8 +46,29 @@
<input type="button" value="Register" onclick="attempt_register();" />
<input type="button" value="Cancel" onclick="show_section('login');" />
</div>
<div id="servers" class="hidden section">
<h1>Server List</h1>
<label>
<input type="button" value="Refresh" id="servers-refresh"
onclick="refresh_list();" />
&nbsp;&nbsp;
<input type="button" value="Log Out" id="servers-logout"
onclick="log_out();" />
</label>
<table id="servers-list">
<thead>
<tr>
<th>&nbsp;</th>
<th>ID</th>
<th>Name</th>
<th>Users</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div id="client" class="hidden section">
<iframe id="client_iframe"></iframe>
<iframe id="client-iframe"></iframe>
</div>
</body>
</html>

View file

@ -1,5 +1,6 @@
let unloading = false;
let sections = {};
let key = null;
let ws;
const MAHOU = [0xB0, 0x0B];
@ -28,21 +29,9 @@ function attempt_login() {
x => ["submit", "button", "text", "password"].indexOf(x.type) !== -1
);
for_each(lock_fields, x => x.disabled = true);
ws.send(pack(
kClientToMaster.LoginRequest, [
document.getElementById("login-user").value,
document.getElementById("login-pwd").value
]
));
receive_callbacks[kMasterToClient.LoginResponse] = pck => {
console.log(pck.regions);
if(pck.regions[0][0] === 0x0) {
console.log(pck.regions[1].unpackUint16());
let error_text = "Login failed";
let error_text = "Login failed.";
switch(pck.regions[1].unpackUint16()) {
case 0x100:
error_text = "Too many attempts. Try again later.";
@ -58,11 +47,20 @@ function attempt_login() {
error.innerHTML = error_text;
error.classList.remove("hidden");
} else {
//show_section()
key = pck.regions[1];
show_section("servers");
}
for_each(lock_fields, x => x.disabled = false);
};
for_each(lock_fields, x => x.disabled = true);
ws.send(pack(
kClientToMaster.LoginRequest, [
document.getElementById("login-user").value,
document.getElementById("login-pwd").value
]
));
}
function attempt_register() {
@ -82,19 +80,100 @@ function attempt_register() {
});
if(fields["pwd"] !== fields["conf-pwd"]) {
error.innerHTML = "";
error.innerHTML = "Confirmation password does not match password.";
error.classList.remove("hidden");
return;
}
receive_callbacks[kMasterToClient.RegisterResponse] = pck => {
if(pck.regions[0][0] === 0x0) {
let error_text = "Registration failed.";
switch(pck.regions[1].unpackUint16()) {
case 0x100:
error_text = "Username is taken.";
break;
case 0x101:
error_text = "Username is illegal.";
break;
case 0x110:
error_text = "Email is taken.";
break;
case 0x111:
error_text = "Email isn't properly formed.";
break;
case 0x120:
error_text = "Password is too weak, just like you.";
break;
}
error.innerHTML = error_text;
error.classList.remove("hidden");
} else {
alert("Registration was successful.");
show_section("servers");
refresh_list();
}
for_each(lock_fields, x => x.disabled = false);
};
for_each(lock_fields, x => x.disabled = true);
ws.send(pack(
kClientToMaster.LoginRequest, [
document.getElementById("login-user").value,
document.getElementById("login-pwd").value
kClientToMaster.RegisterRequest, [
fields["user"],
fields["pwd"],
fields["email"]
]
));
}
function refresh_list() {
if(key === null)
return;
let table = document.getElementById("servers-list");
let button = document.getElementById("servers-refresh");
button.disabled = true;
while(table.rows.length > 1)
table.deleteRow(-1);
receive_callbacks[kMasterToClient.ServerList] = pck => {
let count = pck[0].unpackUint16();
for(let i = 0; i < count; ++i) {
let id = pck[2 + 3*i].unpackUint16();
let row = table.insertRow();
let cell = row.insertCell();
let link = document.createElement("a");
link.setAttribute("href", "javascript: void(0);");
link.text = "Join";
link.tag = id;
link.onclick = e => join_server(id);
cell.appendChild(cell);
cell = row.insertCell();
cell.innerText = id;
cell = row.insertCell();
cell.innerText = pck[3 + 3*i];
cell = row.insertCell();
cell.innerText = pck[4 + 3*i];
}
button.disabled = false;
};
ws.send(pack(kClientToMaster.ServerListRequest, []));
}
function log_out() {
}
function join_server(id) {
}
function show_section(id) {
clear_inputs();
clear_errors();

View file

@ -1,11 +1,21 @@
:root {
/**/
--foreground-color: #000;
--background-color: #fff;
/**/
/*
--foreground-color: #fff;
--background-color: #000;
/**/
}
body {
padding-top: 36px;
font-family: monospace;
text-align: center;
/*background: #000;
color: #fff;*/
background: #fff;
color: #000;
background: var(--background-color);
color: var(--foreground-color);
}
.hidden {
@ -28,3 +38,13 @@ body {
font-size: 1.1em;
max-width: 250px;
}
.section > table {
font-size: 1.1em;
margin: 15px auto;
border-collapse: collapse;
}
.section > table td, .section > table th {
border: 1px solid var(--foreground-color);
}