[erlang-questions] how: cascading events in gen_fsm

jm <>
Tue Nov 27 23:20:59 CET 2007


The problem: I'm using gen_fsm to model a network protocol based on 
messages. For the most part the messages are <<runlength, 
message:runlength/binary>>, except for a few special cases in the 
initial handshake. The messages are sent over TCP, but there is no 
guarantee that a complete message or only one message will arrive in a 
packet. To get around this a queue is mantained as part of the state record,

-record(state, {
                 queue
                 ....
                }).


which is used to hold the bytes recieved but are yet to be processed. 
When there are bytes remaining after processing the current state the 
remainder is placed on the queue and

gen_fsm:send_event(self(), {msg, <<>>}),
{next_state, new_state, State#{queue = Remainder}}.

is executed to cause the gen_fsm process to jump to the next state and 
continue processing. Two questions,

1) Can anyone see a problem doing it this way (race conditions, etc)?
2) Is there a better way to cascade states with gen_fsm?


Jeff.



More information about the erlang-questions mailing list