1 // NeLNS - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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"
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"
46 using namespace NLMISC
;
47 using namespace NLNET
;
54 CBufServer
*WebServer
= NULL
;
56 // uint32 is the hostid to the web connection
57 map
<uint32
, CLoginCookie
> TempCookies
;
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
77 msgin
.serial (reason
);
78 msgout
.serial (reason
);
81 msgin
.serial (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");
95 string str
= cookie
.setToString ();
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);
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
)
135 string userName
, userPriv
, userExtended
;
136 msgin
.serial (shardId
);
137 msgin
.serial (userId
);
138 msgin
.serial (userName
);
142 msgin
.serial (userPriv
);
146 nlwarning ("Web didn't give me the user privilege for user '%s', set to empty", userName
.c_str());
151 msgin
.serial (userExtended
);
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
);
161 for (i
= 0; i
< Shards
.size (); i
++)
163 if (Shards
[i
].ShardId
== shardId
)
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
));
182 // the shard is not available, denied the user
183 nlwarning("ShardId %d is not available, can't add the userid %d", shardId
, userId
);
188 string reason
= "Selected shard is not available";
189 msgout
.serial (reason
);
190 WebServer
->send (msgout
, host
);
193 void cbDisconnectClient (CMemStream
&msgin
, TSockId host
)
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
215 msgout2
.serial(fake
);
217 msgout2
.serial (reason
);
218 WebServer
->send (msgout2
, host
);
223 nlwarning("ShardId %d is not available, can't disconnect the userid %d", shardId
, userId
);
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
,
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);
273 uint8 messageType
= 0;
277 WebServer
->receive (msgin
, &host
);
281 msgin
.serial (messageType
);
285 nlwarning ("Error during receiving: '%s'", e
.what ());
288 if(messageType
<sizeof(WebCallbackArray
)/sizeof(WebCallbackArray
[0]))
290 WebCallbackArray
[messageType
](msgin
, host
);
294 nlwarning ("Received an unknown message type %d from web server", messageType
);
300 nlwarning ("Error during update: '%s'", e
.what ());
304 void connectionWebRelease ()
306 nlassert(WebServer
!= NULL
);