summaryrefslogtreecommitdiffstats
path: root/Kod/display/ClientServerApp/Common/Conduit.h
blob: 012d3f66e6f68b412321e999346656e75c82e702 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Conduit.h
 *
 * Declaration of the Conduit class
 *
 *      Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14
 *    Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13
 */

#ifndef CONDUIT_H_
#define CONDUIT_H_

#include "ipclink.h"
#include <windows.h>

// Segment key length
#define KEY_LEN 4
// Data type of segment key
typedef unsigned short int KeyType;

// Maximum number of semaphores
#define MAX_NUM_SEM 10

// First semaphore for event tracking
// Semaphore 0 : mutual exclusion
// Semaphore 1 : change of data
#define FIRST_EVENT_SEM 2


#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
// union semun is defined by including <sys/sem.h>
#else
// according to X/OPEN we have to define it ourselves
union semun {
	int val;                  // value for SETVAL
	struct semid_ds *buf;     // buffer for IPC_STAT, IPC_SET
	wchar_t *array;    // array for GETALL, SETALL
	// Linux specific part:
	struct seminfo *__buf;    // buffer for IPC_INFO
};
#endif


//
// The Conduit class
//
class Conduit {

public:

	// Construct & Destruct
	Conduit();
	virtual ~Conduit();

	// Server initialization
	bool StartServer(KeyType* key);
	// Client initialization
	bool StartClient(KeyType* key);
	// Detach and remove shared memory segment
	bool Shutdown();

	// Request lock on shared data
	bool RequestLock();
	// Release lock on shared data
	bool ReleaseLock();

	// Wait for a flag
	WaitResult WaitForFlag(FlagType flag);
	// Wait for a flag with timeout
	WaitResult WaitForFlag(FlagType flag, int timeout);

	// Wait for an event
	WaitResult WaitForEvent(int index);
	// Wait for an event with timeout [ms]
	WaitResult WaitForEvent(int index, int timeout);
	// Cause an event
	bool CauseEvent(int index);
	// Cause shutdown event
	bool CauseShutdown();

	// Get state flag
	bool GetFlag(FlagType& flag);
	// Set state flag
	bool SetFlag(FlagType flag);

	// Get shutdown boolean
	bool GetShutdownFlag(bool& flag);
	// Set shutdown boolean
	bool SetShutdownFlag(bool flag);

	//
	// Pure virtual functions
	//
	// Get shared data size
	virtual int GetDataSize() const = 0;
	// Get number of events
	virtual int GetNumEvents() const = 0;
	// Get pointer to common shared data
	virtual CommonData* GetCommonData() const = 0;

protected:

	// Pointer to shared memory segment
	void* ptrShm;

private:

	// Wait-operation on a semaphore
	bool Wait(int index);
	// Wait-operation with timeout [ms] on a semaphore
	bool Wait(int index, int timeout);
	// Signal-operation on semaphore
	bool Signal(int index);

	// Indicate change of flag
	bool IndicateChangedFlag();
	// Wait for change of flag
	bool WaitForChangedFlag();
	// Wait for change with timeout of flag
	bool WaitForChangedFlag(int timeout);
	// Get and compare flag
	bool FlagEqual(FlagType flag, FlagType& current);

	// Check validity of semaphore index
	bool ValidIndex(int index);
	// Calculate number of semaphores
	int NumberOfSemaphores();

	// State flag
	bool isServer;
	// Number of semaphores
	int iNumSems;

	// WIN32 IPC: Shared memory based on file mapping.

	// Shared memory segment handle
	HANDLE hMapObject;
	// Semaphore handle
	HANDLE hSemaphore[MAX_NUM_SEM];

};



#endif /* CONDUIT_H_ */