Paste number 26798: Sockmod

Paste number 26798: Sockmod
Pasted by: Cynos
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+KOE
Channel:None
Paste contents:
Raw Source | XML | Display As
-module(sockmod).
-export([conCreate/1]).


conCreate(Handler) ->
    %If any initialisation needs to occur, it can here.
	conLoop(Handler, 0).

conLoop(Handler, Connection) ->
	receive

	    {connect, Server, Port} ->
		case gen_tcp:connect(Server, Port, [{keepalive, true}, {packet, line}]) of
				
		    {ok, NewCon} ->
			Handler ! ok,
			conLoop(Handler, NewCon);
				
		    {error, Reason} ->
			Handler ! {error, connect, Reason},
			conLoop(Handler, 0)
		end;		
					
	    {send, Data} ->
		case gen_tcp:send(Connection, Data) of
		    ok -> Handler ! ok;
		    {error, Reason} -> Handler ! {error, send, Reason}
		end,
		conLoop(Handler, Connection);
		
		
	    disconnect when Connection == 0 ->
		Handler ! ok,
		conLoop(Handler, 0);
					
	    disconnect ->
		gen_tcp:close(Connection),
		Handler ! ok,
		conLoop(Handler, 0);
                
	    finish ->
		ok;

	    {tcp, _Socket, Data} ->
		Handler ! {data_received, Data},
		conLoop(Handler, Connection);
			
	    {tcp_closed, _Socket} ->
		Handler ! rem_discon,
		conLoop(Handler, 0);
		
	    {tcp_error, _Socket, Reason} ->
		Handler ! {error, other, Reason},
		conLoop(Handler, Connection)
	end.

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.