1 // TwoBufQueue.cpp: implementation of the CTwoBufQueue class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "OverlapRoutine.h"
7 #include "TwoBufQueue.h"
8
9 #ifdef _DEBUG
10 #undef THIS_FILE
11 static char THIS_FILE[]=__FILE__;
12 #define new DEBUG_NEW
13 #endif
14
15 //////////////////////////////////////////////////////////////////////
16 // Construction/Destruction
17 //////////////////////////////////////////////////////////////////////
18
19 CTwoBufQueue::CTwoBufQueue()
20 {
21 this->m_pRead_Q = &m_Queue_A;
22 this->m_pWrite_Q = &m_Queue_B;
23 }
24
25 CTwoBufQueue::~CTwoBufQueue()
26 {
27
28 }
29
30 // 交换缓存对象指针
31 void CTwoBufQueue::private_SwitchPointer()
32 {
33 CBufQueue *pTemp = this->m_pRead_Q;
34 this->m_pRead_Q = this->m_pWrite_Q;
35 this->m_pWrite_Q = pTemp;
36 }
37
38 int CTwoBufQueue::Read(char *pBuf, int readBytes)
39 {
40 int res = this->m_pRead_Q->Read(pBuf,readBytes);
41 if(res>0)
42 {
43 return res;
44 }else //读缓存空 ,交换缓存对象指针
45 {
46 this->m_critcal.Lock();
47 this->private_SwitchPointer();
48 this->m_critcal.Free();
49
50 return this->m_pRead_Q->Read(pBuf,readBytes);
51 }
52 }
53
54 // 写操作指针对当前buf对象,不做交换
55 int CTwoBufQueue::Write(const char *pBuf, int writeBytes)
56 {
57 this->m_critcal.Lock();
58 int res = this->m_pWrite_Q->Write(pBuf,writeBytes);
59 this->m_critcal.Free();
60
61 return res;
62 }
63
64 int CTwoBufQueue::GetdataSize()
65 {
66 return this->m_pRead_Q->GetdataSize()+this->m_pWrite_Q->GetdataSize();
67 }