Re: [Vala] disable free at block end

2016-03-11 Thread MohanR
On Thu, 2016-03-10 at 13:22 -0500, Guillaume Poirier-Morency wrote:
> 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);

Casting a 'struct' to (uint8[]) not working. I still have to use
pointer and memdup to manage the memory. Anyway, thanks for your help.
I'm now using SocketService+SocketConnection+{Input,Output}Stream. They
are just amazing.

Thanks,
Mohan R
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] disable free at block end

2016-03-10 Thread MohanR
On Thu, 2016-03-10 at 13:22 -0500, Guillaume Poirier-Morency wrote:
> 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.

Forgot SocketConnection is there. I'll use it. Thanks for pointing.

Thanks,
Mohan R
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] disable free at block end

2016-03-10 Thread Guillaume Poirier-Morency
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 remotes;
>   public SocketReader(Socket socket) {
>   this.socket = socket;
>   this.remotes = new Array();
>   }
>   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 
É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

signature.asc
Description: This is a digitally signed message part
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list