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
117 changes: 117 additions & 0 deletions core/io/stream_peer_stdio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**************************************************************************/
/* stream_peer_stdio.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "stream_peer_stdio.h"

#include <fcntl.h>
#include <cerrno>
#include <cstdio>

#ifdef WINDOWS_ENABLED
#include <io.h>
#define READ_FUNCTION _read
#define WRITE_FUNCTION _write
#else
#include <unistd.h>
#define READ_FUNCTION read
#define WRITE_FUNCTION write
#endif

StreamPeerStdio::StreamPeerStdio() {
// Set stdin to non-blocking mode and binary mode
#ifdef WINDOWS_ENABLED
stdin_fileno = _fileno(stdin);
stdout_fileno = _fileno(stdout);

_setmode(stdin_fileno, _O_BINARY);
_setmode(stdout_fileno, _O_BINARY);
#else
stdin_fileno = STDIN_FILENO;
stdout_fileno = STDOUT_FILENO;

int flags = fcntl(stdin_fileno, F_GETFL, 0);
fcntl(stdin_fileno, F_SETFL, flags | O_NONBLOCK);
#endif
}

StreamPeerStdio::~StreamPeerStdio() {
// TODO: see if make sense to revert the non-blocking
}

Error StreamPeerStdio::put_data(const uint8_t *p_data, int p_bytes) {
int sent;
return put_partial_data(p_data, p_bytes, sent);
}

Error StreamPeerStdio::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
int sent = WRITE_FUNCTION(stdout_fileno, p_data, p_bytes);
if (sent < 0) {
r_sent = 0;
return FAILED;
}

r_sent = sent;
fflush(stdout);

return OK;
}

Error StreamPeerStdio::get_data(uint8_t *p_buffer, int p_bytes) {
int received;
return get_partial_data(p_buffer, p_bytes, received);
}

GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wlogical-op") // Silence a false positive. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69602

Error StreamPeerStdio::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
int received = READ_FUNCTION(stdin_fileno, p_buffer, p_bytes);
if (received < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
r_received = 0;
return ERR_BUSY;
}
r_received = 0;
return FAILED;
} else if (received == 0) { // EOF
r_received = 0;
return FAILED;
}

r_received = received;
return OK;
}

GODOT_GCC_WARNING_POP

int StreamPeerStdio::get_available_bytes() const {
// Return 1 to indicate data might be available
// The actual read will handle EAGAIN/EWOULDBLOCK
return 1;
}
51 changes: 51 additions & 0 deletions core/io/stream_peer_stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**************************************************************************/
/* stream_peer_stdio.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/io/stream_peer.h"

class StreamPeerStdio : public StreamPeer {
GDCLASS(StreamPeerStdio, StreamPeer);

private:
int stdin_fileno = 0;
int stdout_fileno = 1;

public:
Error put_data(const uint8_t *p_data, int p_bytes) override;
Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) override;
Error get_data(uint8_t *p_buffer, int p_bytes) override;
Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) override;
int get_available_bytes() const override;

StreamPeerStdio();
virtual ~StreamPeerStdio();
};
8 changes: 8 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ void Main::print_help(const char *p_binary) {
print_help_option("--dap-port <port>", "Use the specified port for the GDScript Debug Adapter Protocol. Recommended port range [1024, 49151].\n", CLI_OPTION_AVAILABILITY_EDITOR);
#if defined(MODULE_GDSCRIPT_ENABLED) && !defined(GDSCRIPT_NO_LSP)
print_help_option("--lsp-port <port>", "Use the specified port for the GDScript Language Server Protocol. Recommended port range [1024, 49151].\n", CLI_OPTION_AVAILABILITY_EDITOR);
print_help_option("--lsp", "Start the GDScript Language Server in headless mode (stdio). Useful for integration with external editors.\n", CLI_OPTION_AVAILABILITY_EDITOR);
#endif // MODULE_GDSCRIPT_ENABLED && !GDSCRIPT_NO_LSP
#endif
print_help_option("--quit", "Quit after the first iteration.\n");
Expand Down Expand Up @@ -1881,6 +1882,13 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
OS::get_singleton()->print("Missing <port> argument for --lsp-port <port>.\n");
goto error;
}
} else if (arg == "--lsp") {
editor = true;
GDScriptLanguageServer::use_stdio = true;

// `--lsp` implies `--headless` to avoid spawning an unnecessary window
audio_driver = NULL_AUDIO_DRIVER;
display_driver = NULL_DISPLAY_DRIVER;
#endif // TOOLS_ENABLED && MODULE_GDSCRIPT_ENABLED && !GDSCRIPT_NO_LSP
#if defined(TOOLS_ENABLED)
} else if (arg == "--dap-port") {
Expand Down
78 changes: 49 additions & 29 deletions modules/gdscript/language_server/gdscript_language_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "gdscript_language_protocol.h"

#include "core/config/project_settings.h"
#include "core/io/stream_peer_stdio.h"
#include "editor/doc/doc_tools.h"
#include "editor/doc/editor_help.h"
#include "editor/editor_log.h"
Expand All @@ -49,10 +50,10 @@ Error GDScriptLanguageProtocol::LSPeer::handle_data() {
ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Response header too big");
}
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
if (err != OK) {
if (err == ERR_BUSY || read != 1) {
return ERR_BUSY; // Busy, wait until next poll
} else if (err != OK) {
return FAILED;
} else if (read != 1) { // Busy, wait until next poll
return ERR_BUSY;
}
char *r = (char *)req_buf;
int l = req_pos;
Expand All @@ -77,10 +78,10 @@ Error GDScriptLanguageProtocol::LSPeer::handle_data() {
ERR_FAIL_COND_V_MSG(req_pos >= LSP_MAX_BUFFER_SIZE, ERR_OUT_OF_MEMORY, "Response content too big");
}
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
if (err != OK) {
if (err == ERR_BUSY || read != 1) {
return ERR_BUSY; // Busy, wait until next poll
} else if (err != OK) {
return FAILED;
} else if (read != 1) {
return ERR_BUSY;
}
req_pos++;
}
Expand Down Expand Up @@ -259,35 +260,38 @@ void GDScriptLanguageProtocol::poll(int p_limit_usec) {
HashMap<int, Ref<LSPeer>>::Iterator E = clients.begin();
while (E != clients.end()) {
Ref<LSPeer> peer = E->value;
peer->connection->poll();
StreamPeerTCP::Status status = peer->connection->get_status();
if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
on_client_disconnected(E->key);
E = clients.begin();
continue;
} else {
Error err = OK;
while (peer->connection->get_available_bytes() > 0) {
latest_client_id = E->key;
err = peer->handle_data();
if (err != OK || OS::get_singleton()->get_ticks_usec() >= target_ticks) {
break;
}
}

if (err != OK && err != ERR_BUSY) {
Ref<StreamPeerTCP> tcp_peer = peer->connection;
if (tcp_peer.is_valid()) {
tcp_peer->poll();
StreamPeerTCP::Status status = tcp_peer->get_status();
if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
on_client_disconnected(E->key);
E = clients.begin();
continue;
}
}

err = peer->send_data();
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(E->key);
E = clients.begin();
continue;
Error err = OK;
while (peer->connection->get_available_bytes() > 0) {
latest_client_id = E->key;
err = peer->handle_data();
if (err != OK || OS::get_singleton()->get_ticks_usec() >= target_ticks) {
break;
}
}

if (err != OK && err != ERR_BUSY) {
on_client_disconnected(E->key);
E = clients.begin();
continue;
}

err = peer->send_data();
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(E->key);
E = clients.begin();
continue;
}
++E;
}
}
Expand All @@ -296,10 +300,26 @@ Error GDScriptLanguageProtocol::start(int p_port, const IPAddress &p_bind_ip) {
return server->listen(p_port, p_bind_ip);
}

Error GDScriptLanguageProtocol::start_stdio() {
Ref<LSPeer> peer = memnew(LSPeer);
Ref<StreamPeerStdio> stdio_stream;
stdio_stream.instantiate();
peer->connection = stdio_stream;

clients.insert(next_client_id, peer);
next_client_id++;

OS::get_singleton()->print("[LSP] Started in stdio mode\n");
return OK;
}

void GDScriptLanguageProtocol::stop() {
for (const KeyValue<int, Ref<LSPeer>> &E : clients) {
Ref<LSPeer> peer = clients.get(E.key);
peer->connection->disconnect_from_host();
Ref<StreamPeerTCP> tcp_peer = peer->connection;
if (tcp_peer.is_valid()) {
tcp_peer->disconnect_from_host();
}
}

server->stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "gdscript_text_document.h"
#include "gdscript_workspace.h"

#include "core/io/stream_peer_tcp.h"
#include "core/io/stream_peer.h"
#include "core/io/tcp_server.h"

#include "modules/jsonrpc/jsonrpc.h"
Expand All @@ -46,12 +46,11 @@ class GDScriptLanguageProtocol : public JSONRPC {

private:
struct LSPeer : RefCounted {
Ref<StreamPeerTCP> connection;
Ref<StreamPeer> connection;

uint8_t req_buf[LSP_MAX_BUFFER_SIZE];
int req_pos = 0;
bool has_header = false;
bool has_content = false;
int content_length = 0;
Vector<CharString> res_queue;
int res_sent = 0;
Expand Down Expand Up @@ -99,6 +98,7 @@ class GDScriptLanguageProtocol : public JSONRPC {

void poll(int p_limit_usec);
Error start(int p_port, const IPAddress &p_bind_ip);
Error start_stdio();
void stop();

void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
Expand Down
28 changes: 23 additions & 5 deletions modules/gdscript/language_server/gdscript_language_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "editor/settings/editor_settings.h"

int GDScriptLanguageServer::port_override = -1;
bool GDScriptLanguageServer::use_stdio = false;

GDScriptLanguageServer::GDScriptLanguageServer() {
// TODO: Move to editor_settings.cpp
Expand Down Expand Up @@ -94,12 +95,25 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) {
}

void GDScriptLanguageServer::start() {
host = String(_EDITOR_GET("network/language_server/remote_host"));
port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
poll_limit_usec = (int)_EDITOR_GET("network/language_server/poll_limit_usec");
if (protocol.start(port, IPAddress(host)) == OK) {
EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR);

Error err;
if (GDScriptLanguageServer::use_stdio) {
err = protocol.start_stdio();
if (err == OK) {
OS::get_singleton()->print("--- GDScript language server started in stdio mode ---\n");
}
} else {
host = String(_EDITOR_GET("network/language_server/remote_host"));
port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
err = protocol.start(port, IPAddress(host));
if (err == OK) {
EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR);
}
}

if (err == OK) {
if (use_thread) {
thread_running = true;
thread.start(GDScriptLanguageServer::thread_main, this);
Expand All @@ -117,7 +131,11 @@ void GDScriptLanguageServer::stop() {
}
protocol.stop();
started = false;
EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
if (GDScriptLanguageServer::use_stdio) {
OS::get_singleton()->print("--- GDScript language server stopped ---\n");
} else {
EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
}
}

void register_lsp_types() {
Expand Down
Loading