Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Re: Common IO structure
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-07 (22:11)
From: Benjamin Geer <ben@s...>
Subject: Re: [Caml-list] Re: Common IO structure
Vladimir N. Silyaev wrote:
> This signature looks like a good starting point. However I would rather
> separate this code to three different pieces

That seems fine to me.  I just wanted to give you a very rough idea of 
what I had in mind; I was pretty sure you'd see a better way to design 
it. :)

> Please note,  that write and read inherently separated in signatures,
> it allows simpler interface, supports read/write only streams
> and better feet common model, where read and writes are separated.
> However, module couldn't implement both read and write signatures, if
> it's required.

The main thing I wanted to point out was that there needs to be a way to 
read data into a buffer from a non-blocking socket into a buffer, and 
then write the data from the *same buffer* into another non-blocking 
socket.  Then compact the buffer (move any unwritten data to the 
beginning of the buffer) and start again, like in this loop:

let copy_fd in_fd out_fd =
   let b = AsyncBuffer.create () in
       while (true) do
         AsyncBuffer.from_fd b in_fd;
         AsyncBuffer.flip b;
         AsyncBuffer.to_fd b out_fd;
         AsyncBuffer.compact b
     with End_of_file -> ()

Can that still be done if the read and write signatures are separated?

The other thing that's important is that character encoder/decoders 
would need to be able to read characters from one buffer and write them 
to another buffer in a different encoding.  An encoder/decoder would 
need to gracefully handle the case where it reads from a buffer 
containing incomplete characters.  That's another reason for the 
'compact' function: you could read 10 bytes from a socket into a buffer, 
and those 10 bytes could contain 9 bytes worth of complete UTF-8 
characters; the 10th byte would be the first byte of a multi-byte 
character.  You'd pass the buffer to an encoder/decoder, which would 
read 9 bytes and write them into another buffer in a different encoding 
(say UTF-16), leaving the last byte.  You would then call 'compact' to 
move that byte to the beginning of the buffer, and repeat.

Is there a way to fit this approach into what you've proposed for 


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: