Skip to content

Connection part

Roman edited this page Apr 1, 2017 · 1 revision

Connection part

Next document describe process using of connection, if you want send message directly by TCP or you want write self new protocol.

Arbiter

Arbiter is the general process in connection functional. He decides which stream is free (available), keeps busy socket (process), and it does not allow the use of extra resources.

Using

Arbiter searches a free stream and hold a found. From this moment Socket from this process is locked.

{ok, PoolPid} = sgi_arbiter:alloc(),

Send data to a hold process. Process redirect a request to a socket for async call.

PoolPid ! {send, Data, self()},

Receive data from a hold process. Which received data from socket, but not free at the moment and can get another data.

handle_info({socket_return, Data}, State)

or was some error with connecting or sending a message

handle_info({socket_error, Data}, State)

If we recieve all expect data we can free process (and socket). It's not necessarily close connection and don't need create new.

sgi_arbiter:free(PoolPid),
Or easy way if you want send data and just receive one message

For example you want send quick (test) message and you do not want write many codes, use follow:

{ok, Bin} = sgi_pool:once_call(Request),

Multiplexer

Run multiplexer if your application supports multiplexing. Even if you don't know, will be better to run multiplexer also, it helps your application not to get failed.

{ok, _Pid} = sgi_sup:start_child(sgi_multiplexer, {?MODULE, request_pid})

Send data

sgi_multiplexer ! {send, Data, PoolPid},

You should set callback function where multiplexer will get your local PID from a storage(ETS) by request_id of your request: {?MODULE, request_pid} Set callback after start process sgi_multiplexer:set_callback({M, F}) or set callback to each request {send, Request, PoolPid, M, F}.

Clone this wiki locally