[m-dev.] Socket Library

Peter Ross pro at missioncriticalit.com
Wed Oct 20 08:39:32 AEDT 2010


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
  ).

Alternatively if you just want to throw an exception from the C code
directly, write some Mercury code with throws the exception that you
want and then :- pragma foreign_export it to make it accessible to you
C code.  Then just call the exported name directly.  eg

:- pred throw_exception(string::in) is erroneous.
:- pragma foreign_export("C", throw_exception(in), "c_throw_exception").

throw_exception(Msg) :- throw(some_exception_type(Msg)).

Now you can use c_throw_exception("xxx") directly in your C code.
--------------------------------------------------------------------------
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