1 /**
2 Grimoire
3 Copyright (c) 2017 Enalye
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising
7 from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute
11 it freely, subject to the following restrictions:
12 
13 	1. The origin of this software must not be misrepresented;
14 	   you must not claim that you wrote the original software.
15 	   If you use this software in a product, an acknowledgment
16 	   in the product documentation would be appreciated but
17 	   is not required.
18 
19 	2. Altered source versions must be plainly marked as such,
20 	   and must not be misrepresented as being the original software.
21 
22 	3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 module net.tcpserver;
26 
27 import core.thread;
28 import std.socket;
29 import std.bitmanip;
30 import std.conv;
31 
32 import core.all;
33 
34 abstract class TcpServer: Thread {
35 	protected {
36 		TcpSocket _socket;
37 		Address _address;
38 	}
39 
40 	@property {
41 		string address() const { return _address.toAddrString(); }
42 		string port() const { return _address.toPortString(); }
43 	}
44 
45 	this() {
46 		super(&run);
47 		_socket = new TcpSocket;
48 		_socket.blocking = true;
49 	}
50 
51 	~this() {
52 		close();
53 	}
54 
55 	void open(ushort listenPort) {
56 		if(_socket is null)
57 			return;
58 		_address = new InternetAddress(listenPort);
59 		_socket.bind(_address);
60 		_socket.listen(10);
61 		if(!_socket.isAlive())
62 			throw new Exception("Could not start the server.");
63 	}
64 
65 	void close() {
66 		if(_socket is null)
67 			return;
68 		_socket.shutdown(SocketShutdown.BOTH);
69 		_socket.close();
70 		_socket = null;
71 	}
72 
73 	Socket accept() {
74 		if(_socket is null)
75 			throw new Exception("Server socket not initialized");
76 		return _socket.accept();
77 	}
78 
79 	bool send(Socket clientSocket, string message) {
80 		char[] buffer = message.dup;
81 		bool isValid = (message.length == clientSocket.send(buffer));
82 		return isValid;
83 	}
84 
85 	bool receive(Socket clientSocket, ref string message, uint size) {
86 		char[] buffer = new char[size];
87 		bool isValid = (size == clientSocket.receive(buffer));
88 		message = to!string(buffer);
89 		return isValid;
90 	}
91 
92 	bool send(Socket clientSocket, OutStream stream) {
93 		ubyte[ushort.sizeof] sizeBuffer = nativeToBigEndian!ushort(cast(ushort)stream.length);
94 		bool isValid = (ushort.sizeof == clientSocket.send(sizeBuffer));
95 		if(!isValid)
96 			return isValid;
97 		isValid = (stream.length == clientSocket.send(stream.data));
98 		return isValid;
99 	}
100 
101 	bool receive(Socket clientSocket, ref InStream stream) {
102 		ubyte[ushort.sizeof] sizeBuffer;
103 		bool isValid = (ushort.sizeof == clientSocket.receive(sizeBuffer));
104 		if(!isValid)
105 			return isValid;
106 		ushort size = bigEndianToNative!ushort(sizeBuffer);
107 		stream.length = size;
108 		isValid = (size == clientSocket.receive(stream.data));
109 		return isValid;
110 	}
111 
112 	abstract protected void run();
113 }