Formatted

This commit is contained in:
Eishun Kondoh 2018-01-30 22:47:31 +09:00
parent 5fc01a9bec
commit 7635272fbd
150 changed files with 5055 additions and 4032 deletions

View file

@ -5,23 +5,26 @@ defmodule Openflow do
@spec read(binary()) :: {:ok, map()} | {:error, :binary_too_small}
def read(binary)
when byte_size(binary) < @ofp_header_size do
when byte_size(binary) < @ofp_header_size do
{:error, :binary_too_small}
end
def read(<<_v::8, _t::8, len::16, _x::32, _r::bytes>> = binary)
when byte_size(binary) < len do
when byte_size(binary) < len do
{:error, :binary_too_small}
end
def read(<<ver::8, type::8, len::16, xid::32, binary2::bytes>>) do
body_len = len - @ofp_header_size
<<body_bin::bytes-size(body_len), rest::bytes>> = binary2
result = type
|> Openflow.Enums.to_atom(:openflow_codec)
|> do_read(body_bin)
result =
type
|> Openflow.Enums.to_atom(:openflow_codec)
|> do_read(body_bin)
case result do
{:ok, struct} -> {:ok, %{struct|version: ver, xid: xid}, rest}
{:ok, struct} -> {:ok, %{struct | version: ver, xid: xid}, rest}
{:error, reason} -> {:error, reason}
end
end
@ -30,19 +33,23 @@ defmodule Openflow do
binaries = for message <- messages, do: to_binary(message)
Enum.join(binaries, "")
end
def to_binary(%{__struct__: encoder, version: version, xid: xid} = msg) do
case encoder.to_binary(msg) do
body_bin when is_binary(body_bin) ->
length = @ofp_header_size + byte_size(body_bin)
<<version::8, (encoder.ofp_type)::8, length::16, xid::32, body_bin::bytes>>
<<version::8, encoder.ofp_type::8, length::16, xid::32, body_bin::bytes>>
{:error, reason} ->
{:error, reason}
end
end
def append_body(nil, message), do: message
def append_body(message, continue) do
mod = message.__struct__
if function_exported?(mod, :append_body, 2) do
mod.append_body(message, continue)
else
@ -55,6 +62,7 @@ defmodule Openflow do
defp do_read({:error, reason}, _) do
{:error, reason}
end
defp do_read(decoder, body_bin) do
case decoder.read(body_bin) do
{:error, reason} -> {:error, reason}