1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
%%%-------------------------------------------------------------------
%%% @author Micael Karlberg <[email protected]>
%%% @copyright (C) 2018, Micael Karlberg
%%% @doc
%%%
%%% @end
%%% Created : 27 Jun 2018 by Micael Karlberg <[email protected]>
%%%-------------------------------------------------------------------
-module(socket_server).
-export([start/0]).
-record(handler, {socket, parent}).
start() ->
start_tcp().
start_tcp() ->
start(inet, stream, tcp).
start(Domain, Type, Proto) ->
put(sname, "starter"),
try do_init(Domain, Type, Proto) of
Sock ->
accept_loop(Sock)
catch
throw:E:P ->
e("Failed initiate: "
"~n Error: ~p"
"~n Path: ~p", [E, P])
end.
do_init(Domain, Type, Proto) ->
i("try (socket) open"),
Sock = case socket:open(Domain, Type, Proto) of
{ok, S} ->
S;
{error, OReason} ->
throw({open, OReason})
end,
i("opened - now try find (local) address"),
Addr = which_addr(Domain),
SA = #{family => Domain,
addr => Addr},
i("addr ~p - now try (socket) bind", [Addr]),
Port = case socket:bind(Sock, SA) of
{ok, P} ->
P;
{error, BReason} ->
throw({bind, BReason})
end,
i("bound to ~w - now try (socket) listen", [Port]),
case socket:listen(Sock) of
ok ->
Sock;
{error, LReason} ->
throw({listen, LReason})
end.
which_addr(Domain) ->
Iflist = case inet:getifaddrs() of
{ok, IFL} ->
IFL;
{error, Reason} ->
throw({inet,getifaddrs,Reason})
end,
which_addr(Domain, Iflist).
which_addr(_Domain, []) ->
throw(no_address);
which_addr(Domain, [{Name, IFO}|_IFL]) when (Name =/= "lo") ->
which_addr2(Domain, IFO);
which_addr(Domain, [_|IFL]) ->
which_addr(Domain, IFL).
which_addr2(inet = _Domain, [{addr, Addr}|_IFO]) when (size(Addr) =:= 4) ->
Addr;
which_addr2(inet6 = _Domain, [{addr, Addr}|_IFO]) when (size(Addr) =:= 8) ->
Addr;
which_addr2(Domain, [_|IFO]) ->
which_addr2(Domain, IFO).
accept_loop(LSock) ->
put(sname, "accept-loop"),
accept_loop(LSock, []).
accept_loop(LSock, Handlers) ->
i("try accept"),
case socket:accept(LSock, infinity) of
{ok, Sock} ->
i("accepted: ~p", [Sock]),
case handle_accept_success(Sock) of
{ok, Handler} ->
accept_loop(LSock, [Handler|Handlers]);
{error, HReason} ->
e("Failed starting handler: "
"~n ~p", [HReason]),
socket:close(Sock),
exit({failed_starting_handler, HReason})
end;
{error, Reason} ->
e("accept failure: "
"~n ~p", [Reason]),
exit({accept, Reason})
end.
handle_accept_success(Sock) ->
Self = self(),
Handler = spawn_link(fun() -> handler_init(Self, Sock) end),
case socket:setopt(Sock, otp, controlling_process, Handler) of
ok ->
%% Normally we should have a msgs collection here
%% (of messages we receive before the control was
%% handled over to Handler), but since we don't
%% have active implemented yet...
handler_continue(Handler),
{ok, {Handler, Sock}};
{error, _} = ERROR ->
exit(Handler, kill),
ERROR
end.
handler_init(Parent, Socket) ->
put(sname, "handler"),
receive
{handler, Parent, continue} ->
handler_loop(#handler{parent = Parent,
socket = Socket})
end.
handler_continue(Handler) ->
Handler ! {handler, self(), continue}.
handler_loop(#handler{socket = Socket} = H) ->
case socket:read(Socket, 0) of
{ok, <<0:32, N:32, ReqData/binary>>} ->
i("received request ~w: "
"~n ~p", [N, ReqData]),
Reply = <<1:32, N:32, ReqData/binary>>,
case socket:send(Socket, Reply) of
ok ->
i("successfully sent reply ~w", [N]),
handler_loop(H);
{error, SReason} ->
e("failed sending reply ~w:"
"~n ~p", [N, SReason]),
exit({failed_sending_reply, SReason})
end;
{error, RReason} ->
e("failed reading request: "
"~n ~p", [RReason]),
exit({failed_sending_reply, RReason})
end.
e(F, A) ->
p("<ERROR> " ++ F, A).
i(F) ->
i(F, []).
i(F, A) ->
p("*** " ++ F, A).
p(F, A) ->
p(get(sname), F, A).
p(SName, F, A) ->
io:format("[server,~s] " ++ F ++ "~n", [SName|A]).
|