diff options
Diffstat (limited to 'Kod/display/ClientServerApp/Common')
| -rw-r--r-- | Kod/display/ClientServerApp/Common/ComConduit.cpp | 151 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/ComConduit.h | 59 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/Common.vcproj | 203 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/Common.vcxproj | 139 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/Conduit.cpp | 529 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/Conduit.h | 145 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/ReadABuffer.cpp | 36 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/ReadABuffer.h | 20 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/WriteABuffer.cpp | 67 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/WriteABuffer.h | 22 | ||||
| -rw-r--r-- | Kod/display/ClientServerApp/Common/ipclink.h | 80 |
11 files changed, 1451 insertions, 0 deletions
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 <string.h>
+//#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; ii<data[1]+EXTRA_LEN; ii++)
+ {
+ ptrData->string[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; ii<ptrData->string[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 <stdio.h>
+//
+// 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 @@ +<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="9,00"
+ Name="Common"
+ ProjectGUID="{8C1E78F5-0564-498A-958D-37A997BBFE49}"
+ RootNamespace="Common"
+ TargetFrameworkVersion="196613"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="4"
+ CharacterSet="0"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=""
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ WholeProgramOptimization="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="2"
+ EnableIntrinsicFunctions="true"
+ RuntimeLibrary="2"
+ EnableFunctionLevelLinking="true"
+ WarningLevel="3"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ <File
+ RelativePath=".\ComConduit.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\Conduit.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\ReadABuffer.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\WriteABuffer.cpp"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ <File
+ RelativePath=".\ComConduit.h"
+ >
+ </File>
+ <File
+ RelativePath=".\Conduit.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ipclink.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ReadABuffer.h"
+ >
+ </File>
+ <File
+ RelativePath=".\WriteABuffer.h"
+ >
+ </File>
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{8C1E78F5-0564-498A-958D-37A997BBFE49}</ProjectGuid>
+ <RootNamespace>Common</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <CharacterSet>MultiByte</CharacterSet>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <CharacterSet>MultiByte</CharacterSet>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <CharacterSet>NotSet</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <CharacterSet>NotSet</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup>
+ <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(SolutionDir)$(Configuration)\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(Configuration)\</IntDir>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(SolutionDir)$(Configuration)\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(Configuration)\</IntDir>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <Optimization>Disabled</Optimization>
+ <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <MinimalRebuild>true</MinimalRebuild>
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
+ <WarningLevel>Level3</WarningLevel>
+ <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
+ </ClCompile>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <Optimization>Disabled</Optimization>
+ <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
+ <WarningLevel>Level3</WarningLevel>
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
+ </ClCompile>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <Optimization>MaxSpeed</Optimization>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <WarningLevel>Level3</WarningLevel>
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
+ </ClCompile>
+ <Link>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <OptimizeReferences>true</OptimizeReferences>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <TargetMachine>MachineX86</TargetMachine>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <Optimization>MaxSpeed</Optimization>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <WarningLevel>Level3</WarningLevel>
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
+ </ClCompile>
+ <Link>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <OptimizeReferences>true</OptimizeReferences>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="ComConduit.cpp" />
+ <ClCompile Include="Conduit.cpp" />
+ <ClCompile Include="ReadABuffer.cpp" />
+ <ClCompile Include="WriteABuffer.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="ComConduit.h" />
+ <ClInclude Include="Conduit.h" />
+ <ClInclude Include="ipclink.h" />
+ <ClInclude Include="ReadABuffer.h" />
+ <ClInclude Include="WriteABuffer.h" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
\ 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 <stdio.h>
+#include "Conduit.h"
+#include <tchar.h>
+
+// Constructor
+Conduit::Conduit() {
+
+ ptrShm = NULL;
+ isServer = false;
+ iNumSems = 0;
+
+ hMapObject = NULL;
+ for(int i=0; i<MAX_NUM_SEM; i++)
+ hSemaphore[i] = NULL;
+}
+
+// Destructor
+Conduit::~Conduit() {
+
+ // Issue shutdown function
+ Shutdown();
+}
+
+// Detach and/or remove shared memory segment
+bool Conduit::Shutdown() {
+
+ bool ret = true;
+ int i;
+
+ if(hMapObject != NULL) {
+ // Detach shared memory segment
+ if(!UnmapViewOfFile(ptrShm))
+ ret = false;
+
+ if(isServer) {
+ // Remove shared memory segment
+ if(CloseHandle(hMapObject) == FALSE)
+ ret = false;
+
+ // Destroy semaphores
+ for(i=0; i<iNumSems; i++) {
+ if(CloseHandle(hSemaphore[i]) == FALSE)
+ ret = false;
+ }
+ }
+ hMapObject = NULL;
+ }
+
+ ptrShm = NULL;
+ isServer = false;
+
+ return ret;
+}
+
+// Server initialization
+bool Conduit::StartServer(KeyType* key) {
+
+ int i;
+ bool ret = true;
+
+ if(ptrShm != NULL)
+ return false;
+
+ // Calculate number of semaphores
+ iNumSems = NumberOfSemaphores();
+
+ if(iNumSems > 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<KEY_LEN; i++)
+ strKey[i+1] = key[i]+48;
+ strKey[0] = 'A';
+ strKey[KEY_LEN+1] = '\0';
+
+
+ // Create shared memory segment
+ hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, // Use paging file
+ &attr, // Security attr.
+ PAGE_READWRITE, // Read/write access
+ 0, // Size: high 32-bits
+ GetDataSize(), // Size: low 32-bits
+ strKey); // Name of map object
+
+ if(hMapObject == NULL)
+ return false;
+
+ // Attach to shared memory segment
+ ptrShm = MapViewOfFile(hMapObject, // Object to map view of
+ FILE_MAP_WRITE, // Read/write access
+ 0, // High offset: map from
+ 0, // Low offset: beginning
+ 0); // Default: map entire file
+ if(ptrShm == NULL) {
+ CloseHandle(hMapObject);
+ return false;
+ }
+
+ // Create semaphore for mutual exclusion [initial value 1]
+ strKey[0]++;
+ hSemaphore[0] = CreateSemaphore(&attr, // Security attributes
+ 1, // Initial count
+ 1, // Maximum count
+ strKey); // Named semaphore
+ if(hSemaphore[0] == NULL)
+ ret = false;
+
+ // Create semaphores for event tracking [initial value 0]
+ for(i=1; i<iNumSems; i++) {
+ strKey[0]++;
+ hSemaphore[i] = CreateSemaphore(&attr, // Security attributes
+ 0, // Initial count
+ 1, // Maximum count
+ strKey); // Named semaphore
+ if(hSemaphore[i] == NULL)
+ ret = false;
+ }
+
+ // Making absolutely sure that the semaphores are initialized correctly
+ Signal(0);
+ for(i=1; i<iNumSems; i++)
+ Wait(i,0);
+
+
+
+ if(ret) {
+ isServer = true;
+ return true;
+ }
+ else {
+ isServer = true;
+ Shutdown();
+ isServer = false;
+ return false;
+ }
+}
+
+// Client initialization
+bool Conduit::StartClient(KeyType* key) {
+
+ int i;
+
+ if(ptrShm != NULL)
+ return false;
+
+ // Calculate number of semaphores
+ iNumSems = NumberOfSemaphores();
+
+
+ // WIN32 IPC: Shared memory based on file mapping.
+
+ char strKey[KEY_LEN+2];
+
+ // Convert integer array 'key' to char array 'strKey'
+ for(i=0; i<KEY_LEN; i++)
+ strKey[i+1] = key[i]+48;
+ strKey[0] = 'A';
+ strKey[KEY_LEN+1] = '\0';
+
+ // Locate shared memory segment
+ hMapObject = OpenFileMapping(FILE_MAP_WRITE, // Read/write acces
+ FALSE, // Handle not inheritable
+ strKey); // Name of map object
+ if(hMapObject == NULL)
+ return false;
+
+ // Attach to shared memory segment
+ ptrShm = MapViewOfFile(hMapObject, // Object to map view of
+ FILE_MAP_WRITE, // Read/write access
+ 0, // High offset: map from
+ 0, // Low offset: beginning
+ 0); // Default: map entire file
+ if(ptrShm == NULL)
+ return false;
+
+ // Locate semaphore for mutual exclusion
+ strKey[0]++;
+ hSemaphore[0] = OpenSemaphore(SEMAPHORE_MODIFY_STATE |
+ STANDARD_RIGHTS_ALL, // Desired access
+ FALSE, // No inheritance
+ strKey); // Sempahore name
+ if(hSemaphore[0] == NULL)
+ return false;
+
+ // Locate semaphores for event tracking
+ for(i=1; i<iNumSems; i++) {
+ strKey[0]++;
+ hSemaphore[i] = OpenSemaphore(SEMAPHORE_MODIFY_STATE |
+ STANDARD_RIGHTS_ALL, // Desired access
+ FALSE, // No inheritance
+ strKey); // Sempahore name
+ if(hSemaphore[i] == NULL)
+ return false;
+ }
+
+
+ isServer = false;
+ return true;
+}
+
+// Wait-operation on a semaphore
+bool Conduit::Wait(int index) {
+
+
+ if(hSemaphore[index] == NULL)
+ return false;
+
+ if(WaitForSingleObject(hSemaphore[index],INFINITE) != WAIT_OBJECT_0)
+ return false;
+
+ return true;
+
+
+}
+
+// Wait-operation with timeout [ms] on a semaphore
+bool Conduit::Wait(int index, int timeout) {
+
+
+ if(hSemaphore[index] == NULL)
+ return false;
+
+ if(WaitForSingleObject(hSemaphore[index],timeout) != WAIT_OBJECT_0)
+ return false;
+
+ return true;
+}
+
+// Signal-operation on semaphore
+bool Conduit::Signal(int index) {
+
+ if(hSemaphore[index] == NULL)
+ return false;
+
+ if(ReleaseSemaphore(hSemaphore[index],1,NULL) == FALSE) {
+ if(GetLastError() == ERROR_TOO_MANY_POSTS)
+ // Already unlocked
+ return true;
+ else
+ // Error
+ return false;
+ }
+
+ return true;
+}
+
+// Request lock on resource
+bool Conduit::RequestLock() {
+
+ return Wait(0);
+}
+
+// Release lock on resource
+bool Conduit::ReleaseLock() {
+
+ return Signal(0);
+}
+
+// Indicate change of flag
+bool Conduit::IndicateChangedFlag() {
+
+ return Signal(1);
+}
+
+// Wait for change of flag
+bool Conduit::WaitForChangedFlag() {
+
+ return Wait(1);
+}
+
+// Wait for change with timeout of flag
+bool Conduit::WaitForChangedFlag(int timeout) {
+
+ return Wait(1,timeout);
+}
+
+// Wait for a flag
+WaitResult Conduit::WaitForFlag(FlagType flag) {
+
+ bool ret,sflag;
+ WaitResult wr;
+ FlagType currentFlag;
+
+ do {
+ if(FlagEqual(flag,currentFlag))
+ return WAIT_OK;
+
+ // Perform wait
+ ret = WaitForChangedFlag();
+
+ wr = WAIT_OK;
+ if(!ret || !GetShutdownFlag(sflag))
+ wr = WAIT_FAIL;
+ if(sflag)
+ wr = WAIT_CLOSE;
+
+ } while(wr == WAIT_OK);
+
+ return wr;
+}
+
+// Wait for a flag with timeout
+WaitResult Conduit::WaitForFlag(FlagType flag, int timeout) {
+
+ bool ret,sflag;
+ WaitResult wr;
+ FlagType currentFlag;
+
+ do {
+ if(FlagEqual(flag,currentFlag))
+ return WAIT_OK;
+
+ // Perform wait
+ ret = WaitForChangedFlag(timeout);
+
+ wr = WAIT_OK;
+ if(!ret || !GetShutdownFlag(sflag))
+ wr = WAIT_FAIL;
+ if(sflag)
+ wr = WAIT_CLOSE;
+
+ } while(wr == WAIT_OK);
+
+ return wr;
+}
+
+// Get flag but also check if equal to given parameter. Equal means
+// that any of the bits set to one in flag matches a bit set to one in
+// the current flag, i.e. if an and-operation gives a positive result.
+bool Conduit::FlagEqual(FlagType flag, FlagType& current) {
+
+ return(GetFlag(current) &&
+ ((flag == NULL_FLAG && current == NULL_FLAG) ||
+ ((current & flag) > 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; i<iNumSems; i++) {
+ if(!Signal(i))
+ return false;
+ }
+ return true;
+}
+
+// Get state flag
+bool Conduit::GetFlag(FlagType& flag) {
+
+ bool ret = true;
+ CommonData *ptr;
+
+ // Request lock
+ if(!RequestLock())
+ return false;
+
+ if((ptr = GetCommonData()) != NULL)
+ flag = ptr->Flag;
+ 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 <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_ */
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 <windows.h>
+#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<lenBuf+EXTRA_LEN; ii++)
+ {
+ printBuf[ii] = (unsigned char) lpBuf[ii];
+// printf("\nWriteABuffer, variable printBuf[%d]: %d \n", ii, printBuf[ii]);
+ }
+ printBuf[ii] ='\0';
+// printf("\nWriteABuffer, variable printBuf[%d]: %d \n", ii, printBuf[ii]);
+
+
+
+
+
+
+ // 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.
+// printf("\tNum to write: %i\n",dwToWrite);
+// if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
+ if (!WriteFile(hComm, ptrPrintBuf, lenBuf+3, &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;
+}
+
+
diff --git a/Kod/display/ClientServerApp/Common/WriteABuffer.h b/Kod/display/ClientServerApp/Common/WriteABuffer.h new file mode 100644 index 0000000..d34b7f7 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/WriteABuffer.h @@ -0,0 +1,22 @@ +#ifndef WRITEABUFFER_H
+#define WRITEABUFFER_H
+
+#include <windows.h>
+#include <stdio.h>
+#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 <windows.h>
+
+//
+// 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 <DC1>, 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_ */
|
