NetworkWorkerThread.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file NetworkWorkerThread.h
  13. @brief The NetworkWorkerThread class. Implements a background thread for responsive
  14. processing of server and client connections. */
  15. #include "SharedPtr.h"
  16. #include "Lockable.h"
  17. #include "MessageConnection.h"
  18. #include "NetworkServer.h"
  19. #include "Thread.h"
  20. namespace kNet
  21. {
  22. class NetworkWorkerThread
  23. {
  24. public:
  25. NetworkWorkerThread();
  26. void AddConnection(MessageConnection *connection);
  27. void RemoveConnection(MessageConnection *connection);
  28. void AddServer(NetworkServer *server);
  29. void RemoveServer(NetworkServer *server);
  30. void StartThread();
  31. void StopThread();
  32. int NumConnections() const;
  33. int NumServers() const;
  34. Thread &ThreadObject() { return workThread; }
  35. private:
  36. Lockable<std::vector<MessageConnection *> > connections;
  37. Lockable<std::vector<NetworkServer *> > servers;
  38. Thread workThread;
  39. /// The entry point for the work thread, which runs a loop that manages network connections.
  40. void MainLoop();
  41. };
  42. #ifdef WIN32
  43. DWORD WINAPI NetworkWorkerThreadMain(LPVOID lpParameter);
  44. #endif
  45. } // ~kNet