- Edited
Hello. I am doing a plugin for winsock because I could not get the standard TCP Peer class to connect.
I tried many different things by looking at the example in the godot source code and right now I have something like this:
void WinSocket::set_message_buffer(godot::Ref<godot::PoolByteArray> &messageBuffer)
{
this->messageBuffer = messageBuffer.ptr();
//this->messageBuffer = messageBuffer;
}
But I am getting some cryptic errors when compiling:
C:\Projects\godot-cpp\include\core\Godot.hpp(131): error C2440: 'return': cannot convert from 'godot::Variant' to 'T' with [ T=godot::Ref<godot::PoolByteArray> & ] C:\Projects\godot-cpp\include\core\Godot.hpp(130): note: while compiling class template member function 'T godot::_ArgCast<T>::_arg_cast(godot::Variant)' with [ T=godot::Ref<godot::PoolByteArray> & ] C:\Projects\godot-cpp\include\core\Godot.hpp(263): note: see reference to function template instantiation 'T godot::_ArgCast<T>::_arg_cast(godot::Variant)' being compiled with [ T=godot::Ref<godot::PoolByteArray> & ] C:\Projects\godot-cpp\include\core\Godot.hpp(262): note: see reference to class template instantiation 'godot::_ArgCast<godot::Ref<godot::PoolByteArray> &>' being compiled
The code for writing to the PoolByte array looks something like this (the print(c) successfully prints the right character to the output:
godot::PoolByteArray::Write writeObject = messageBuffer->write();
uint8_t* ptr = writeObject.ptr();
byte* tmpBuffer = (byte*) malloc(messageSize);
blocking_receive(tmpBuffer, messageSize);
for (int i = 0; i < messageSize; i++) {
byte c = tmpBuffer[i];
godot::Godot::print(c);
*(ptr+i) = c;
}
::free(tmpBuffer);
My gdscript code looks like this, but nothing gets printed when calling get_string_from_ascii()
extends Node
export var win_socket_script: NodePath
var win_socket
var dataBuffer: PoolByteArray
func _ready():
dataBuffer.resize(16384)
win_socket = get_node(win_socket_script)
win_socket.connect_to_host("localhost", 2000)
win_socket.set_message_header_size(2)
win_socket.set_message_buffer(dataBuffer)
print("Waiting for message...")
win_socket.blocking_receive_message()
print("Received message:")
print(dataBuffer.get_string_from_ascii())
The reason I tried to pass by reference now, using Ref, is without it the code compiles but any changes I do to the PoolByteArray from gdnative does not seems to reflect in gdscript.