-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDartMud.dart
54 lines (43 loc) · 1.3 KB
/
DartMud.dart
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
#library("dartmud:server");
#import("dart:io");
#import('Connection.dart');
#import("lib/Mudlib.dart");
/**
* ServerManager class opens server connection, creates a Connection
* and hands it off to the mudlib.
*/
class ServerManager {
ServerSocket _listenSocket;
Mudlib _mudlib;
/** Create a new ServerSocket to listen on. Throws error if fails */
ServerManager() {
_mudlib = new Mudlib(this);
_listenSocket = new ServerSocket("127.0.0.1", 5700, 0);
print("DartMud server now running on 127.0.0.1 port 5700");
if(_listenSocket == null) {
throw "Error: Unable to open Server Socket";
}
_listenSocket.onConnection = this._handleConn;
_listenSocket.onError = (Exception e) {
print("Error occured with ServerSocket!:");
print(e);
};
}
/**
* On connection to ServerSocket, creates a new Connection object
* and hands it off to the mudlib. Prints connection log to console.
*/
void _handleConn(Socket sock) {
Connection conn = new Connection(sock);
_mudlib.login(conn);
print("Connection from: ${sock.remoteHost}:${sock.remotePort}");
}
/** Shutdown the server. */
void shutdown() {
print("Stopping server!");
_listenSocket.close();
}
}
void main() {
ServerManager server = new ServerManager();
}