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.tcpclient;
26 
27 import std.regex;
28 import std.socket;
29 import std..string;
30 import std.conv;
31 import std.bitmanip;
32 import core.thread;
33 
34 import core.stream;
35 
36 TcpSocket connectToServer(string address, ushort port) {
37 	TcpSocket socket = new TcpSocket;
38 	Address[] addresses = getAddress(address, port);
39 	socket.blocking = true;
40 	if(addresses.length == 0)
41 		return null;
42 	foreach(Address addr; addresses) {
43 		if(matchFirst(addr.toAddrString(), `\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}`)) {
44 			socket.connect(addr);
45 			if(socket.isAlive) {
46 				return socket;
47 			}
48 		}
49 	}
50 	return null;
51 }
52 
53 abstract class TcpClient: Thread {
54 	protected {
55 		Address[] _addresses; //Todo: Fetch this from socket
56 		TcpSocket _socket;
57 	}
58 
59 	@property {
60 		string address() const { return _addresses.length ? _addresses[0].toAddrString() : ""; }
61 		string port() const { return _addresses.length ? _addresses[0].toPortString() : ""; }
62 	}
63 
64 	this(TcpSocket socket) {
65 		super(&run);
66 		_socket = socket;
67 	}
68 
69 	~this() {
70 		close();
71 	}
72 
73 	void close() {
74 		if(_socket is null)
75 			return;
76 		_socket.shutdown(SocketShutdown.BOTH);
77 		_socket.close();
78 		_socket = null;
79 	}
80 
81 	bool send(string message) {
82 		char[] buffer = message.dup;
83 		bool isValid = (message.length == _socket.send(buffer)); 
84 		return isValid;
85 	}
86 
87 	bool receive(ref string message, uint size) {
88 		char[] buffer = new char[size];
89 		bool isValid = (size == _socket.receive(buffer));
90 		message = to!string(buffer);
91 		return isValid;
92 	}
93 
94 	bool send(OutStream stream) {
95 		ubyte[ushort.sizeof] sizeBuffer = nativeToBigEndian!ushort(cast(ushort)stream.length);
96 		bool isValid = (ushort.sizeof == _socket.send(sizeBuffer));
97 		if(!isValid)
98 			return isValid;
99 		isValid = (stream.length == _socket.send(stream.data));
100 		return isValid;
101 	}
102 
103 	bool receive(ref InStream stream) {
104 		ubyte[ushort.sizeof] sizeBuffer;
105 		bool isValid = (ushort.sizeof == _socket.receive(sizeBuffer));
106 		if(!isValid)
107 			return isValid;
108 		ushort size = bigEndianToNative!ushort(sizeBuffer);
109 		stream.length = size;
110 		isValid = (size == _socket.receive(stream.data));
111 		return isValid;
112 	}
113 
114 	abstract protected void run();
115 }