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.client;
26 
27 import std.stdio;
28 import std.socket;
29 import core.time: dur;
30 
31 import core.all;
32 import net.tcpclient;
33 import net.server;
34 import net.event;
35 
36 shared ClientInfo[] localClientsInfo;
37 shared bool hasLocalClientsInfoChanged;
38 
39 private {
40 	__gshared RingBuffer!OutStream clientSendBuffer;
41 	__gshared RingBuffer!InStream clientReceiveBuffer;
42 	Client client;
43 }
44 
45 void startClient(TcpSocket socket) {
46 	if(client !is null && client.isWorking)
47 		throw new Exception("Client already running");
48 	client = new Client(socket);
49 	client.start();
50 }
51 
52 void closeClient() {
53 	if(client is null)
54 		throw new Exception("Could not close the client");
55 	client.isWorking = false;
56 }
57 
58 bool isClientWorking() {
59 	return (client !is null && client.isWorking);
60 }
61 
62 void clientSend(OutStream stream) {
63 	clientSendBuffer.write(stream);
64 }
65 
66 InStream clientReceive() {
67 	return clientReceiveBuffer.read();
68 }
69 
70 bool clientHasPendingMessage() {
71 	return !clientReceiveBuffer.isEmpty;
72 }
73 
74 class Client: TcpClient {
75 	shared bool isWorking = true;
76 	bool hasJustConnected = true;
77 
78 	this(TcpSocket socket) {
79 		super(socket);
80 		clientSendBuffer = new RingBuffer!OutStream;
81 		clientReceiveBuffer = new RingBuffer!InStream;
82 	}
83 
84 	override void run() {
85 		try {
86 			SocketSet readSet = new SocketSet(1u);
87 			SocketSet writeSet = new SocketSet(1u);
88 			SocketSet errorSet = new SocketSet(1u);
89 
90 			while(isWorking) {
91 				readSet.reset();
92 				writeSet.reset();
93 				errorSet.reset();
94 				
95 				readSet.add(_socket);
96 				writeSet.add(_socket);
97 				errorSet.add(_socket);
98 
99 				Socket.select(readSet, writeSet, errorSet, dur!"seconds"(1));
100 
101 				if(errorSet.isSet(_socket))
102 					isWorking = false;
103 				else if(readSet.isSet(_socket) && !clientReceiveBuffer.isFull) {
104 					auto inStream = new InStream;
105 					if(receive(inStream)) {
106 						if(hasJustConnected) {
107 							hasJustConnected = false;
108 							if(inStream.read!NetEvent() == NetEvent.Disconnected)
109 								isWorking = false;
110 						}
111 						else
112 							clientReceiveBuffer.write(inStream);
113 					}
114 					else
115 						isWorking = false;
116 				}
117 				else if(writeSet.isSet(_socket) && !clientSendBuffer.isEmpty) {
118 					auto outStream = clientSendBuffer.read();
119 					if(!send(outStream))
120 						isWorking = false;
121 				}
122 			}
123 			close();
124 		}
125 		catch(Exception e) {
126 			writeln(e.msg);
127 			close();
128 		}
129 	}
130 }