MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
shared_memory.h
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <set>
4 
5 #include "timing.h"
6 #include "defines.h"
7 
8 #if defined(__CYGWIN__) || defined(_WIN32)
9 #include <windows.h>
10 #else
11 #include <semaphore.h>
12 #endif
13 
14 #define STAT_EX_SIZE 175
15 #define PLAY_EX_SIZE 2+6*SIMUL_TRACKS
16 
17 namespace IPC {
18 
20  class statExchange {
21  public:
22  statExchange(char * _data);
23  void now(long long int time);
24  long long int now();
25  void time(long time);
26  long time();
27  void lastSecond(long time);
28  long lastSecond();
29  void down(long long int bytes);
30  long long int down();
31  void up(long long int bytes);
32  long long int up();
33  void host(std::string name);
34  std::string host();
35  void streamName(std::string name);
36  std::string streamName();
37  void connector(std::string name);
38  std::string connector();
39  void crc(unsigned int sum);
40  char getSync();
41  void setSync(char s);
42  unsigned int crc();
43  private:
56  char * data;
57  };
58 
60  class semaphore {
61  public:
62  semaphore();
63  semaphore(const char * name, int oflag, mode_t mode, unsigned int value);
64  ~semaphore();
65  operator bool() const;
66  void open(const char * name, int oflag, mode_t mode = 0, unsigned int value = 0);
67  int getVal() const;
68  void post();
69  void wait();
70  bool tryWait();
71  bool tryWaitOneSecond();
72  void close();
73  void unlink();
74  private:
75 #if defined(__CYGWIN__) || defined(_WIN32)
76  static SECURITY_ATTRIBUTES getSecurityAttributes();
78  HANDLE mySem;
79 #else
80  sem_t * mySem;
81 #endif
82  std::string myName;
83  };
84 
86  class semGuard {
87  public:
88  semGuard(semaphore * thisSemaphore);
89  ~semGuard();
90  private:
93  };
94 
96  class sharedFile {
97  public:
98  sharedFile(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true);
99  sharedFile(const sharedFile & rhs);
100  ~sharedFile();
101  operator bool() const;
102  void init(std::string name_, unsigned int len_, bool master_ = false, bool autoBackoff = true);
103  void operator =(sharedFile & rhs);
104  bool operator < (const sharedFile & rhs) const {
105  return name < rhs.name;
106  }
107  void close();
108  void unmap();
110  int handle;
112  std::string name;
114  long long int len;
116  bool master;
118  char * mapped;
119  };
120 
121 #if defined(__CYGWIN__) || defined(_WIN32)
122  void preservePage(std::string);
123  void releasePage(std::string);
124 #endif
125 
126 #ifdef SHM_ENABLED
127  class sharedPage {
129  public:
130  sharedPage(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true);
131  sharedPage(const sharedPage & rhs);
132  ~sharedPage();
133  operator bool() const;
134  void init(std::string name_, unsigned int len_, bool master_ = false, bool autoBackoff = true);
135  void operator =(sharedPage & rhs);
136  bool operator < (const sharedPage & rhs) const {
137  return name < rhs.name;
138  }
139  void unmap();
140  void close();
141  #if defined(__CYGWIN__) || defined(_WIN32)
142  HANDLE handle;
144  #else
145  int handle;
147  #endif
148  std::string name;
151  long long int len;
153  bool master;
155  char * mapped;
156  };
157 #else
158  class sharedPage: public sharedFile {
161  public:
162  sharedPage(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true);
163  sharedPage(const sharedPage & rhs);
164  ~sharedPage();
165  };
166 #endif
167 
177  class sharedServer {
178  public:
179  sharedServer();
180  sharedServer(std::string name, int len, bool withCounter = false);
181  void init(std::string name, int len, bool withCounter = false);
182  ~sharedServer();
183  void parseEach(void (*callback)(char * data, size_t len, unsigned int id));
184  operator bool() const;
186  unsigned int amount;
187  private:
188  bool isInUse(unsigned int id);
189  void newPage();
190  void deletePage();
192  std::string baseName;
194  unsigned int payLen;
196  std::set<sharedPage> myPages;
201  };
202 
212  class sharedClient {
213  public:
214  sharedClient();
215  sharedClient(const sharedClient & rhs);
216  sharedClient(std::string name, int len, bool withCounter = false);
217  void operator = (const sharedClient & rhs);
218  ~sharedClient();
219  void write(char * data, int len);
220  void finish();
221  void keepAlive();
222  char * getData();
223  private:
225  std::string baseName;
231  int payLen;
236  };
237 
239  public:
240  userConnection(char * _data);
241  unsigned long getTrackId(size_t offset) const;
242  void setTrackId(size_t offset, unsigned long trackId) const;
243  unsigned long getKeynum(size_t offset) const;
244  void setKeynum(size_t offset, unsigned long keynum);
245  private:
246  char * data;
247  };
248 }
long time()
Gets time currently connected.
Definition: shared_memory.cpp:618
A class used as a semaphore guard.
Definition: shared_memory.h:86
semaphore * mySemaphore
The semaphore to guard.
Definition: shared_memory.h:92
char * data
Definition: shared_memory.h:246
A class used for the abstraction of semaphores.
Definition: shared_memory.h:60
long long int up()
Gets the amount of bytes sent.
Definition: shared_memory.cpp:654
semaphore mySemaphore
A semaphore that is locked upon creation and deletion of the page, to ensure no new data is allocated...
Definition: shared_memory.h:198
long lastSecond()
Gets the last viewing second of this user.
Definition: shared_memory.cpp:630
unsigned int amount
The amount of connected clients.
Definition: shared_memory.h:186
int handle
The fd handle of the opened shared file.
Definition: shared_memory.h:110
unsigned int payLen
The length of each consecutive piece of payload.
Definition: shared_memory.h:194
std::set< sharedPage > myPages
The set of sharedPage structures to manage the actual memory.
Definition: shared_memory.h:196
std::string baseName
The basename of the shared pages.
Definition: shared_memory.h:225
The server part of a server/client model for shared memory.
Definition: shared_memory.h:177
bool master
Whether this class should unlink the shared file upon deletion or not.
Definition: shared_memory.h:116
int offsetOnPage
The offset of the payload reserved for this client within the opened page.
Definition: shared_memory.h:233
int payLen
The size in bytes of the opened page.
Definition: shared_memory.h:231
void wait(int ms)
Sleeps for the indicated amount of milliseconds or longer.
Definition: timing.cpp:32
semaphore mySemaphore
A semaphore that is locked upon trying to allocate space on a page.
Definition: shared_memory.h:229
statExchange(char *_data)
StatExchange constructor, sets the datapointer to the given value.
Definition: shared_memory.cpp:598
A class for managing shared files.
Definition: shared_memory.h:96
long long int now()
Gets timestamp of the current stats.
Definition: shared_memory.cpp:606
unsigned int crc()
Gets checksum field.
Definition: shared_memory.cpp:703
sharedPage myPage
The shared page this client has reserved a space on.
Definition: shared_memory.h:227
bool hasCounter
Whether the payload has a counter, if so, it is added in front of the payload.
Definition: shared_memory.h:200
std::string host()
Gets the host of this connection.
Definition: shared_memory.cpp:669
Definition: shared_memory.cpp:25
long long int down()
Gets the amount of bytes received.
Definition: shared_memory.cpp:642
The client part of a server/client model for shared memory.
Definition: shared_memory.h:212
char getSync()
Gets checksum field.
Definition: shared_memory.cpp:715
std::string myName
Definition: shared_memory.h:82
char * mapped
A pointer to the payload of the file file.
Definition: shared_memory.h:118
sem_t * mySem
Definition: shared_memory.h:80
function init(plot)
Definition: jquery.flot.time.min.js:1
char * data
The payload for the stat exchange.
Definition: shared_memory.h:56
long long int len
The size in bytes of the opened shared file.
Definition: shared_memory.h:114
void setSync(char s)
Sets checksum field.
Definition: shared_memory.cpp:710
std::string streamName()
Gets the name of the stream this user is viewing.
Definition: shared_memory.cpp:683
A class for handling shared memory pages.
Definition: shared_memory.h:160
A class used for the exchange of statistics over shared memory.
Definition: shared_memory.h:20
std::string name
The name of the opened shared file.
Definition: shared_memory.h:112
Definition: shared_memory.h:238
std::string connector()
Gets the name of the connector through which this user is viewing.
Definition: shared_memory.cpp:693
bool hasCounter
Whether the payload has a counter, if so, it is added in front of the payload.
Definition: shared_memory.h:235
std::string baseName
The basename of the shared pages.
Definition: shared_memory.h:192