[m-dev.] Socket Library
Daniel Waterworth
da.waterworth at gmail.com
Wed Oct 20 20:26:09 AEDT 2010
On 20 October 2010 02:03, Peter Ross <pro at missioncriticalit.com> wrote:
> On Wed, Oct 20, 2010 at 11:16 AM, Julien Fischer
> <juliensf at csse.unimelb.edu.au> wrote:
>>
>> On Wed, 20 Oct 2010, Paul Bone wrote:
>>
>>> On Wed, Oct 20, 2010 at 08:39:32AM +1100, Peter Ross wrote:
>>>>
>>>> On Wed, Oct 20, 2010 at 3:04 AM, Daniel Waterworth
>>>> <da.waterworth at gmail.com> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I'm making a socket library in mercury. It works (I have sent simple
>>>>> HTTP requests using the code I've written and it supports IPv6), but
>>>>> as it stands it doesn't check for errors. So, how should C code go
>>>>> about raising exceptions? I haven't found any documentation on the
>>>>> subject.
>>>>>
>>>> If I was writing a socket library, I would make sure that it
>>>> implements the stream interface from the standard library. In that
>>>> case errors are raised by constructing a mercury type which indicates
>>>> the success or failure of the operation.
>>>>
>>>> Generally I write code like the following to handle that case. First
>>>> you write a low level predicate whose body is written in C. That
>>>> predicate returns an integer indicating whether or not the operation
>>>> succeeded and returns a string which is the reason why it failed.
>>>>
>>>> :- pred send_string_c(string::in, int::out, string::out, io::di, io::uo)
>>>> is det.
>>>>
>>>> The you write you high level interface.
>>>>
>>>> :- pred send_string(string::in, result::out, io::di, io::uo) is det.
>>>>
>>>> send_string(S, Result, !IO) :-
>>>> send_string_c(S, Res, Msg, !IO),
>>>> ( Res = -1 ->
>>>> Result = error(Msg)
>>>> ;
>>>> Result = ok
>>>> ).
>>
>> I would recommend using an exported enumeration to return the result
>> status. Passing small integers back and forth over the foreign language
>> interface tends to lead to less maintainable code IMO.
>>
> I agree with that point, exported enumerations are a nice way to do
> that. They didn't exist when I wrote most of my interfaces.
>
> --------------------------------------------------------------------------
> mercury-developers mailing list
> Post messages to: mercury-developers at csse.unimelb.edu.au
> Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
> Subscriptions: mercury-developers-request at csse.unimelb.edu.au
> --------------------------------------------------------------------------
>
Thanks for all your replies.
So it seems the best way to throw error conditions is for the C code
to return an item from an exported enumeration and for the mercury
code to output a stream.result(socket_error) where socket_error is the
aforementioned enumeration. I will implement this now.
I haven't done a lot of programming in mercury, doesn't this cause the
kind of code C is famous for, where 7/10ths of it are error checking?
One last question. I've been using the string type to store data, this
isn't ideal as it is null terminated, is there a more appropriate
type? Or will the string type in the future be implemented as a
bytestring?
Thanks again,
Daniel
p.s. If anyone's interested, I happy to make the library open-source
when I'm done.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions: mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the developers
mailing list