From e66efbbe8df2dd4e7de0a1e9bd129cf92e00f92f Mon Sep 17 00:00:00 2001 From: vikle012 Date: Wed, 11 Sep 2019 13:57:14 +0200 Subject: Initializes repo. --- Kod/display/ClientServerApp/Common/ComConduit.cpp | 151 ++++++ Kod/display/ClientServerApp/Common/ComConduit.h | 59 +++ Kod/display/ClientServerApp/Common/Common.vcproj | 203 ++++++++ Kod/display/ClientServerApp/Common/Common.vcxproj | 139 ++++++ Kod/display/ClientServerApp/Common/Conduit.cpp | 529 +++++++++++++++++++++ Kod/display/ClientServerApp/Common/Conduit.h | 145 ++++++ Kod/display/ClientServerApp/Common/ReadABuffer.cpp | 36 ++ Kod/display/ClientServerApp/Common/ReadABuffer.h | 20 + .../ClientServerApp/Common/WriteABuffer.cpp | 67 +++ Kod/display/ClientServerApp/Common/WriteABuffer.h | 22 + Kod/display/ClientServerApp/Common/ipclink.h | 80 ++++ 11 files changed, 1451 insertions(+) create mode 100644 Kod/display/ClientServerApp/Common/ComConduit.cpp create mode 100644 Kod/display/ClientServerApp/Common/ComConduit.h create mode 100644 Kod/display/ClientServerApp/Common/Common.vcproj create mode 100644 Kod/display/ClientServerApp/Common/Common.vcxproj create mode 100644 Kod/display/ClientServerApp/Common/Conduit.cpp create mode 100644 Kod/display/ClientServerApp/Common/Conduit.h create mode 100644 Kod/display/ClientServerApp/Common/ReadABuffer.cpp create mode 100644 Kod/display/ClientServerApp/Common/ReadABuffer.h create mode 100644 Kod/display/ClientServerApp/Common/WriteABuffer.cpp create mode 100644 Kod/display/ClientServerApp/Common/WriteABuffer.h create mode 100644 Kod/display/ClientServerApp/Common/ipclink.h (limited to 'Kod/display/ClientServerApp/Common') diff --git a/Kod/display/ClientServerApp/Common/ComConduit.cpp b/Kod/display/ClientServerApp/Common/ComConduit.cpp new file mode 100644 index 0000000..1bb952f --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ComConduit.cpp @@ -0,0 +1,151 @@ +/* + * ComConduit.cpp + * + * Definition of the ComConduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#include "ComConduit.h" +#include +//#include "mex.h" + +// Constructor +ComConduit::ComConduit() { + + ptrData = NULL; +} + +// Destructor +ComConduit::~ComConduit() { + + // Issue shutdown function + Shutdown(); +} + +// Detach and remove shared memory segment +bool ComConduit::Shutdown() { + + if(Conduit::Shutdown()) { + ptrData = NULL; + return true; + } + else + return false; +} + +// Server initialization +bool ComConduit::StartServer(KeyType* key) { + + if(!Conduit::StartServer(key)) + return false; + + ptrData = (COMdata*)ptrShm; + ptrData->Common.Flag = NULL_FLAG; + ptrData->Common.DoShutdown = false; + ptrData->string[0] = '\0'; + + return true; +} + +// Client initialization +bool ComConduit::StartClient(KeyType* key) { + + if(!Conduit::StartClient(key)) + return false; + + ptrData = (COMdata*)ptrShm; + + return true; +} + +// Get shared data size +int ComConduit::GetDataSize() const { + + return sizeof(COMdata); +} + +// Get pointer to common shared data +CommonData* ComConduit::GetCommonData() const { + + if(ptrData == NULL) + return(NULL); + else + return(&ptrData->Common); +} + +// Get number of events +int ComConduit::GetNumEvents() const { + + return COM_NEVENTS; +} + +// Set the data +bool ComConduit::SetData(const wchar_t* data) { + + int ii; + + if(ptrData == NULL) + return false; + + // Request lock + if(!RequestLock()) + return false; + + // Copy string +// wcscpy_s(ptrData->string,COMSTR_LEN, data); + + /* Copy the wchar_t-string manual */ + for (ii=0; iistring[ii] = data[ii]; + // printf("\nptrData->string[%d]: %d \n", ii, ptrData->string[ii]); + } + /* Stop with a "null" char */ + ptrData->string[ii] = '\0'; + + // Release lock + if(!ReleaseLock()) + return false; + + return true; +} + +// Get the data +bool ComConduit::GetData(wchar_t* data) { + + int ii; + + if(ptrData == NULL) + return false; + + // Request lock + if(!RequestLock()) + return false; + + // Copy string +// wcscpy_s(data,COMSTR_LEN,ptrData->string); +// strcpy((char*) data, ptrData->string); + + //printf("\tComConduit:GetData: %d\n",ptrData->string[1]); + + /* Copy the wchar_t-string manual */ + for (ii=0; iistring[1]+EXTRA_LEN; ii++) + { + data[ii] = ptrData->string[ii]; + //printf("\nGetData:data[%d]: %d \n", ii, data[ii]); + } + /* Stop with a "null" char */ + data[ii] = (wchar_t) '\0'; + //printf("\nGetData:data[%d]: %d \n", ii, data[ii]); + + // Release lock + if(!ReleaseLock()) + return false; + + return true; +} + + + diff --git a/Kod/display/ClientServerApp/Common/ComConduit.h b/Kod/display/ClientServerApp/Common/ComConduit.h new file mode 100644 index 0000000..3948355 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ComConduit.h @@ -0,0 +1,59 @@ +/* + * ComConduit.h + * + * Declaration of the ComConduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#ifndef COMCONDUIT_H_ +#define COMCONDUIT_H_ + +// Include conduit base class +#include "Conduit.h" +// Include ipc interface +#include "ipclink.h" + +#include +// +// The ComConduit class +// +class ComConduit : public Conduit { + +public: + + // Construct & Destruct + ComConduit(); + virtual ~ComConduit(); + + // Server initialization + bool StartServer(KeyType* key); + // Client initialization + bool StartClient(KeyType* key); + // Detach and remove shared memory segment + bool Shutdown(); + + // Get shared data size + int GetDataSize() const; + // Get number of events + int GetNumEvents() const; + // Get pointer to common shared data + CommonData* GetCommonData() const; + + // Write data + bool SetData(const wchar_t* data); + + // Data read + bool GetData(wchar_t* data); + +private: + + // Pointer to shared data + COMdata* ptrData; + +}; + + + +#endif /* COMCONDUIT_H_ */ diff --git a/Kod/display/ClientServerApp/Common/Common.vcproj b/Kod/display/ClientServerApp/Common/Common.vcproj new file mode 100644 index 0000000..b5be92d --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Common.vcproj @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Kod/display/ClientServerApp/Common/Common.vcxproj b/Kod/display/ClientServerApp/Common/Common.vcxproj new file mode 100644 index 0000000..be80a98 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Common.vcxproj @@ -0,0 +1,139 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {8C1E78F5-0564-498A-958D-37A997BBFE49} + Common + + + + StaticLibrary + MultiByte + true + + + StaticLibrary + MultiByte + true + + + StaticLibrary + NotSet + + + StaticLibrary + NotSet + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + + + + Disabled + %(AdditionalIncludeDirectories) + true + EnableFastChecks + MultiThreadedDebugDLL + Level3 + EditAndContinue + + + + + Disabled + %(AdditionalIncludeDirectories) + EnableFastChecks + MultiThreadedDebugDLL + Level3 + ProgramDatabase + + + + + MaxSpeed + true + MultiThreadedDLL + true + Level3 + ProgramDatabase + + + true + true + true + MachineX86 + + + + + MaxSpeed + true + MultiThreadedDLL + true + Level3 + ProgramDatabase + + + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/Conduit.cpp b/Kod/display/ClientServerApp/Common/Conduit.cpp new file mode 100644 index 0000000..1d827f4 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Conduit.cpp @@ -0,0 +1,529 @@ +/* + * Conduit.cpp + * + * Definition 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 + */ + +#include +#include "Conduit.h" +#include + +// Constructor +Conduit::Conduit() { + + ptrShm = NULL; + isServer = false; + iNumSems = 0; + + hMapObject = NULL; + for(int i=0; i MAX_NUM_SEM) + return false; + + + // WIN32 IPC: Shared memory based on file mapping. + + char strKey[KEY_LEN+2]; + SECURITY_ATTRIBUTES attr; + + // Fill struct with security attributes + attr.nLength = sizeof(SECURITY_ATTRIBUTES); + attr.lpSecurityDescriptor = NULL; + attr.bInheritHandle = FALSE; + + // Convert integer array 'key' to char array 'strKey' + for(i=0; i 0))); +} + +// Wait for an event (reserve associated semaphore) +WaitResult Conduit::WaitForEvent(int index) { + + bool ret,flag; + + if(!ValidIndex(index)) + return WAIT_FAIL; + + // Perform wait + ret = Wait(index + FIRST_EVENT_SEM); + + if(!ret || !GetShutdownFlag(flag)) + return WAIT_FAIL; + if(flag) + return WAIT_CLOSE; + + return WAIT_OK; +} + +// Wait for an event with timeout +WaitResult Conduit::WaitForEvent(int index, int timeout) { + + bool ret,flag; + + if(!ValidIndex(index)) + return WAIT_FAIL; + + // Perform wait + ret = Wait(index + FIRST_EVENT_SEM,timeout); + + if(!ret || !GetShutdownFlag(flag)) + return WAIT_FAIL; + if(flag) + return WAIT_CLOSE; + + return WAIT_OK; +} + +// Cause an event (release associated sempahore) +bool Conduit::CauseEvent(int index) { + + if(!ValidIndex(index)) + return false; + else + return Signal(index + FIRST_EVENT_SEM); +} + +// Cause shutdown event +bool Conduit::CauseShutdown() { + + int i; + + // Indicate shutdown + if(!SetShutdownFlag(true)) + return false; + + // Release all semaphores but the mutex + for(i=1; iFlag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Set state flag +bool Conduit::SetFlag(FlagType flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + ptr->Flag = flag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + // Indicate change + if(!IndicateChangedFlag()) + return false; + + return ret; +} + +// Get shutdown boolean +bool Conduit::GetShutdownFlag(bool& flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + flag = ptr->DoShutdown; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Set shutdown boolean +bool Conduit::SetShutdownFlag(bool flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + ptr->DoShutdown = flag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Check validity of semaphore index +bool Conduit::ValidIndex(int index) { + + if(index < 0 || index > (iNumSems - FIRST_EVENT_SEM)) + return false; + + return true; +} + +// Calculate number of semaphores +int Conduit::NumberOfSemaphores() { + + return(FIRST_EVENT_SEM + GetNumEvents()); +} + + + diff --git a/Kod/display/ClientServerApp/Common/Conduit.h b/Kod/display/ClientServerApp/Common/Conduit.h new file mode 100644 index 0000000..012d3f6 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Conduit.h @@ -0,0 +1,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 + +// 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 +#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_ */ diff --git a/Kod/display/ClientServerApp/Common/ReadABuffer.cpp b/Kod/display/ClientServerApp/Common/ReadABuffer.cpp new file mode 100644 index 0000000..97bd9b5 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ReadABuffer.cpp @@ -0,0 +1,36 @@ +#include "ReadABuffer.h" + +BOOL ReadABuffer(unsigned char * lpBuf, DWORD dwToWrite, HANDLE hComm) +{ + OVERLAPPED osWrite = {0}; + DWORD dwWritten; + BOOL fRes; + + // Create this writes OVERLAPPED structure hEvent. + osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (osWrite.hEvent == NULL) + // Error creating overlapped event handle. + return FALSE; + + // Issue write. + if (!ReadFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) { + if (GetLastError() != ERROR_IO_PENDING) { + // WriteFile failed, but it isn't delayed. Report error and abort. + fRes = FALSE; + } + else { + // Write is pending. + if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE)) + fRes = FALSE; + else + // Write operation completed successfully. + fRes = TRUE; + } + } + else + // WriteFile completed immediately. + fRes = TRUE; + + CloseHandle(osWrite.hEvent); + return fRes; +} \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/ReadABuffer.h b/Kod/display/ClientServerApp/Common/ReadABuffer.h new file mode 100644 index 0000000..56cf400 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ReadABuffer.h @@ -0,0 +1,20 @@ +#ifndef READABUFFER_H +#define READABUFFER_H + +#include +#include "ipclink.h" + + +BOOL ReadABuffer(unsigned char * lpBuf, DWORD dwToWrite, HANDLE hComm); + + + + + + + + + + + +#endif \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/WriteABuffer.cpp b/Kod/display/ClientServerApp/Common/WriteABuffer.cpp new file mode 100644 index 0000000..41ecf52 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/WriteABuffer.cpp @@ -0,0 +1,67 @@ + + +#include "WriteABuffer.h" + + +BOOL WriteABuffer(wchar_t* lpBuf, HANDLE hComm) +{ + OVERLAPPED osWrite = {0}; + DWORD dwWritten; + BOOL fRes; + + unsigned char printBuf[COMSTR_LEN]; + unsigned char* ptrPrintBuf; + ptrPrintBuf = &printBuf[0]; + + int lenBuf = (int) lpBuf[1]; + int ii; + +// printf("\tInteger: %d\n",lenBuf); + +// printf("\tLength of lpBuf: %d\n",wcslen(lpBuf)); + + for (ii=0; ii +#include +#include "ipclink.h" + +BOOL WriteABuffer(wchar_t * lpBuf, HANDLE hComm); + +BOOL ReadABuffer(wchar_t * lpBuf, DWORD dwToWrite, HANDLE hComm); + + + + + + + + + + + +#endif \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/ipclink.h b/Kod/display/ClientServerApp/Common/ipclink.h new file mode 100644 index 0000000..d62429e --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ipclink.h @@ -0,0 +1,80 @@ +/* + * ipclink.h + * + * Interface for the shared memory communication + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#ifndef IPCLINK_H_ +#define IPCLINK_H_ + +// Include files for sleep-functions +#include + +// +// Shared data description +// + +// Keys +#define COM_KEY {3,1,4,6} + +// Define what is returned from wait operations +typedef unsigned int WaitResult; +#define WAIT_OK 0 +#define WAIT_FAIL 1 +#define WAIT_CLOSE 2 + +// Definition of flags +typedef unsigned int FlagType; +#define NULL_FLAG 0 + +// Exit flags +#define EXIT_OK 0 +#define EXIT_FAIL 1 + +// +// COMMON DATA +// +typedef struct structCommonData { + FlagType Flag; // State flag + bool DoShutdown; // Shutdown flag +} CommonData; + +// +// Length of , len, and bcc +// see manual for "smallprotocol package". +// +#define EXTRA_LEN 3 + + +// +// COM CONDUIT +// +// Maximum string length +//#define COMSTR_LEN 512 +// EXTRA_LEN is included +#define COMSTR_LEN 131 + + + +// Shared memory structure +typedef struct { + CommonData Common; // Common data + wchar_t string[COMSTR_LEN]; // Data string +} COMdata; + +// Flags +//#define COM_FLAG 0x01 // Example flag +// Events +#define COM_NEVENTS 2 // Number of events +#define COM_READY 0 // Ready +#define COM_REQUEST 1 // Request + +// Define sleep function +#define msleep(t) Sleep(t) + + + +#endif /* IPCLINK_H_ */ -- cgit v1.2.1