Designing protocol library

Max Lapshin <>
Wed Dec 23 08:41:31 CET 2009


Hi everyone, I need help: I want to extract RTMP encode/decode library
from erlyvideo, but I still don't have clear idea, how would it look
like.

There are two ideas:
1) RTMP library just as HTTP mode of socket should be able to work
with sockets in active mode (send subscriber messages) and should
maintain its internal state
2) RTMP library should be useable as "pure functional" decoding
methods just like erlang:decode_packet

It seems for me, that API must look like:

Pure functional part:

-type(rtmp_state, opaque).
-type(rtmp_message, ...).

decode(State::rtmp_state(), Data::binary()) ->
{NewState::rtmp_state(), Message::rtmp_message(), Rest::binary()} |
{State::rtmp_state(), Data::binary()}.

encode(State::rtmp_state(), Message::rtmp_message()) ->
{NewState::rtmp_state(), Data::binary()}.

It is very important to track state, because RTMP is very, very
stateful protocol: each incoming message change RTMP decoder state.


Active socket part:

type(socket(), port()).
type(rtmp_mode(), active|passive|once).
type(rtmp_socket, {socket = socket(), state = rtmp_state(), consumer =
pid(), mode = rtmp_mode()}).
connect(Socket::socket()) -> RTMP::rtmp_socket()   — method for client
connection to RTMP server
accept(Socket::socket()) -> RTMP::rtmp_socket() — method for server
accepting new RTMP connection from client
setopts(RTMP::rtmp_socket(), [{active, true} | {active, once} |
passive]). — the same as mode for HTTP socket
send(RTMP::rtmp_socket(), Message::rtmp_message()} — it is very
important to send this message through API, because sending outside
messages also modifies state of RTMP encoder

Now, RTMP socket will send messages to opener:

1) {rtmp, RTMP, connected} — after encrypted handshake passes, this
message is sent to server side and to client side
2) {rtmp, RTMP, Message::rtmp_message()}
3) {rtmp, RTMP, disconnected}



Is such API clear?


More information about the erlang-questions mailing list