Skip to content

Commit

Permalink
完善了删除通道的操作,进行了安全析构
Browse files Browse the repository at this point in the history
  • Loading branch information
hkxiaoyu118 committed Oct 9, 2019
1 parent 6bf8995 commit 89d4ef7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
39 changes: 34 additions & 5 deletions WinRPC/WinRPC/Channel/MemoryChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

MemoryChannel::MemoryChannel(const std::string channelName, bool isServer, DWORD shareMemorySize, unsigned sendMaxSize, unsigned receiveMaxSize)
{
m_hSendThread = NULL;
m_hReceiveThread = NULL;
m_channelName = channelName;
m_shareMemorySize = shareMemorySize;
m_isServer = isServer;
Expand All @@ -14,6 +16,16 @@ MemoryChannel::MemoryChannel(const std::string channelName, bool isServer, DWORD

MemoryChannel::~MemoryChannel()
{
//先停止收发工作线程
m_threadWorking = false;
HANDLE hHandles[2] = { m_hSendThread, m_hReceiveThread };
WaitForMultipleObjects(2, hHandles, TRUE, INFINITE);
CloseHandle(m_hSendThread);
CloseHandle(m_hReceiveThread);
delete m_shareMemoryClient;
delete m_shareMemoryServer;
CloseHandle(m_eventClientRead);
CloseHandle(m_eventServerRead);
DeleteCriticalSection(&m_dataCs);
DeleteCriticalSection(&m_sendCs);
}
Expand All @@ -39,8 +51,9 @@ CHANNEL_ERROR MemoryChannel::InitChannel()
if (m_eventClientRead != NULL && m_eventServerRead != NULL)
{
//创建消息收发线程
_beginthread(SendDataThread, 0, this);
_beginthread(ReceiveDataThread, 0, this);
m_threadWorking = true;
m_hSendThread = (HANDLE)_beginthreadex(NULL, 0, SendDataThread, (LPVOID)this, 0, NULL);
m_hReceiveThread = (HANDLE)_beginthreadex(NULL, 0, ReceiveDataThread, (LPVOID)this, 0, NULL);
errorCode = CHANNEL_ERROR::NOT_ERROR;
}
else
Expand Down Expand Up @@ -118,7 +131,7 @@ bool MemoryChannel::GetReceiveData(std::queue<std::string>& dataSet)
return result;
}

void MemoryChannel::SendDataThread(LPVOID args)
unsigned __stdcall MemoryChannel::SendDataThread(LPVOID args)
{
MemoryChannel *p = (MemoryChannel*)args;
HANDLE hEventRead = NULL;
Expand Down Expand Up @@ -155,10 +168,18 @@ void MemoryChannel::SendDataThread(LPVOID args)
{
Sleep(10);
}

//如果发现线程需要停止,则退出循环体,停止线程
if (p->m_threadWorking == false)
{
break;
}
}

return 0;
}

void MemoryChannel::ReceiveDataThread(LPVOID args)
unsigned __stdcall MemoryChannel::ReceiveDataThread(LPVOID args)
{
MemoryChannel *p = (MemoryChannel*)args;
HANDLE hEventRead = NULL;
Expand All @@ -179,7 +200,7 @@ void MemoryChannel::ReceiveDataThread(LPVOID args)

while (true)
{
if (WaitForSingleObject(hEventRead, INFINITE) == WAIT_OBJECT_0)
if (WaitForSingleObject(hEventRead, 500) == WAIT_OBJECT_0)
{
std::string receiveData;
receiveData.resize(p->m_shareMemorySize);
Expand All @@ -189,5 +210,13 @@ void MemoryChannel::ReceiveDataThread(LPVOID args)
printf("%s\n", receiveData.c_str());
ResetEvent(hEventRead);//数据存储已经完成,通知写入端可以写入了
}

//如果发现线程需要停止,则退出循环体,停止线程
if (p->m_threadWorking == false)
{
break;
}
}

return 0;
}
8 changes: 6 additions & 2 deletions WinRPC/WinRPC/Channel/MemoryChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class MemoryChannel
bool GetSendData(std::string& data);
void StoreReceiveData(std::string data);
bool GetReceiveData(std::queue<std::string>& dataSet);
static void SendDataThread(LPVOID args);
static void ReceiveDataThread(LPVOID args);
static unsigned __stdcall SendDataThread(LPVOID args);
static unsigned __stdcall ReceiveDataThread(LPVOID args);


private:
Expand All @@ -51,4 +51,8 @@ class MemoryChannel

unsigned m_sendMaxSize;
unsigned m_receiveMaxSize;

bool m_threadWorking; //判断线程是否工作
HANDLE m_hSendThread; //发消息线程句柄
HANDLE m_hReceiveThread; //收消息线程句柄
};
2 changes: 2 additions & 0 deletions WinRPC/WinRPCClientTest/WinRPCClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ int _tmain(int argc, _TCHAR* argv[])
count++;
Sleep(20);
}

// manager.DelChannelItem(channelName);
system("pause");
return 0;
}
Expand Down
3 changes: 0 additions & 3 deletions WinRPC/WinRPCServerTest/WinRPCServerTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
Expand Down
3 changes: 0 additions & 3 deletions WinRPC/WinRPCServerTest/WinRPCServerTest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
Expand Down

0 comments on commit 89d4ef7

Please sign in to comment.