erlang - Why no need to remember the socket handle in handle_info? -


i confused chapter 4 example of "erlang , otp in action". https://github.com/erlware/erlang-and-otp-in-action-source/blob/master/chapter_04/tcp_rpc/src/tr_server.erl#l92

handle_info(timeout, #state{lsock = lsock} = state) ->     {ok, _sock} = gen_tcp:accept(lsock),     {noreply, state}. 

the book said:

you don’t need remember socket handle returned accept, because it’s included in each data package.

what mean exactly? what's "data package"? why socket handle name prefixed underscore? why no need save somewhere in case of gc? happens behind scene?

gen_server behavior seems difficult follow. explain in simple , clear way?

when establish connection in erlang tcp sockets, can make socket active or not active.

if socket active, every time receive data client, send erlang message format {tcp, socket, data}, tcp tuple id, socket variable have information regarding socket, on local port listening, on interface, , port/ip of peer sending data, , data data sent.

that's reason don't need remember socket, because every time receive package, have socket argument on message.

if set socket non active, need use function recv/2 in order receive data. active sockets easier use, if have traffic can overload erlang messaging system, when expect high load use non active sockets.

a "data package" package being sent on connection, in example command send client sent in 1 data package because small package, imagine data client has send you, instead of being 10 or 20 chars, 100kb, won't in 1 single package, data in different packages, , responsability programmer put data on multiple data packages together.

in erlang, when pattern matching, can match underscore, like

{expected, _} = {expected, anything} 

no matter have bound on variable anything, pattern matching true. now, imagine have underscores on code, times gets confused want add better code documentation, add someting after underscore, like

{expected, _middlename} = {expected, anything} 

so don't care middle name because don't need @ all, yet documented on code second value of tuple should have content of middle name.

the garbage collector won't collect socket in use.

gen_server generic server written in erlang, big topic because part of otp architecture, can find totorial on link, in few words, when start doing erlang, realize write same code initializing server (i'm not talking tcp server here, erlang server, process listen erlang messages , when receives acts , answer, or not, answer on synchronic message , won't answer on asyncrhonic message). saying, on time realize write on , on again same initialization, same init functions, etc.

otp_server erlang module abstract same code use repeats on , on again, , lets focus on task write real code customized functions in order serve clients, , because used everyone, gives fixed structure every erlang programmer understand , every programmer can follow without major issues. on link gave you, shows perfect example of writing erlang server module without otp, on writing generic server module template next server modules, , ends explaining otp you.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -