1 #include "Lobby2Server_PGSQL.h"
2 #include "PostgreSQLInterface.h"
3
4 using namespace RakNet;
5
Lobby2ServerWorkerThread(Lobby2ServerCommand input,bool * returnOutput,void * perThreadData)6 Lobby2ServerCommand Lobby2ServerWorkerThread(Lobby2ServerCommand input, bool *returnOutput, void* perThreadData)
7 {
8 PostgreSQLInterface *postgreSQLInterface = (PostgreSQLInterface *) perThreadData;
9 input.returnToSender = input.lobby2Message->ServerDBImpl(&input, postgreSQLInterface);
10 *returnOutput=input.returnToSender;
11 if (input.deallocMsgWhenDone && input.returnToSender==false)
12 RakNet::OP_DELETE(input.lobby2Message, __FILE__, __LINE__);
13 return input;
14 }
15
Lobby2Server_PGSQL()16 Lobby2Server_PGSQL::Lobby2Server_PGSQL()
17 {
18 }
~Lobby2Server_PGSQL()19 Lobby2Server_PGSQL::~Lobby2Server_PGSQL()
20 {
21 Clear();
22 }
AddInputFromThread(Lobby2Message * msg,unsigned int targetUserId,RakNet::RakString targetUserHandle)23 void Lobby2Server_PGSQL::AddInputFromThread(Lobby2Message *msg, unsigned int targetUserId, RakNet::RakString targetUserHandle)
24 {
25 Lobby2ServerCommand command;
26 command.lobby2Message=msg;
27 command.deallocMsgWhenDone=true;
28 command.returnToSender=true;
29 command.callerUserId=targetUserId;
30 command.callingUserName=targetUserHandle;
31 command.server=this;
32 AddInputCommand(command);
33 }
AddInputCommand(Lobby2ServerCommand command)34 void Lobby2Server_PGSQL::AddInputCommand(Lobby2ServerCommand command)
35 {
36 threadPool.AddInput(Lobby2ServerWorkerThread, command);
37 }
AddOutputFromThread(Lobby2Message * msg,unsigned int targetUserId,RakNet::RakString targetUserHandle)38 void Lobby2Server_PGSQL::AddOutputFromThread(Lobby2Message *msg, unsigned int targetUserId, RakNet::RakString targetUserHandle)
39 {
40 Lobby2ServerCommand command;
41 command.lobby2Message=msg;
42 command.deallocMsgWhenDone=true;
43 command.returnToSender=true;
44 command.callerUserId=targetUserId;
45 command.callingUserName=targetUserHandle;
46 command.server=this;
47 msg->resultCode=L2RC_SUCCESS;
48 threadPool.AddOutput(command);
49 }
ConnectToDB(const char * conninfo,int numWorkerThreads)50 bool Lobby2Server_PGSQL::ConnectToDB(const char *conninfo, int numWorkerThreads)
51 {
52 if (numWorkerThreads<=0)
53 return false;
54
55 StopThreads();
56
57 int i;
58 PostgreSQLInterface *connection;
59 for (i=0; i < numWorkerThreads; i++)
60 {
61 connection = RakNet::OP_NEW<PostgreSQLInterface>( __FILE__, __LINE__ );
62 if (connection->Connect(conninfo)==false)
63 {
64 RakNet::OP_DELETE(connection, __FILE__, __LINE__);
65 ClearConnections();
66 return false;
67 }
68 connectionPoolMutex.Lock();
69 connectionPool.Insert(connection, __FILE__, __LINE__ );
70 connectionPoolMutex.Unlock();
71 }
72
73 threadPool.SetThreadDataInterface(this,0);
74 threadPool.StartThreads(numWorkerThreads,0,0,0);
75 return true;
76 }
PerThreadFactory(void * context)77 void* Lobby2Server_PGSQL::PerThreadFactory(void *context)
78 {
79 (void)context;
80
81 PostgreSQLInterface* p;
82 connectionPoolMutex.Lock();
83 p=connectionPool.Pop();
84 connectionPoolMutex.Unlock();
85 return p;
86 }
PerThreadDestructor(void * factoryResult,void * context)87 void Lobby2Server_PGSQL::PerThreadDestructor(void* factoryResult, void *context)
88 {
89 (void)context;
90
91 PostgreSQLInterface* p = (PostgreSQLInterface*)factoryResult;
92 RakNet::OP_DELETE(p, __FILE__, __LINE__);
93 }
ClearConnections(void)94 void Lobby2Server_PGSQL::ClearConnections(void)
95 {
96 unsigned int i;
97 connectionPoolMutex.Lock();
98 for (i=0; i < connectionPool.Size(); i++)
99 RakNet::OP_DELETE(connectionPool[i], __FILE__, __LINE__);
100 connectionPool.Clear(false, __FILE__, __LINE__);
101 connectionPoolMutex.Unlock();
102 }
103