48 lines
1.2 KiB
Erlang
48 lines
1.2 KiB
Erlang
% Copyright (c) Camden Dixie O'Brien
|
|
% SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-module(session_server).
|
|
-behaviour(gen_server).
|
|
|
|
-export([start_link/1]).
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
terminate/2, code_change/3]).
|
|
|
|
start_link(Socket) ->
|
|
gen_server:start_link(?MODULE, Socket, []).
|
|
|
|
init(Socket) ->
|
|
ok = ssl:setopts(Socket, [{active, true}]),
|
|
{ok, #{socket => Socket}}.
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
{reply, ok, State}.
|
|
|
|
handle_cast(_Msg, State) ->
|
|
{noreply, State}.
|
|
|
|
handle_info({ssl, Socket, Data}, State) ->
|
|
case 'StudySystemProtocol':decode('Request', Data) of
|
|
{ok, {ping, _}} ->
|
|
{ok, Encoded} = 'StudySystemProtocol':encode(
|
|
'Response', {ack, 'NULL'}),
|
|
ok = ssl:send(Socket, Encoded);
|
|
{error, {asn1, _Reason}} ->
|
|
{ok, Encoded} = 'StudySystemProtocol':encode(
|
|
'Response', {error, invalidRequest}),
|
|
ok = ssl:send(Socket, Encoded)
|
|
end,
|
|
{noreply, State};
|
|
handle_info({ssl_closed, _Socket}, State) ->
|
|
{stop, normal, State};
|
|
handle_info({ssl_error, _Socket, _Reason}, State) ->
|
|
{stop, normal, State};
|
|
handle_info(_Info, State) ->
|
|
{noreply, State}.
|
|
|
|
terminate(_Reason, #{socket := Socket}) ->
|
|
ssl:close(Socket).
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
{ok, State}.
|