Le jeudi 10 mars 2016 à 23:34 +0530, MohanR a écrit :
Hi, May be a i'm wrong, but here is my issue, namespace test { struct Remote { uint16 id; uint32 ip; } class SocketReader: Object { Socket socket; Array<Remote*> remotes; public SocketReader(Socket socket) { this.socket = socket; this.remotes = new Array<Remote*>(); } public read() { int8[] buffer = new int8[sizeof(Remote)]; while(socket.receive(buffer, sizeof(Remote)) > 0) { Remote* remote = Memory.dup(buffer, sizeof(Remote)); this.remotes.append_val(remote); } } } } when read() goes out-of-scope, "buffer" gets freed, So I have to make a dup of that memory to store it in "remotes" array. But If I have some way to tell vala not to free buffer, I dont need to dup the buffer. Am I doing this right? or any better way? All I need is to get the data coming from the socket and use it as "Remote" struct. Need your suggestions. Thanks, Mohan R
You should read directly in the memory allocated for the 'Remote' struct instead of using a temporary buffer. I think it's correct to cast it to uint8[]. socket.receive ((uint8[]) remote); remotes.append_val (remote); However, your code is likely incorrect, because 'Socket.receive' states that it will read up to 'buffer.length', so you may end with a partial read. You need to loop with an offset, something like: size_t offset = 0; while (offset < sizeof (Remote) offset += socket.receive ((uint8[] remote) [offset:sizeof (Remote) - offset]); Unless you are doing something very specific, use SocketService, it has a much simpler interface: http://valadoc.org/#!api=gio-2.0/GLib.SocketService The SocketConnection is an IOStream, so you have access to input_stream.read_all, which will perform a complete read for the requested buffer size.
_______________________________________________ vala-list mailing list vala-list gnome org https://mail.gnome.org/mailman/listinfo/vala-list
-- Guillaume Poirier-Morency <guillaumepoiriermorency gmail com> Étudiant au baccalauréat en Informatique à l'Université de MontréalDéveloppeur d'application web Mon blog: https://arteymix.github.io/Mon projet de coopérative: https://pittoresque.github.io/Clé PGP: B1AD6EA5
Attachment:
signature.asc
Description: This is a digitally signed message part