| 1 |
== Writing parsers |
| 2 |
|
| 3 |
There are three kinds of protocols: |
| 4 |
|
| 5 |
* Text protocols |
| 6 |
* Schema-less binary protocols |
| 7 |
* Schema-based binary protocols |
| 8 |
|
| 9 |
This chapter introduces the first two kinds. It will not cover |
| 10 |
more advanced topics such as continuations or parser generators. |
| 11 |
|
| 12 |
This chapter isn't specifically about Ranch, we assume here that |
| 13 |
you know how to read data from the socket. The data you read and |
| 14 |
the data that hasn't been parsed is saved in a buffer. Every |
| 15 |
time you read from the socket, the data read is appended to the |
| 16 |
buffer. What happens next depends on the kind of protocol. We |
| 17 |
will only cover the first two. |
| 18 |
|
| 19 |
=== Parsing text |
| 20 |
|
| 21 |
Text protocols are generally line based. This means that we can't |
| 22 |
do anything with them until we receive the full line. |
| 23 |
|
| 24 |
A simple way to get a full line is to use `binary:split/2,3`. |
| 25 |
|
| 26 |
.Using binary:split/2 to get a line of input |
| 27 |
|
| 28 |
[source,erlang] |
| 29 |
case binary:split(Buffer, <<"\n">>) of |
| 30 |
[_] -> |
| 31 |
get_more_data(Buffer); |
| 32 |
[Line, Rest] -> |
| 33 |
handle_line(Line, Rest) |
| 34 |
end. |
| 35 |
|
| 36 |
In the above example, we can have two results. Either there was |
| 37 |
a line break in the buffer and we get it split into two parts, |
| 38 |
the line and the rest of the buffer; or there was no line break |
| 39 |
in the buffer and we need to get more data from the socket. |
| 40 |
|
| 41 |
Next, we need to parse the line. The simplest way is to again |
| 42 |
split, here on space. The difference is that we want to split |
| 43 |
on all spaces character, as we want to tokenize the whole string. |
| 44 |
|
| 45 |
.Using binary:split/3 to split text |
| 46 |
|
| 47 |
[source,erlang] |
| 48 |
case binary:split(Line, <<" ">>, [global]) of |
| 49 |
[<<"HELLO">>] -> |
| 50 |
be_polite(); |
| 51 |
[<<"AUTH">>, User, Password] -> |
| 52 |
authenticate_user(User, Password); |
| 53 |
[<<"QUIT">>, Reason] -> |
| 54 |
quit(Reason) |
| 55 |
%% ... |
| 56 |
end. |
| 57 |
|
| 58 |
Pretty simple, right? Match on the command name, get the rest |
| 59 |
of the tokens in variables and call the respective functions. |
| 60 |
|
| 61 |
After doing this, you will want to check if there is another |
| 62 |
line in the buffer, and handle it immediately if any. |
| 63 |
Otherwise wait for more data. |
| 64 |
|
| 65 |
=== Parsing binary |
| 66 |
|
| 67 |
Binary protocols can be more varied, although most of them are |
| 68 |
pretty similar. The first four bytes of a frame tend to be |
| 69 |
the size of the frame, which is followed by a certain number |
| 70 |
of bytes for the type of frame and then various parameters. |
| 71 |
|
| 72 |
Sometimes the size of the frame includes the first four bytes, |
| 73 |
sometimes not. Other times this size is encoded over two bytes. |
| 74 |
And even other times little-endian is used instead of big-endian. |
| 75 |
|
| 76 |
The general idea stays the same though. |
| 77 |
|
| 78 |
.Using binary pattern matching to split frames |
| 79 |
|
| 80 |
[source,erlang] |
| 81 |
<< Size:32, _/bits >> = Buffer, |
| 82 |
case Buffer of |
| 83 |
<< Frame:Size/binary, Rest/bits >> -> |
| 84 |
handle_frame(Frame, Rest); |
| 85 |
_ -> |
| 86 |
get_more_data(Buffer) |
| 87 |
end. |
| 88 |
|
| 89 |
You will then need to parse this frame using binary pattern |
| 90 |
matching, and handle it. Then you will want to check if there |
| 91 |
is another frame fully received in the buffer, and handle it |
| 92 |
immediately if any. Otherwise wait for more data. |