Linux multi-monitor fullscreen support
[ryzomcore.git] / nelns / login_service / connection_web.cpp
blobe8008f1382c7a8b3b0a2e145f1cd9f8ef84ddee9
1 // NeLNS - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nel/misc/types_nl.h"
22 #include <cstdio>
23 #include <ctype.h>
24 #include <cmath>
26 #include <vector>
27 #include <map>
29 #include "nel/misc/debug.h"
30 #include "nel/misc/config_file.h"
31 #include "nel/misc/displayer.h"
32 #include "nel/misc/log.h"
34 #include "nel/net/buf_server.h"
35 #include "nel/net/login_cookie.h"
37 #include "login_service.h"
38 #include "connection_ws.h"
42 // Namespaces
45 using namespace std;
46 using namespace NLMISC;
47 using namespace NLNET;
51 // Variables
54 CBufServer *WebServer = NULL;
56 // uint32 is the hostid to the web connection
57 map<uint32, CLoginCookie> TempCookies;
61 // Callbacks
64 static void cbWSShardChooseShard/* (CMessage &msgin, TSockId from, CCallbackNetBase &netbase)*/ (CMessage &msgin, const std::string &serviceName, TServiceId sid)
66 nlassert(WebServer != NULL);
69 // S10: receive "SCS" message from WS
72 CMemStream msgout;
73 uint32 fake = 0;
74 msgout.serial(fake);
76 string reason;
77 msgin.serial (reason);
78 msgout.serial (reason);
80 CLoginCookie cookie;
81 msgin.serial (cookie);
83 // search the cookie
84 map<uint32, CLoginCookie>::iterator it = TempCookies.find (cookie.getUserAddr ());
86 if (it == TempCookies.end ())
88 // not found in TempCookies, can't do anything
89 nlwarning ("Receive an answer from welcome service but no connection waiting");
90 return;
93 if (reason.empty())
95 string str = cookie.setToString ();
96 msgout.serial (str);
98 string addr;
99 msgin.serial (addr);
100 msgout.serial (addr);
102 // MTR: No longer sent by WS?
103 //uint32 nbPendingUser;
104 //msgin.serial(nbPendingUser);
106 // read patch addresses sent by WS
108 // OBSOLETE: web doesn't read incoming patching URLs any longer, but them directly from database
109 std::string patchURLS;
112 msgin.serial(patchURLS);
114 catch (Exception&)
116 patchURLS.clear();
119 msgout.serial(patchURLS);
123 WebServer->send (msgout, (TSockId)cookie.getUserAddr ()); // FIXME: 64-bit
126 static const TUnifiedCallbackItem WSCallbackArray[] =
128 { "SCS", cbWSShardChooseShard },
131 void cbAskClientConnection (CMemStream &msgin, TSockId host)
133 sint32 shardId;
134 uint32 userId;
135 string userName, userPriv, userExtended;
136 msgin.serial (shardId);
137 msgin.serial (userId);
138 msgin.serial (userName);
142 msgin.serial (userPriv);
144 catch (Exception &)
146 nlwarning ("Web didn't give me the user privilege for user '%s', set to empty", userName.c_str());
151 msgin.serial (userExtended);
153 catch (Exception &)
155 nlwarning ("Web didn't give me the extended data for user '%s', set to empty", userName.c_str());
158 nlinfo ("Web wants to add userid %d (name '%s' priv '%s' extended '%s') to the shardid %d, send request to the shard", userId, userName.c_str(), userPriv.c_str(), userExtended.c_str(), shardId);
160 uint32 i;
161 for (i = 0; i < Shards.size (); i++)
163 if (Shards[i].ShardId == shardId)
165 // generate a cookie
166 CLoginCookie Cookie ((uint32)(uintptr_t)host, userId);
168 // send message to the welcome service to see if it s ok and know the front end ip
169 CMessage msgout ("CS");
170 msgout.serial (Cookie);
171 msgout.serial (userName, userPriv, userExtended);
172 //WSServer->send (msgout, Shards[i].SockId);
173 CUnifiedNetwork::getInstance ()->send (Shards[i].SId, msgout);
174 beep (1000, 1, 100, 100);
176 // add the connection in temp cookie
177 TempCookies.insert(make_pair(Cookie.getUserAddr(), Cookie));
178 return;
182 // the shard is not available, denied the user
183 nlwarning("ShardId %d is not available, can't add the userid %d", shardId, userId);
185 CMemStream msgout;
186 uint32 fake = 0;
187 msgout.serial(fake);
188 string reason = "Selected shard is not available";
189 msgout.serial (reason);
190 WebServer->send (msgout, host);
193 void cbDisconnectClient (CMemStream &msgin, TSockId host)
195 sint32 shardId;
196 sint32 userId;
197 msgin.serial (shardId);
198 msgin.serial (userId);
200 nlinfo ("Web wants to disconnect userid %d, send request to the shard %d", userId, shardId);
202 for (uint i = 0; i < Shards.size (); i++)
204 if (Shards[i].ShardId == shardId)
206 // ask the WS to disconnect the player from the shard
207 CMessage msgout ("DC");
208 msgout.serial (userId);
209 //WSServer->send (msgout, Shards[i].SockId);
210 CUnifiedNetwork::getInstance ()->send (Shards[i].SId, msgout);
212 // send answer to the web
213 CMemStream msgout2;
214 uint32 fake = 0;
215 msgout2.serial(fake);
216 string reason = "";
217 msgout2.serial (reason);
218 WebServer->send (msgout2, host);
219 return;
223 nlwarning("ShardId %d is not available, can't disconnect the userid %d", shardId, userId);
225 CMemStream msgout;
226 uint32 fake = 0;
227 msgout.serial(fake);
228 string reason = "ShardId "+toString(shardId)+"is not available, can't disconnect the userid"+toString(userId);
229 msgout.serial (reason);
230 WebServer->send (msgout, host);
233 typedef void (*WebCallback)(CMemStream &msgin, TSockId host);
235 WebCallback WebCallbackArray[] = {
236 cbAskClientConnection,
237 cbDisconnectClient
241 // Functions
244 void connectionWebInit ()
246 nlassert(WebServer == NULL);
248 WebServer = new CBufServer ();
249 nlassert(WebServer != NULL);
251 uint16 port = (uint16) IService::getInstance ()->ConfigFile.getVar ("WebPort").asInt();
252 WebServer->init (port);
254 // catch the messages from Welcome Service to know if the user can connect or not
255 CUnifiedNetwork::getInstance ()->addCallbackArray (WSCallbackArray, sizeof(WSCallbackArray)/sizeof(WSCallbackArray[0]));
257 nlinfo ("Set the server connection for web to port %hu", port);
260 void connectionWebUpdate ()
262 nlassert(WebServer != NULL);
266 WebServer->update ();
268 while (WebServer->dataAvailable ())
270 // create a string mem stream to easily communicate with web server
271 NLMISC::CMemStream msgin (true);
272 TSockId host;
273 uint8 messageType = 0;
277 WebServer->receive (msgin, &host);
278 uint32 fake = 0;
279 msgin.serial(fake);
281 msgin.serial (messageType);
283 catch (Exception &e)
285 nlwarning ("Error during receiving: '%s'", e.what ());
288 if(messageType<sizeof(WebCallbackArray)/sizeof(WebCallbackArray[0]))
290 WebCallbackArray[messageType](msgin, host);
292 else
294 nlwarning ("Received an unknown message type %d from web server", messageType);
298 catch (Exception &e)
300 nlwarning ("Error during update: '%s'", e.what ());
304 void connectionWebRelease ()
306 nlassert(WebServer != NULL);
308 delete WebServer;
309 WebServer = 0;