Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / net / login_cookie.cpp
blob09de7c8a2e57966f179c7a654d06af0d031252b7
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdnet.h"
19 #include "nel/net/login_cookie.h"
21 using namespace std;
22 using namespace NLMISC;
25 namespace NLNET {
29 * Comparison == operator
31 bool operator== (const CLoginCookie &c1, const CLoginCookie &c2)
33 nlassert (c1._Valid && c2._Valid);
35 return c1._UserAddr==c2._UserAddr && c1._UserKey==c2._UserKey && c1._UserId==c2._UserId;
39 * Comparison != operator
41 bool operator!= (const CLoginCookie &c1, const CLoginCookie &c2)
43 return !(c1 == c2);
46 CLoginCookie::CLoginCookie (uint32 addr, uint32 id) : _Valid(true), _UserAddr(addr), _UserId(id)
48 // generates the key for this cookie
49 _UserKey = generateKey();
52 uint32 CLoginCookie::generateKey()
54 uint32 t = (uint32)time (NULL);
55 srand (t);
57 uint32 r = rand ();
58 static uint32 n = 0;
59 n++;
61 // 12bits for the time (in second) => loop in 1 hour
62 // 8bits for random => 256 case
63 // 12bits for the inc number => can generate 4096 keys per second without any problem (if you generate more than this number, you could have 2 same keys)
64 return (t&0xFFF)<<20 | (r&0xFF)<<12 | (n&0xFFF);
66 // 12bits for the time (in second) => loop in 1 hour
67 // 20bits for the inc number => can generate more than 1 million keys per second without any problem (never exceed on my computer)
68 // return (t&0xFFF)<<20 | (n&0xFFFFF);
72 /* test key generation
73 void main()
75 set<uint32> myset;
77 // generates the key for this cookie
78 for(;;)
80 uint32 val = (t&0xFFF)<<20 | (r&0xFF)<<12 | (n&0xFFF);
81 pair<set<uint32>::iterator,bool> p = myset.insert (val);
82 if (!p.second) printf("%10u 0x%x already inserted\n", val, val);
87 } // NL.