Lobby2Server_PGSQL.cpp 3.3 KB

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