diff options
author | James Fish <[email protected]> | 2013-04-02 00:49:07 +0100 |
---|---|---|
committer | James Fish <[email protected]> | 2013-08-16 18:44:24 +0100 |
commit | ca6817880ee5592cf890fb5a71da41f52818d29a (patch) | |
tree | 79a0bb2b006fab3e1b23d267c0eb06633c37c015 /src/ranch_ssl.erl | |
parent | c767739ee3e917c9ce3c67827e655f10d6c191f9 (diff) | |
download | ranch-ca6817880ee5592cf890fb5a71da41f52818d29a.tar.gz ranch-ca6817880ee5592cf890fb5a71da41f52818d29a.tar.bz2 ranch-ca6817880ee5592cf890fb5a71da41f52818d29a.zip |
Add Transport:sendfile/4,/5
Adds offset based sendfile to transports. Same behaviour as
file:sendfile/4,/5 except socket and file arguments are reversed and
either a raw file or a filename can be used.
sendfile/2,/4,/5 now compulsory callbacks in ranch_transport.
ranch_tcp:sendfile/2 now defaults to a chunk_size of 8191 - the default
for ranch_ssl:sendfile/2. The same default is used for both
ranch_tcp:sendfile/4,5 and ranch_ssl:sendfile/4,5.
Diffstat (limited to 'src/ranch_ssl.erl')
-rw-r--r-- | src/ranch_ssl.erl | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/src/ranch_ssl.erl b/src/ranch_ssl.erl index 98b2f5e..dfc5f11 100644 --- a/src/ranch_ssl.erl +++ b/src/ranch_ssl.erl @@ -34,6 +34,8 @@ -export([recv/3]). -export([send/2]). -export([sendfile/2]). +-export([sendfile/4]). +-export([sendfile/5]). -export([setopts/2]). -export([controlling_process/2]). -export([peername/1]). @@ -188,33 +190,32 @@ recv(Socket, Length, Timeout) -> send(Socket, Packet) -> ssl:send(Socket, Packet). -%% @doc Send a file on a socket. +%% @equiv sendfile(Socket, Filename, 0, 0, []) +-spec sendfile(ssl:sslsocket(), file:name_all()) + -> {ok, non_neg_integer()} | {error, atom()}. +sendfile(Socket, Filename) -> + sendfile(Socket, Filename, 0, 0, []). + +%% @equiv sendfile(Socket, File, Offset, Bytes, []) +-spec sendfile(ssl:sslsocket(), file:name_all() | file:fd(), + non_neg_integer(), non_neg_integer()) + -> {ok, non_neg_integer()} | {error, atom()}. +sendfile(Socket, File, Offset, Bytes) -> + sendfile(Socket, File, Offset, Bytes, []). + +%% @doc Send part of a file on a socket. %% %% Unlike with TCP, no syscall can be used here, so sending files -%% through SSL will be much slower in comparison. +%% through SSL will be much slower in comparison. Note that unlike +%% file:sendfile/5 this function accepts either a file or a file name. %% -%% @see file:sendfile/2 --spec sendfile(ssl:sslsocket(), file:name()) +%% @see ranch_transport:sendfile/6 +%% @see file:sendfile/5 +-spec sendfile(ssl:sslsocket(), file:name_all() | file:fd(), + non_neg_integer(), non_neg_integer(), ranch_transport:sendfile_opts()) -> {ok, non_neg_integer()} | {error, atom()}. -sendfile(Socket, Filepath) -> - {ok, IoDevice} = file:open(Filepath, [read, binary, raw]), - sendfile(Socket, IoDevice, 0). - --spec sendfile(ssl:sslsocket(), file:io_device(), non_neg_integer()) - -> {ok, non_neg_integer()} | {error, atom()}. -sendfile(Socket, IoDevice, Sent) -> - case file:read(IoDevice, 16#1FFF) of - eof -> - ok = file:close(IoDevice), - {ok, Sent}; - {ok, Bin} -> - case send(Socket, Bin) of - ok -> - sendfile(Socket, IoDevice, Sent + byte_size(Bin)); - {error, Reason} -> - {error, Reason} - end - end. +sendfile(Socket, File, Offset, Bytes, Opts) -> + ranch_transport:sendfile(?MODULE, Socket, File, Offset, Bytes, Opts). %% @doc Set options on the given socket. %% @see ssl:setopts/2 |