From 9453c6fd1a288af42864e2111645b4de856f9ff1 Mon Sep 17 00:00:00 2001 From: Barshon Clinton Sarkar Date: Thu, 25 Apr 2024 18:25:07 +0800 Subject: [PATCH 1/3] added read and write functions for godot --- src/stream_peer_unix.cpp | 39 ++++++++++++++++++++++++++++++++++++++- src/stream_peer_unix.h | 3 +++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/stream_peer_unix.cpp b/src/stream_peer_unix.cpp index 8f2a566..d765861 100644 --- a/src/stream_peer_unix.cpp +++ b/src/stream_peer_unix.cpp @@ -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(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); @@ -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(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); @@ -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"), diff --git a/src/stream_peer_unix.h b/src/stream_peer_unix.h index f4468f0..4e352d0 100644 --- a/src/stream_peer_unix.h +++ b/src/stream_peer_unix.h @@ -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(); @@ -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); From 6cf51e6aea23e932a104dfe42b14000a91eaa0a5 Mon Sep 17 00:00:00 2001 From: Barshon Clinton Sarkar Date: Thu, 25 Apr 2024 18:35:17 +0800 Subject: [PATCH 2/3] updated readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c6cf32..0ad23a1 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,11 @@ scons target=template_release | [bool][class_bool] | `is_open()` | Checks if the stream peer is connected | | [String][class_string] | `get_path()` | Gets the current connected socket path | | void | `close()` | Closes the socket connection | +| [String][class_string] | `read()` | Reads a string from the current connected socket | +| [Error][error_enum] | `write()` | Writes a string to the current connected socket | [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 From 337c446625273d44f1afd8f6e33c0ee0230f6f15 Mon Sep 17 00:00:00 2001 From: Barshon Clinton Sarkar Date: Thu, 25 Apr 2024 18:36:43 +0800 Subject: [PATCH 3/3] changed order in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ad23a1..8150e61 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ 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 | -| void | `close()` | Closes the socket connection | | [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