This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6161aa
commit 9b6ede3
Showing
7 changed files
with
281 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include "datum_socket.h" | ||
#include "../core/core.h" | ||
#include "../core/proc_management.h" | ||
|
||
std::unordered_map<unsigned int, std::unique_ptr<DatumSocket>> sockets; | ||
unsigned int recv_sleep_opcode = -1; | ||
|
||
DatumSocket::DatumSocket() | ||
{ | ||
} | ||
|
||
DatumSocket::DatumSocket(const DatumSocket& other) | ||
{ | ||
} | ||
|
||
DatumSocket::~DatumSocket() | ||
{ | ||
close(); | ||
} | ||
|
||
bool DatumSocket::connect(std::string addr, std::string port) | ||
{ | ||
stream = TcpStream(); | ||
bool connected = stream.connect(port.c_str(), addr.c_str()); | ||
if (connected) | ||
{ | ||
std::thread(&DatumSocket::recv_loop, this).detach(); | ||
} | ||
return connected; | ||
} | ||
|
||
bool DatumSocket::send(std::string data) | ||
{ | ||
return stream.send(data); | ||
} | ||
|
||
std::string DatumSocket::recv(int len) | ||
{ | ||
std::lock_guard<std::mutex> lk(buffer_lock); | ||
size_t nom = min(len, buffer.size()); | ||
std::string sub = buffer.substr(0, nom); | ||
buffer.erase(buffer.begin(), buffer.begin() + nom); | ||
return sub; | ||
} | ||
|
||
void DatumSocket::close() | ||
{ | ||
stream.close(); | ||
open = false; | ||
} | ||
|
||
void DatumSocket::recv_loop() | ||
{ | ||
while (open) | ||
{ | ||
std::string data = stream.recv(); | ||
if (data.empty()) | ||
{ | ||
close(); | ||
return; | ||
} | ||
buffer_lock.lock(); | ||
buffer += data; | ||
buffer_lock.unlock(); | ||
if (data_awaiter) | ||
{ | ||
data_awaiter->time_to_resume = 0; | ||
data_awaiter = nullptr; | ||
} | ||
} | ||
} | ||
|
||
trvh register_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
//Core::Alert("register"); | ||
sockets[src.value] = std::make_unique<DatumSocket>(); | ||
return Value::Null(); | ||
} | ||
|
||
trvh connect_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
//Core::Alert("connect"); | ||
return sockets[src.value]->connect(args[0], std::to_string((int)args[1].valuef)) ? Value::True() : Value::False(); | ||
} | ||
|
||
trvh send_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
//Core::Alert("send"); | ||
return sockets[src.value]->send(args[0]) ? Value::True() : Value::False(); | ||
} | ||
|
||
trvh check_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
//Core::Alert("check"); | ||
return sockets[src.value]->has_data() ? Value::True() : Value::False(); | ||
} | ||
|
||
trvh retrieve_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
//Core::Alert("retrieve"); | ||
return Value(sockets[src.value]->recv(1024)); | ||
} | ||
|
||
trvh deregister_socket(unsigned int args_len, Value* args, Value src) | ||
{ | ||
if (sockets.find(src.value) == sockets.end()) | ||
{ | ||
return Value::Null(); | ||
} | ||
sockets[src.value].reset(); | ||
sockets.erase(src.value); | ||
return Value::Null(); | ||
} | ||
|
||
void recv_suspend(ExecutionContext* ctx) | ||
{ | ||
ctx->current_opcode++; | ||
SuspendedProc* proc = Suspend(ctx, 0); | ||
proc->time_to_resume = 0x7FFFFF; | ||
StartTiming(proc); | ||
int datum_id = ctx->constants->src.value; | ||
sockets[datum_id]->set_awaiter(proc); | ||
ctx->current_opcode--; | ||
} | ||
|
||
bool enable_sockets() | ||
{ | ||
Core::get_proc("/datum/socket/proc/__register_socket").hook(register_socket); | ||
Core::get_proc("/datum/socket/proc/__check_has_data").hook(check_socket); | ||
Core::get_proc("/datum/socket/proc/__retrieve_data").hook(retrieve_socket); | ||
Core::get_proc("/datum/socket/proc/connect").hook(connect_socket); | ||
Core::get_proc("/datum/socket/proc/send").hook(send_socket); | ||
Core::get_proc("/datum/socket/proc/__deregister_socket").hook(deregister_socket); | ||
recv_sleep_opcode = Core::register_opcode("RECV_SLEEP", recv_suspend); | ||
Core::get_proc("/datum/socket/proc/__wait_for_data").set_bytecode(new std::vector<std::uint32_t>({ recv_sleep_opcode, 0, 0, 0 })); | ||
return true; | ||
} | ||
|
||
extern "C" __declspec(dllexport) const char* init_sockets(int a, const char** b) | ||
{ | ||
enable_sockets(); | ||
return "ok"; | ||
} |
Oops, something went wrong.