diff options
author | Loïc Hoguin <[email protected]> | 2013-08-27 18:00:59 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-08-27 18:00:59 +0200 |
commit | 0da3b24b3f123f4871fe2701aa9963f4a8e2855b (patch) | |
tree | 5b452fd77ed1b96dce5615b8e7ec6c132ed9542a /guide | |
parent | 41705752ffc7e3ecb96eef8374015ead4ca7f699 (diff) | |
parent | ca6817880ee5592cf890fb5a71da41f52818d29a (diff) | |
download | ranch-0da3b24b3f123f4871fe2701aa9963f4a8e2855b.tar.gz ranch-0da3b24b3f123f4871fe2701aa9963f4a8e2855b.tar.bz2 ranch-0da3b24b3f123f4871fe2701aa9963f4a8e2855b.zip |
Merge branch 'add-offset-sendfile' of git://github.com/fishcakez/ranch
Diffstat (limited to 'guide')
-rw-r--r-- | guide/transports.md | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/guide/transports.md b/guide/transports.md index 1844ac9..aaf530f 100644 --- a/guide/transports.md +++ b/guide/transports.md @@ -119,6 +119,34 @@ end. You can easily integrate active sockets with existing Erlang code as all you really need is just a few more clauses when receiving messages. +Sending files +------------- + +As in the previous section it is assumed `Transport` is a valid transport +handler and `Socket` is a connected socket obtained through the listener. + +To send a whole file, with name `Filename`, over a socket: + +```erlang +{ok, SentBytes} = Transport:sendfile(Socket, Filename). +``` + +Or part of a file, with `Offset` greater than or equal to 0, `Bytes` number of +bytes and chunks of size `ChunkSize`: + +```erlang +Opts = [{chunk_size, ChunkSize}], +{ok, SentBytes} = Transport:sendfile(Socket, Filename, Offset, Bytes, Opts). +``` + +To improve efficiency when sending multiple parts of the same file it is also +possible to use a file descriptor opened in raw mode: + +```erlang +{ok, RawFile} = file:open(Filename, [raw, read, binary]), +{ok, SentBytes} = Transport:sendfile(Socket, RawFile, Offset, Bytes, Opts). +``` + Writing a transport handler --------------------------- @@ -131,3 +159,7 @@ socket. These do not need to be common to all transports as it's easy enough to write different initialization functions for the different transports that will be used. With one exception though. The `setopts/2` function *must* implement the `{active, once}` and the `{active, true}` options. + +If the transport handler doesn't have a native implementation of `sendfile/5` a +fallback is available, `ranch_transport:sendfile/6`. The extra first argument +is the transport's module. See `ranch_ssl` for an example. |