cShare.cpp
4.46 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2001
*
*****************************************************************************/
/*******************************************************************************
* Filename:
* ---------
* cShare.cpp
*
* Project:
* --------
* Catcher
*
* Description:
* ------------
* This module contains implementation of IPC between U-T and Catcher
*
* Author:
* -------
* -------
*
*==============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*==============================================================================
*******************************************************************************/
//#include "StdAfx.h"
#include "cShare.h"
#include "stdio.h"
CShare::CShare()
{
Data_Read[0] = NULL;
Data_Read[1] = NULL;
Data_Written[0] = NULL;
Data_Written[1] = NULL;
m_hMap = NULL;
m_pSharedData = NULL;
m_size = 0;
}
CShare::~CShare()
{
DestroyShare();
}
bool CShare::CreateShare(const char* ID, const int &size)
{
DestroyShare();
char Map_ID[256];
char Read_ID[256];
char Write_ID[256];
sprintf(Map_ID, "%s_Map", ID);
if (strlen(Map_ID) > 250)
{
printf("Error: CShare::CreateShare with ID too long. It won't be able to connect Catcher!\n");
return false;
}
sprintf(Read_ID, "%s_Read", ID);
sprintf(Write_ID, "%s_Write", ID);
m_hMap = ::CreateFileMapping( (HANDLE)0xFFFFFFFF,
0, // Security
PAGE_READWRITE,
0, // size --> high order
size, // size --> low order
Map_ID // map ID
);
if( m_hMap==NULL )
goto Create_Failed;
m_pSharedData = (char *)::MapViewOfFile(m_hMap,
FILE_MAP_WRITE,
0, // offset high
0, // offset low
0 // number of bytes, 0-->entire
);
if( m_pSharedData==NULL )
goto Create_Failed;
Data_Read[0] = ::CreateEvent(NULL, TRUE, FALSE, Read_ID);
if( Data_Read[0]==NULL )
goto Create_Failed;
Data_Read[1] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
if( Data_Read[1]==NULL )
goto Create_Failed;
Data_Written[0] = ::CreateEvent(NULL, TRUE, FALSE, Write_ID);
if( Data_Written[0]==NULL )
goto Create_Failed;
Data_Written[1] = Data_Read[1];
m_size = size - sizeof(int);
return true;
Create_Failed:
DestroyShare();
return false;
}
void CShare::DestroyShare()
{
if( m_pSharedData!=NULL )
{
::UnmapViewOfFile(m_pSharedData);
m_pSharedData = NULL;
}
if( m_hMap!=NULL )
{
CloseHandle(m_hMap);
m_hMap = NULL;
}
if( Data_Read[0]!=NULL )
{
CloseHandle(Data_Read[0]);
Data_Read[0] = NULL;
}
if( Data_Read[1]!=NULL )
{
CloseHandle(Data_Read[1]);
Data_Read[1] = NULL;
}
if( Data_Written[0]!=NULL )
{
CloseHandle(Data_Written[0]);
Data_Written[0] = NULL;
}
Data_Written[1] = NULL;
m_size = 0;
}
int CShare::Write(const void *data, const int &size)
{
if( m_pSharedData==NULL )
return 0;
if( size>m_size || size==0 )
return 0;
reinterpret_cast<const char *>(data);
::memcpy(m_pSharedData, &size, sizeof(int));
::memcpy(m_pSharedData+sizeof(int), data, size);
::SetEvent(Data_Written[0]);
if( ::WaitForMultipleObjects(2, Data_Read, FALSE, 3000)!=WAIT_OBJECT_0 )
{
::ResetEvent(Data_Written[0]);
return 0;
}
else
{
::ResetEvent(Data_Read[0]);
return size;
}
}
int CShare::Read(void *data, const int &size)
{
if( m_pSharedData==NULL )
return 0;
reinterpret_cast<char *>(data);
int _len;
if( ::WaitForMultipleObjects(2, Data_Written, FALSE, INFINITE)!=WAIT_OBJECT_0 )
return 0;
::ResetEvent(Data_Written[0]);
::memcpy(&_len, m_pSharedData, sizeof(int));
if( _len>=size )
{
::memcpy(data, m_pSharedData+sizeof(int), size);
::SetEvent(Data_Read[0]);
return size;
}
else
{
::memcpy(data, m_pSharedData+sizeof(int), _len);
::SetEvent(Data_Read[0]);
return _len;
}
}
void CShare::Stop()
{
::SetEvent(Data_Read[1]);
}