Permalink
Browse files

Move functions around into related groups

  • Loading branch information...
1 parent 750a0fb commit d27c3f9a0a672115ca4341266f6b801cb82c1fb2 @brendonh committed Feb 9, 2010
Showing with 86 additions and 83 deletions.
  1. +86 −83 src/slerl_sim.erl
View
@@ -18,11 +18,10 @@
%% gen_fsm callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
--define(PORT, 0).
--define(MTU, 1200). % It's what libOMV uses.
+-define(MTU, 1200).
-define(ACK_PACKET_TIMER, 500).
-
+-define(PORT, 0).
-define(SOCKET_OPTIONS, [binary, {active, true}]).
-record(state, {
@@ -100,9 +99,14 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
+
%%--------------------------------------------------------------------
-%%% Internal functions
-%%--------------------------------------------------------------------
+%%% Receiving
+%%--------------------------------------------------------------------
+
+bool(1) -> true;
+bool(0) -> false.
+
handle_packet(Packet, State) ->
try parse_packet(Packet, State) of
@@ -128,6 +132,75 @@ handle_message(Message, State) ->
end.
+parse_packet(<<Zeroed:1/integer-unit:1, Reliable:1/integer-unit:1,
+ Resent:1/integer-unit:1, HasAcks:1/integer-unit:1,
+ 0:4/integer-unit:1,
+ Sequence:4/integer-unit:8, ExtraHeader:1/integer-unit:8,
+ Rest/binary>>, _State) ->
+ {MessageBlock, AckBlock} = split_packet(bool(HasAcks), Rest),
+
+ Acks = parse_acks(AckBlock, []),
+
+ case Acks of
+ [] -> ok;
+ _ -> ?DBG({got_acks, Acks})
+ end,
+
+ Skeleton = #message{
+ zerocoded=bool(Zeroed),
+ reliable=bool(Reliable),
+ resend=bool(Resent),
+ sequence=Sequence},
+ parse_message(ExtraHeader, MessageBlock, Skeleton).
+
+
+split_packet(false, Packet) ->
+ {Packet, <<>>};
+split_packet(true, Packet) ->
+ BS = byte_size(Packet) - 1,
+ <<Tail:BS/binary, AckCount:1/integer-unit:8>> = Packet,
+ split_binary(Tail, BS - (AckCount * 4)).
+
+
+parse_message(0, Rest, Message) ->
+ slerl_message:parse_message(Rest, Message);
+parse_message(Count, Packet, Message) ->
+ <<Extra:Count/binary, Rest/binary>> = Packet,
+ slerl_message:parse_message(Rest, Message#message{extra=Extra}).
+
+
+parse_acks(<<>>, Buff) -> lists:reverse(Buff);
+parse_acks(<<X:1/integer-unit:32, Rest/binary>>, Buff) ->
+ parse_acks(Rest, [X|Buff]);
+parse_acks(_Other, _) -> []. % Screw it.
+
+
+update_ack_timer(#state{ackPacketTimer=none}=State) ->
+ {ok, TRef} = timer:send_after(?ACK_PACKET_TIMER, ack_packet_timer),
+ State#state{ackPacketTimer=TRef};
+update_ack_timer(#state{ackPacketTimer=OldTRef}=State) ->
+ timer:cancel(OldTRef),
+ {ok, TRef} = timer:send_after(?ACK_PACKET_TIMER, ack_packet_timer),
+ State#state{ackPacketTimer=TRef}.
+
+
+
+%%====================================================================
+%% Sending
+%%====================================================================
+
+unbool(true) -> 1;
+unbool(false) -> 0.
+
+
+reliable(M) -> M#message{reliable=true}.
+
+
+assign_sequence(Message, State) ->
+ Seq = State#state.sequence,
+ {Message#message{sequence=Seq}, State#state{sequence=Seq+1}}.
+
+
dispatch_message(Message, true, State) ->
{RelMessage, NewState} = assign_sequence(reliable(Message), State),
NewState2 = dispatch_message(RelMessage, NewState),
@@ -140,18 +213,13 @@ dispatch_message(Message, false, State) ->
dispatch_message(M, S).
-assign_sequence(Message, State) ->
- Seq = State#state.sequence,
- {Message#message{sequence=Seq}, State#state{sequence=Seq+1}}.
-
-
dispatch_message(Message, State) ->
Spec = Message#message.spec,
?DBG({sending, Spec#messageDef.name}),
{Packet, NewState} = build_packet(Message, State),
send(Packet, NewState).
-
+
send(Bin, State) ->
Sim = State#state.sim,
ok = gen_udp:send(State#state.socket,
@@ -160,34 +228,6 @@ send(Bin, State) ->
update_ack_timer(State).
-update_ack_timer(#state{ackPacketTimer=none}=State) ->
- {ok, TRef} = timer:send_after(?ACK_PACKET_TIMER, ack_packet_timer),
- State#state{ackPacketTimer=TRef};
-update_ack_timer(#state{ackPacketTimer=OldTRef}=State) ->
- timer:cancel(OldTRef),
- {ok, TRef} = timer:send_after(?ACK_PACKET_TIMER, ack_packet_timer),
- State#state{ackPacketTimer=TRef}.
-
-
-
-
-reliable(M) -> M#message{reliable=true}.
-
-
-bool(1) -> true;
-bool(0) -> false.
-
-unbool(true) -> 1;
-unbool(false) -> 0.
-
-
-make_flags(Bits) ->
- [Zero, Reliable, Resend, Acks] = lists:map(fun unbool/1, Bits),
- <<Zero:1/integer-unit:1, Reliable:1/integer-unit:1,
- Resend:1/integer-unit:1, Acks:1/integer-unit:1,
- 0:4/integer-unit:1>>.
-
-
build_packet(Message, State) ->
Spec = Message#message.spec,
Flags = make_flags([Spec#messageDef.zerocoded,
@@ -203,56 +243,20 @@ build_packet(Message, State) ->
{list_to_binary([Packet, AckSuffix]), NewState#state{sequence=Seq+1}}.
+make_flags(Bits) ->
+ [Zero, Reliable, Resend, Acks] = lists:map(fun unbool/1, Bits),
+ <<Zero:1/integer-unit:1, Reliable:1/integer-unit:1,
+ Resend:1/integer-unit:1, Acks:1/integer-unit:1,
+ 0:4/integer-unit:1>>.
+
+
ack_suffix(BS, Count, Bits, #state{queuedAcks=Acks}=State)
when BS == ?MTU orelse Count == 255 orelse Acks == [] ->
{[<<Count:1/integer-unit:8>>|lists:reverse(Bits)], State};
ack_suffix(BS, Count, Bits, #state{queuedAcks=[Ack|Rest]}=State) ->
ack_suffix(BS+4, Count+1, [<<Ack:1/integer-unit:32>>|Bits], State#state{queuedAcks=Rest}).
-parse_packet(<<Zeroed:1/integer-unit:1, Reliable:1/integer-unit:1,
- Resent:1/integer-unit:1, HasAcks:1/integer-unit:1,
- 0:4/integer-unit:1,
- Sequence:4/integer-unit:8, ExtraHeader:1/integer-unit:8,
- Rest/binary>>, _State) ->
- {MessageBlock, AckBlock} = split_packet(bool(HasAcks), Rest),
-
- Acks = parse_acks(AckBlock, []),
-
- case Acks of
- [] -> ok;
- _ -> ?DBG({got_acks, Acks})
- end,
-
- Skeleton = #message{
- zerocoded=bool(Zeroed),
- reliable=bool(Reliable),
- resend=bool(Resent),
- sequence=Sequence},
- parse_message(ExtraHeader, MessageBlock, Skeleton).
-
-
-
-split_packet(false, Packet) ->
- {Packet, <<>>};
-split_packet(true, Packet) ->
- BS = byte_size(Packet) - 1,
- <<Tail:BS/binary, AckCount:1/integer-unit:8>> = Packet,
- split_binary(Tail, BS - (AckCount * 4)).
-
-
-parse_message(0, Rest, Message) ->
- slerl_message:parse_message(Rest, Message);
-parse_message(Count, Packet, Message) ->
- <<Extra:Count/binary, Rest/binary>> = Packet,
- slerl_message:parse_message(Rest, Message#message{extra=Extra}).
-
-
-parse_acks(<<>>, Buff) -> lists:reverse(Buff);
-parse_acks(<<X:1/integer-unit:32, Rest/binary>>, Buff) ->
- parse_acks(Rest, [X|Buff]);
-parse_acks(_Other, _) -> []. % Screw it.
-
%%====================================================================
%% Timer callbacks
@@ -272,7 +276,6 @@ send_ack_packet(#state{queuedAcks=Acks}=State) ->
%%====================================================================
-
send_connect_packets(State) ->
State1 = use_circuit_code(State),
State2 = complete_agent_movement(State1),

0 comments on commit d27c3f9

Please sign in to comment.