Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ scons target=template_release
| [int][class_int] | `open(path: String)` | Opens a connection to a socket at `path` and returns an error code |
| [bool][class_bool] | `is_open()` | Checks if the stream peer is connected |
| [String][class_string] | `get_path()` | Gets the current connected socket path |
| [String][class_string] | `read()` | Reads a string from the current connected socket |
| [Error][error_enum] | `write()` | Writes a string to the current connected socket |
| void | `close()` | Closes the socket connection |

[class_int]: https://docs.godotengine.org/en/stable/classes/class_int.html
[class_bool]: https://docs.godotengine.org/en/stable/classes/class_bool.html
[class_string]: https://docs.godotengine.org/en/stable/classes/class_string.html

[error_enum]: https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-error

39 changes: 38 additions & 1 deletion src/stream_peer_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@

using namespace godot;

String StreamPeerUnix::read() {
String result;
int32_t bytes_to_read = get_available_bytes() - total_bytes;
uint8_t* buf = new uint8_t[bytes_to_read];
int32_t received;

Error error = _get_data(buf,bytes_to_read, &received);

if (error == Error::OK) {
std::string receivedString(reinterpret_cast<char*>(buf), received);
result = receivedString.c_str();
}
else {
result = "Error reading socket";
}

total_bytes = get_available_bytes();
delete[] buf;
return result;
}

Error StreamPeerUnix::_get_data(uint8_t *p_buffer, int32_t p_bytes, int32_t *r_received) {
ERR_FAIL_COND_V(not is_open(), Error::ERR_UNCONFIGURED);
ERR_FAIL_COND_V(p_bytes < 0, Error::ERR_INVALID_PARAMETER);
Expand Down Expand Up @@ -49,6 +70,19 @@ Error StreamPeerUnix::_get_partial_data(uint8_t *p_buffer, int p_bytes, int *r_r
return error;
}

Error StreamPeerUnix::write(String request) {
const char *request_data = request.utf8().get_data();
int32_t bytes_to_send = static_cast<int32_t>(strlen(request_data));
uint8_t* uint8_data = new uint8_t[bytes_to_send];

memcpy(uint8_data, request_data, bytes_to_send);
int32_t sent_bytes;

Error error = _put_data(uint8_data, bytes_to_send, &sent_bytes);

return error;
}

Error StreamPeerUnix::_put_data(const uint8_t *p_data, int32_t p_bytes, int32_t *r_sent) {
*r_sent = 0;
ERR_FAIL_COND_V(not is_open(), Error::ERR_UNCONFIGURED);
Expand Down Expand Up @@ -144,11 +178,14 @@ StreamPeerUnix::~StreamPeerUnix() {
}

void StreamPeerUnix::_bind_methods() {
ClassDB::bind_method(D_METHOD("open"), &StreamPeerUnix::open);
ClassDB::bind_method(D_METHOD("open", "path"), &StreamPeerUnix::open);
ClassDB::bind_method(D_METHOD("get_path"), &StreamPeerUnix::get_path);
ClassDB::bind_method(D_METHOD("is_open"), &StreamPeerUnix::is_open);
ClassDB::bind_method(D_METHOD("close"), &StreamPeerUnix::close);

ClassDB::bind_method(D_METHOD("write", "request"), &StreamPeerUnix::write);
ClassDB::bind_method(D_METHOD("read"), &StreamPeerUnix::read);

ClassDB::bind_method(D_METHOD("set_blocking_mode"),
&StreamPeerUnix::set_blocking_mode);
ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"),
Expand Down
3 changes: 3 additions & 0 deletions src/stream_peer_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StreamPeerUnix : public godot::StreamPeerExtension {
int socketfd = -1;
godot::String path;
struct sockaddr_un server_address;
int32_t total_bytes = 0;

static void _bind_methods();

Expand All @@ -28,6 +29,8 @@ class StreamPeerUnix : public godot::StreamPeerExtension {
int open(const godot::String path);
godot::String get_path() const;
bool is_open() const;
godot::Error write(godot::String request);
godot::String read();
void close();

void set_blocking_mode(const bool value);
Expand Down