EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClientSession.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Update: Allan
25 */
26 
27 #include "ClientSession.h"
28 #include "EntityList.h"
29 #include "EVEServerConfig.h"
30 
39 static std::map<std::string, bool> NONPERSISTVARS = {
40  {"clientID", true},
41  {"sessionID", true},
42  {"sid", true}
43 };
44 
45 
47 : mSession(new PyDict()),
48 mDirty(false),
49 m_sessionID(0)
50 {
51  /* default session values */
53  mSession->SetItemString("userid", new_tuple(PyStatic.NewNone(), PyStatic.NewZero(), PyStatic.NewFalse()));
54  mSession->SetItemString("address", new_tuple(PyStatic.NewNone(), new PyString("0.0.0.0"), PyStatic.NewFalse()));
55 
56  /* session id is unique to each session.
57  * is not saved or shared between chars
58  */
59  //random.getrandbits(63)
61  sEntityList.RegisterSID(m_sessionID);
62 }
63 
65 {
66  // do we clear session vars here, or let ~PyDict() do it?
68  sEntityList.RemoveSID(m_sessionID);
69 }
70 
71 // note: cannot destroy these Py* objects here.
72 void ClientSession::Clear(const char* name)
73 {
74  _Set(name, PyStatic.NewNone());
75 }
76 
77 void ClientSession::SetInt(const char* name, int32 value)
78 {
79  _Set(name, new PyInt(value));
80 }
81 
82 void ClientSession::SetLong(const char* name, int64 value)
83 {
84  _Set(name, new PyLong(value));
85 }
86 
87 void ClientSession::SetString(const char* name, const char* value)
88 {
89  _Set(name, new PyString(value));
90 }
91 
92 int32 ClientSession::GetLastInt(const char* name) const
93 {
94  return PyRep::IntegerValue(_GetLast(name));
95 }
96 
97 int32 ClientSession::GetCurrentInt(const char* name) const
98 {
99  return PyRep::IntegerValue(_GetCurrent(name));
100 }
101 
102 int64 ClientSession::GetLastLong(const char* name) const
103 {
104  return PyRep::IntegerValue(_GetLast(name));
105 }
106 
107 int64 ClientSession::GetCurrentLong(const char* name) const
108 {
109  return PyRep::IntegerValue(_GetCurrent(name));
110 }
111 
112 std::string ClientSession::GetLastString(const char* name) const
113 {
114  return PyRep::StringContent(_GetLast(name));
115 }
116 
117 std::string ClientSession::GetCurrentString(const char* name) const
118 {
119  return PyRep::StringContent(_GetCurrent(name));
120 }
121 
123 {
124  if (!mDirty)
125  return;
126 
127  for (auto cur : *mSession)
128  {
129  PyTuple* valueTuple = cur.second->AsTuple ();
130 
131  if (valueTuple->GetItem (2)->AsBool ()->value () == false)
132  continue;
133 
134  // mark the value as not new
135  valueTuple->SetItem (2, PyStatic.NewFalse());
136 
137  // add the value to the list if it should be persisted
138  if (NONPERSISTVARS.find (cur.first->AsString ()->content ()) == NONPERSISTVARS.end ())
139  into->SetItem (cur.first, new_tuple (valueTuple->GetItem (0), valueTuple->GetItem(1)));
140  }
141 
142  mDirty = false;
143 }
144 
146 {
147  for (auto cur : *mSession)
148  {
149  PyTuple* valueTuple = cur.second->AsTuple ();
150 
151  valueTuple->SetItem(2, PyStatic.NewFalse());
152 
153  // add the value to the initial state only if required
154  if (NONPERSISTVARS.find (cur.first->AsString ()->content ()) == NONPERSISTVARS.end ())
155  into->SetItem (cur.first, cur.second->AsTuple ()->GetItem (1));
156  }
157 
158  // mark the session as not dirty
159  mDirty = false;
160 }
161 
162 PyTuple* ClientSession::_GetValueTuple(const char* name) const
163 {
164  PyRep* value(mSession->GetItemString(name));
165  if (value == nullptr)
166  return nullptr;
167  return value->AsTuple();
168 }
169 
170 PyRep* ClientSession::_GetLast(const char* name) const
171 {
172  PyTuple* tuple(_GetValueTuple(name)); // copy c'tor
173  if (tuple == nullptr) {
174  _log(CLIENT__SESSION_NOTFOUND, "ClientSession::_GetLast - value not found with name '%s'", name);
175  return nullptr;
176  }
177  return tuple->GetItem(0);
178 }
179 
180 PyRep* ClientSession::_GetCurrent(const char* name) const
181 {
182  PyTuple* tuple(_GetValueTuple(name)); // copy c'tor
183  if (tuple == nullptr) {
184  if (is_log_enabled(CLIENT__SESSION_NOTFOUND)) {
185  _log(CLIENT__SESSION_NOTFOUND, "ClientSession::_GetCurrent - value not found with name '%s'", name);
186  EvE::traceStack();
187  }
188  return nullptr;
189  }
190  return tuple->GetItem(1);
191 }
192 
193 void ClientSession::_Set(const char* name, PyRep* value)
194 {
195  PyTuple* tuple(_GetValueTuple(name)); // copy c'tor
196  if (tuple == nullptr) {
197  tuple = new_tuple(PyStatic.NewNone(), PyStatic.NewNone(), PyStatic.NewFalse());
198  mSession->SetItemString(name, tuple);
199  }
200 
201  PyRep* current(tuple->GetItem(1)); // copy c'tor
202  if (value->hash() != current->hash()) {
203  tuple->SetItem(0, current); /* didn't the session need to store the old value too? */
204  tuple->SetItem(1, value);
205  tuple->SetItem(2, PyStatic.NewTrue());
206  mDirty = true;
207  } else {
208  PyDecRef(value);
209  }
210 }
std::string GetLastString(const char *name) const
int64 GetLastLong(const char *name) const
Base Python wire object.
Definition: PyRep.h:66
PyTuple * AsTuple()
Definition: PyRep.h:138
std::string GetCurrentString(const char *name) const
static std::string StringContent(PyRep *pRep)
Definition: PyRep.cpp:103
#define _log(type, fmt,...)
Definition: logsys.h:124
PyRep * GetItem(size_t index) const
Returns Python object.
Definition: PyRep.h:602
Python string.
Definition: PyRep.h:430
PyRep * GetItemString(const char *key) const
Obtains database entry based on given key string.
Definition: PyRep.cpp:702
PyBool * AsBool()
Definition: PyRep.h:128
Python's dictionary.
Definition: PyRep.h:719
void SetLong(const char *name, int64 value)
PyTuple * _GetValueTuple(const char *name) const
bool value() const
Definition: PyRep.h:340
#define sEntityList
Definition: EntityList.h:208
Python tuple.
Definition: PyRep.h:567
void Clear(const char *name)
static std::map< std::string, bool > NONPERSISTVARS
signed __int32 int32
Definition: eve-compat.h:49
#define is_log_enabled(type)
Definition: logsys.h:78
double GetTimeUSeconds()
Definition: utils_time.cpp:116
int64 GetCurrentLong(const char *name) const
PyTuple * new_tuple(int64 arg1)
Definition: PyRep.cpp:1160
void SetItem(size_t index, PyRep *object)
Stores Python object.
Definition: PyRep.h:610
Python integer.
Definition: PyRep.h:231
PyRep * _GetLast(const char *name) const
#define PyStatic
Definition: PyRep.h:1209
virtual int32 hash() const
virtual function to generate a hash value of a object.
Definition: PyRep.cpp:96
int32 GetCurrentInt(const char *name) const
#define PyDecRef(op)
Definition: PyRep.h:57
PyRep * _GetCurrent(const char *name) const
signed __int64 int64
Definition: eve-compat.h:51
void SetString(const char *name, const char *value)
PyDict *const mSession
Definition: ClientSession.h:85
void traceStack(void)
Definition: misc.cpp:169
void _Set(const char *name, PyRep *value)
int32 GetLastInt(const char *name) const
static int64 IntegerValue(PyRep *pRep)
Definition: PyRep.cpp:118
void SetInt(const char *name, int32 value)
void SetItem(PyRep *key, PyRep *value)
SetItem adds or sets a database entry.
Definition: PyRep.cpp:713
void EncodeChanges(PyDict *into)
void SetItemString(const char *key, PyRep *value)
SetItemString adds or sets a database entry.
Definition: PyRep.h:812
void EncodeInitialState(PyDict *into)
Python long integer.
Definition: PyRep.h:261