-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmserver.erl
72 lines (61 loc) · 2.42 KB
/
mserver.erl
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
%% @author M Wright <[email protected]>
%% @copyright Wright, MJ 2014-2015
%% @version 2.2
%% @title Server Manager
%% @doc Server Manager spawns new client handler for each connecting client.
%% It supervisors client manager.
%% @end
-module(mserver).
-define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr, true}]).
-define(TCP_OPTIONS2,[binary, {packet, 0}, {active, false}, {reuseaddr, true}]).
-export([start_link/1]).
-export([st/0]).
-include("recs.hrl").
%%entry point for server to start, on any point defined
%%Start link for supervisor
start_link(Port)->start(Port).
%%Default start
st()->start(1024).
%%Primary starting method
start(Port)->
io:fwrite("Server attempting startup... ~n"),
random:seed(now()),
case gen_tcp:listen(Port, ?TCP_OPTIONS) of
{ok, LSocket}->
io:fwrite("Server started on port: ~p~n",[Port]),
Rooms = room:st(),
io:fwrite("Dungeon has been loaded succesfully!~n"),
io:fwrite("Starting Client Manager...~n"),
Pid = spawn(fun() -> clientm:manage_clients([],Rooms) end),
register(client_manager, Pid),%register process to clientmangager atom
io:fwrite("Client manager started!~n"),
io:fwrite("Starting event engine...~n"),
Eid = spawn(fun() -> eventai:handle_events([],10000) end),
register(event_handler, Eid),
io:fwrite("Event engine started!~n"),
io:fwrite("Waiting for incoming connections...~n"),
accept_connection(LSocket);
{error, Reason}->io:fwrite("Failed to start server: ~s~n", [Reason])
end.
%%accepting client connections
accept_connection(LSocket) ->
case gen_tcp:accept(LSocket) of%wait for next client to join
{ok, Socket} ->
spawn(fun() -> handle_client(Socket) end),%handle arbitrary no clients
client_manager ! {connect, Socket};%send new client connected
{error, Reason} -> io:fwrite("Socket accept error: ~s~n", [Reason])
end,
accept_connection(LSocket).%infinite loop
%%Handle client messages and send them to client manager
handle_client(Socket) ->%handles data recieved and for disconnects of clients
case gen_tcp:recv(Socket, 0) of
{ok, Data}->
client_manager ! {data, Socket, Data},
handle_client(Socket);
{error, closed}->
client_manager ! {disconnect, Socket};
{error, Reason} ->
client_manager ! {error, Reason};
{badarg, Reason}->
client_manager ! {error, Reason}
end.