SmartFoxServer 2X C++ API
ThreadManager.h
1 // ===================================================================
2 //
3 // Description
4 // Contains the definition of thread manager
5 //
6 // Revision history
7 // Date Description
8 // 30-Nov-2012 First version
9 //
10 // ===================================================================
11 #ifndef __ThreadManager__
12 #define __ThreadManager__
13 
14 #include "../Util/DelegateOneArgument.h" // Delegate with one parameter
15 #include "../Util/DelegateThreeArguments.h" // Delegate with three parameter
16 #include "../Core/Sockets/ISocketLayer.h" // ISocketLayer interface
17 
18 #include "../Core/PacketHeader.h"
19 #include "../Util/ByteArray.h"
20 
21 #include <boost/thread.hpp> // Boost thread
22 #include <boost/shared_ptr.hpp> // Boost Asio shared pointer
23 
24 #if defined(_MSC_VER)
25 #pragma warning(disable:4786) // STL library: disable warning 4786; this warning is generated due to a Microsoft bug
26 #endif
27 #include <string> // STL library: string object
28 #include <map> // STL library: map object
29 #include <vector> // STL library: vector object
30 #include <list> // STL library: list object
31 using namespace std; // STL library: declare the STL namespace
32 
33 using namespace Sfs2X::Core::Sockets;
34 using namespace Sfs2X::Util;
35 
36 namespace Sfs2X {
37 namespace Core {
38 
39  // -------------------------------------------------------------------
40  // Definition of delegates specific for the thread manager
41  // -------------------------------------------------------------------
42  typedef DelegateOneArgument<boost::shared_ptr<void> > ParameterizedThreadStart;
43  typedef DelegateThreeArguments<boost::shared_ptr<PacketHeader>, boost::shared_ptr<ByteArray>, bool> WriteBinaryDataDelegate;
44 
45  // -------------------------------------------------------------------
46  // Class ThreadManager
47  // -------------------------------------------------------------------
48  class ThreadManager
49  {
50  public:
51 
52  // -------------------------------------------------------------------
53  // Public methods
54  // -------------------------------------------------------------------
55 
56  ThreadManager();
57  ~ThreadManager();
58 
59  void EnqueueCustom(boost::shared_ptr<ParameterizedThreadStart> callback, boost::shared_ptr<std::map<string, boost::shared_ptr<void> > > data);
60  void EnqueueDataCall(boost::shared_ptr<OnDataDelegate> callback, boost::shared_ptr<vector<unsigned char> > data);
61  void EnqueueSend(boost::shared_ptr<WriteBinaryDataDelegate> callback, boost::shared_ptr<PacketHeader> header, boost::shared_ptr<ByteArray> data, boost::shared_ptr<bool> udp);
62 
63  // -------------------------------------------------------------------
64  // Public members
65  // -------------------------------------------------------------------
66 
67  void Start();
68 
69  protected:
70 
71  // -------------------------------------------------------------------
72  // Protected methods
73  // -------------------------------------------------------------------
74 
75  // -------------------------------------------------------------------
76  // Protected members
77  // -------------------------------------------------------------------
78 
79  private:
80 
81  // -------------------------------------------------------------------
82  // Private methods
83  // -------------------------------------------------------------------
84 
85  void InThread();
86  void OutThread();
87  void ProcessOutItem(boost::shared_ptr<map<string, boost::shared_ptr<void> > > item);
88  void ProcessItem(boost::shared_ptr<map<string, boost::shared_ptr<void> > > item);
89 
90  // -------------------------------------------------------------------
91  // Private members
92  // -------------------------------------------------------------------
93 
94  bool running;
95 
96  boost::shared_ptr<boost::thread> inThread;
97  boost::shared_ptr<list<boost::shared_ptr<map<string, boost::shared_ptr<void> > > > > inThreadQueue;
98  boost::mutex inQueueLocker;
99 
100  boost::shared_ptr<boost::thread> outThread;
101  boost::shared_ptr<list<boost::shared_ptr<map<string, boost::shared_ptr<void> > > > > outThreadQueue;
102  boost::mutex outQueueLocker;
103  };
104 
105 } // namespace Core
106 } // namespace Sfs2X
107 
108 #endif
STL namespace.
Definition: IPAddress.cpp:15
Definition: BuddyOnlineState.h:15
Definition: SmartFox.cpp:24