Curt,

Thanks, your code really helped me. But some questions:


<snip>

I've had trouble with CStruct types and "is rw", so I usually just make those
"Pointer is rw" to get the pointer back, then use nativecast() to make it into
the CStruct type I want.
This is useful, and a comment in the documentation would be good.
<snip>
class SockAddr {
     has uint16 $.sa_family;
     has CArray[int8] $.sa_data;
}
This is incorrect because "CArray[int8]" will make a pointer to
the buffer, but the C struct really wants to put the 14 bytes directly
here.  But that is only for INET addresses, for INET6 (which google.com
also has), you need even more bytes here.
I realised from the C documentation that a fixed 14 byte variable was required, but I could not find a mention about how to map that into Perl6.
Thankfully, we can ignore most of that since for this example, we're
only reading stuff, not actually trying to create the structure.
Suppose it was necessary to create a structure, how would this be done? Actually you use .allocate  below, is this the mechanism? Since I am not familiar with this C function, I don't know whether there is ever a need to create a SockAddr.

<snip>
sub inet_ntop(int32, Pointer, Blob, int32 --> Str)
     is native() {}
a) Blob is TBD in NativeCall
Some explanation would be useful.

b) I used 'returns Str' and you have used '--> Str'
GTK::Simple uses the 'return' syntax.
Are the two equivalent and the choice is a programmer style one, or are there differences?
Is there a reason '-->' is better than 'returns'?

<snip>
class SockAddr-in is repr('CStruct') {
     has int16 $.sin_family;
     has uint16 $.sin_port;
     has uint32 $.sin_addr;

     method address {
         my $buf = buf8.allocate(INET_ADDRSTRLEN);
         inet_ntop(AF_INET, Pointer.new(nativecast(Pointer,self)+4),
             $buf, INET_ADDRSTRLEN)
     }
}
I have not come across the stanza 'buf8.allocate(...)' before.
Is this in the missing 'Blob and Buffer' section?
Do I understand correctly that it allocates ... 'buf8' units to $buf?
Are these zero initialised automatically by Perl6?

Also the code 'Pointer.new(nativecast(Pointer,self)+4)'

I understand you said pointer arithmetic. Where did the +4 for INET and +8 for INET6 come from?

However, nativecast seems to be doing some magic here because 'self' is the class instantiation. Or since everything is a reference in Perl6, everything can be cast to a Pointer?


Regards,
Richard

Reply via email to