SCF.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. #include <dlfcn.h>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <unistd.h>
  5. #include <sys/time.h>
  6. #include "common_api.h"
  7. #include "RawCircleBuff.h"
  8. #include "SCF.h"
  9. SCFBuffer::SCFBuffer() {
  10. m_CurPacketIdx = 0;
  11. m_pPacket[0] = (SCFPacket*)MallocSCFPacket();
  12. m_pPacket[1] = (SCFPacket*)MallocSCFPacket();
  13. }
  14. SCFBuffer::~SCFBuffer() {
  15. ReleaseSCFPacket(m_pPacket[0]);
  16. ReleaseSCFPacket(m_pPacket[1]);
  17. }
  18. bool SCFBuffer::SetBuff(uint32_t Len) {
  19. bool ret = true;
  20. ret &= m_pPacket[0]->SetBuffLen(Len);
  21. ret &= m_pPacket[1]->SetBuffLen(Len);
  22. return ret;
  23. }
  24. const char* SCFBuffer::GetBuff() {
  25. return *m_pPacket[m_CurPacketIdx];
  26. }
  27. uint32_t SCFBuffer::GetPacketLen() {
  28. return m_pPacket[m_CurPacketIdx]->GetPacketLen();
  29. }
  30. int SCFBuffer::Add(const char* data, uint32_t len) {
  31. return m_pPacket[m_CurPacketIdx]->AddTail(data, len);
  32. }
  33. int SCFBuffer::Del(uint32_t len) {
  34. uint32_t Length = m_pPacket[m_CurPacketIdx]->GetPacketLen();
  35. if (Length == len) {
  36. m_pPacket[m_CurPacketIdx]->SetPacketLen(0);
  37. return SCF_SUCCEED;
  38. }
  39. else if (Length < len) {
  40. m_pPacket[m_CurPacketIdx]->SetPacketLen(0);
  41. return SCF_OVERFLOW;
  42. }
  43. const char* pOrg = *m_pPacket[m_CurPacketIdx];
  44. const char* pOrgLeft = &pOrg[len];
  45. uint32_t NextIdx = (m_CurPacketIdx + 1) % 2;
  46. m_CurPacketIdx = NextIdx;
  47. return m_pPacket[NextIdx]->SetPacket(pOrgLeft, Length - len);
  48. }
  49. void SCFBuffer::ClearData() {
  50. m_pPacket[m_CurPacketIdx]->SetPacketLen(0);
  51. }
  52. SCFPacket::SCFPacket() : m_PacketLength(0) {
  53. m_PacketBuff = new char[4096];
  54. m_PacketBuffLen = m_PacketBuff ? 4096 : 0;
  55. }
  56. SCFPacket::~SCFPacket() {
  57. delete[] m_PacketBuff;
  58. }
  59. SCFPacket::SCFPacket(char* pData, uint32_t Length) {
  60. if (pData && Length != 0) {
  61. m_PacketBuff = new char[Length + 1];
  62. if (m_PacketBuff) {
  63. m_PacketLength = Length;
  64. m_PacketBuffLen = Length + 1;
  65. memcpy(m_PacketBuff, pData, Length);
  66. m_PacketBuff[Length] = 0;
  67. }
  68. else {
  69. m_PacketBuff = nullptr;
  70. m_PacketLength = 0;
  71. m_PacketBuffLen = 0;
  72. }
  73. }
  74. else {
  75. m_PacketBuff = nullptr;
  76. m_PacketLength = 0;
  77. m_PacketBuffLen = 0;
  78. }
  79. }
  80. bool SCFPacket::ReAlloc(uint32_t BaseLen) {
  81. // 添加内存上限检查
  82. const uint32_t MAX_ALLOWED_SIZE = 1024 * 1024 * 100; // 100MB上限
  83. if (BaseLen > MAX_ALLOWED_SIZE) {
  84. return false;
  85. }
  86. uint32_t newSize = std::min(BaseLen + 1024 * 1024, MAX_ALLOWED_SIZE);
  87. char* temp = new (std::nothrow) char[newSize]; // 使用nothrow避免异常
  88. if (!temp) {
  89. return false;
  90. }
  91. // 初始化新内存
  92. memset(temp, 0, newSize);
  93. if (m_PacketBuff) {
  94. // 只复制有效数据
  95. uint32_t copyLen = std::min(m_PacketLength, newSize);
  96. if (copyLen > 0) {
  97. memcpy(temp, m_PacketBuff, copyLen);
  98. }
  99. delete[] m_PacketBuff;
  100. m_PacketLength = copyLen;
  101. }
  102. m_PacketBuff = temp;
  103. m_PacketBuffLen = newSize;
  104. return true;
  105. }
  106. bool SCFPacket::SetBuffLen(uint32_t Len) {
  107. if (m_PacketBuffLen <= Len) {
  108. return ReAlloc(Len);
  109. }
  110. return true;
  111. }
  112. SCFPacket& SCFPacket::operator = (const SCFPacket& tValue) {
  113. // Implementation needed
  114. return *this;
  115. }
  116. int SCFPacket::SetPacket(const char* pData, uint32_t Length) {
  117. if (Length >= m_PacketBuffLen) {
  118. if (!ReAlloc(Length)) return SCF_NOMEMORY;
  119. }
  120. memcpy(m_PacketBuff, pData, Length);
  121. m_PacketLength = Length;
  122. return m_PacketLength;
  123. }
  124. int SCFPacket::AddTail(const char* pData, uint32_t Length) {
  125. if (!pData) return SCF_PARAMETER_ERR;
  126. if (m_PacketBuffLen <= (m_PacketLength + Length)) {
  127. if (!ReAlloc(m_PacketLength + Length)) return SCF_NOMEMORY;
  128. }
  129. memcpy(&m_PacketBuff[m_PacketLength], pData, Length);
  130. m_PacketLength += Length;
  131. return m_PacketLength;
  132. }
  133. void SCFPacket::SetPacketLen(uint32_t Len) {
  134. m_PacketLength = Len;
  135. }
  136. uint32_t SCFPacket::GetPacketLen() {
  137. return m_PacketLength;
  138. }
  139. uint32_t SCFPacket::GetBuffLen() {
  140. return m_PacketBuffLen;
  141. }
  142. SCFPacket::operator char* () {
  143. return m_PacketBuff;
  144. }
  145. int SCFPacket::GetPacket(char* pData, uint32_t Length) {
  146. if (m_PacketLength > 0) {
  147. if (m_PacketLength > Length) return SCF_OVERFLOW;
  148. memcpy(pData, m_PacketBuff, m_PacketLength);
  149. return m_PacketLength;
  150. }
  151. return SCF_NOPACKET;
  152. }
  153. void* MallocSCFPacket() {
  154. return new SCFPacket();
  155. }
  156. void ReleaseSCFPacket(void* pObj) {
  157. delete static_cast<SCFPacket*>(pObj);
  158. }
  159. SCF::SCF() {
  160. m_pTransferBuffer = nullptr;
  161. m_pRtoNBuffer = nullptr;
  162. m_pNotifybuffer = nullptr;
  163. m_pReceivebuffer = nullptr;
  164. m_hNotifyhandle = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET,false);
  165. m_hReceivehandle = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false);
  166. MaxComLen = SCF_MAX_LIMITED_BUFF_SIZE;
  167. NormalPackLen = 4096;
  168. ReadWaitTime = 20000;
  169. m_pTransferBuffer = (SCFPacket*)MallocSCFPacket();
  170. m_pRtoNBuffer = (SCFPacket*)MallocSCFPacket();
  171. if (!m_pTransferBuffer || !m_pRtoNBuffer) {
  172. // 清理已分配的资源
  173. if (m_pTransferBuffer) {
  174. ReleaseSCFPacket(m_pTransferBuffer);
  175. m_pTransferBuffer = nullptr;
  176. }
  177. if (m_pRtoNBuffer) {
  178. ReleaseSCFPacket(m_pRtoNBuffer);
  179. m_pRtoNBuffer = nullptr;
  180. }
  181. return;
  182. }
  183. m_pNotifybuffer = new CircleBuff();
  184. m_pReceivebuffer = new CircleBuff();
  185. if (!m_pNotifybuffer || !m_pReceivebuffer) {
  186. // 清理已分配的资源
  187. if (m_pNotifybuffer) {
  188. delete static_cast<CircleBuff*>(m_pNotifybuffer);
  189. m_pNotifybuffer = nullptr;
  190. }
  191. if (m_pReceivebuffer) {
  192. delete static_cast<CircleBuff*>(m_pReceivebuffer);
  193. m_pReceivebuffer = nullptr;
  194. }
  195. return;
  196. }
  197. SetBuffersize(NormalPackLen, MaxComLen);
  198. m_LibHandle = nullptr;
  199. }
  200. SCF::~SCF() {
  201. StopSCFServer();
  202. if (m_pRtoNBuffer) {
  203. ReleaseSCFPacket(m_pRtoNBuffer);
  204. m_pRtoNBuffer = nullptr;
  205. }
  206. if (m_pTransferBuffer) {
  207. ReleaseSCFPacket(m_pTransferBuffer);
  208. m_pTransferBuffer = nullptr;
  209. }
  210. if (m_pNotifybuffer) {
  211. delete static_cast<CircleBuff*>(m_pNotifybuffer);
  212. m_pNotifybuffer = nullptr;
  213. }
  214. if (m_pReceivebuffer) {
  215. delete static_cast<CircleBuff*>(m_pReceivebuffer);
  216. m_pReceivebuffer = nullptr;
  217. }
  218. }
  219. void SCF::SetBuffersize(uint32_t NormalPackSize, uint32_t MaxBuffLimit) {
  220. MaxComLen = MaxBuffLimit;
  221. NormalPackLen = NormalPackSize;
  222. m_RawBuffer.SetBuff(NormalPackLen);
  223. m_pTransferBuffer->SetBuffLen(NormalPackLen);
  224. m_pRtoNBuffer->SetBuffLen(NormalPackLen);
  225. static_cast<CircleBuff*>(m_pNotifybuffer)->SetBuffMaxLimit(MaxComLen);
  226. static_cast<CircleBuff*>(m_pReceivebuffer)->SetBuffMaxLimit(MaxComLen);
  227. }
  228. void SCF::InitSCF(const char* pLogFileName) {
  229. // Linux日志初始化
  230. }
  231. const char* SCF::GetConnectionType() {
  232. return nullptr;
  233. }
  234. ResDataObject SCF::GetConnectionAttributes() {
  235. return m_ConnectionParams;
  236. }
  237. bool SCF::SetSourceAttribute(ResDataObject& Sourceprameters) {
  238. return false;
  239. }
  240. int SCF::Connect(ResDataObject& Connectprameters, PacketParser callback,
  241. SCF_TRANSFERTYPE TransferType, uint32_t CommunicateTimeout) {
  242. if (TransferType == SCF_PACKET_TRANSFER && callback == nullptr) {
  243. return SCF_PARAMETER_ERR;
  244. }
  245. m_TransferType = TransferType;
  246. if (StartSCFServer()) {
  247. m_CommunicateTimeout = CommunicateTimeout;
  248. m_pPacketParser = callback;
  249. m_ConnectionParams = Connectprameters;
  250. return SCF_SUCCEED;
  251. }
  252. return SCF_FAILED;
  253. }
  254. void SCF::Disconnect() {
  255. StopSCFServer();
  256. m_pPacketParser = nullptr;
  257. m_ConnectionParams.clear();
  258. }
  259. bool SCF::isConnected() {
  260. return false;
  261. }
  262. bool SCF::StartSCFServer() {
  263. if (m_TransferType == SCF_PACKET_TRANSFER) {
  264. return StartThread();
  265. }
  266. return true;
  267. }
  268. void SCF::StopSCFServer() {
  269. StopThread();
  270. }
  271. int SCF::ProcessCmd(const char* data, int datalength) {
  272. if (!data || datalength <= 0) {
  273. return SCF_PARAMETER_ERR;
  274. }
  275. int bret = 0;
  276. uint32_t stop = 0;
  277. PACKET_RET ret = PACKET_ISPACKET;
  278. m_RawBuffer.Thread_Lock();
  279. bret = m_RawBuffer.Add(data, datalength);
  280. if (bret < 0) {
  281. m_RawBuffer.Thread_UnLock();
  282. return bret;
  283. }
  284. if (!m_pPacketParser) {
  285. m_RawBuffer.Thread_UnLock();
  286. return SCF_FAILED;
  287. }
  288. while (((ret == PACKET_ISPACKET) || (ret == PACKET_USELESS)) && (m_RawBuffer.GetPacketLen() > 0)) {
  289. ret = m_pPacketParser(m_RawBuffer.GetBuff(), m_RawBuffer.GetPacketLen(), stop);
  290. if (stop > m_RawBuffer.GetPacketLen() * 10) {
  291. // 防止异常大的stop值导致问题
  292. m_RawBuffer.ClearData();
  293. m_RawBuffer.Thread_UnLock();
  294. return SCF_OVERFLOW;
  295. }
  296. if (ret == PACKET_ISPACKET) {
  297. if (stop > 0) {
  298. if (stop > m_RawBuffer.GetPacketLen()) {
  299. m_RawBuffer.ClearData();
  300. m_RawBuffer.Thread_UnLock();
  301. return 0;
  302. }
  303. uint32_t result = SCFLock(0);
  304. if (result == WAIT_OBJECT_0) {
  305. if (m_pNotifybuffer) {
  306. uint32_t push_res = static_cast<CircleBuff*>(m_pNotifybuffer)->Push(m_RawBuffer.GetBuff(), stop);
  307. if (push_res == 0) {
  308. m_RawBuffer.Thread_UnLock();
  309. SCFUnLock();
  310. return SCF_NOMEMORY;
  311. }
  312. SCFUnLock();
  313. m_hNotifyhandle->SetEvent();
  314. }
  315. }
  316. else {
  317. if (m_pReceivebuffer) {
  318. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_Lock();
  319. if (static_cast<CircleBuff*>(m_pReceivebuffer)->Push(m_RawBuffer.GetBuff(), stop) == 0) {
  320. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_UnLock();
  321. m_RawBuffer.Thread_UnLock();
  322. return SCF_NOMEMORY;
  323. }
  324. m_hReceivehandle->SetEvent();
  325. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_UnLock();
  326. }
  327. }
  328. m_RawBuffer.Del(stop);
  329. }
  330. else {
  331. // 错误处理
  332. m_RawBuffer.ClearData();
  333. }
  334. }
  335. else if (ret == PACKET_USELESS) {
  336. if (stop > 0) {
  337. if (stop > m_RawBuffer.GetPacketLen()) {
  338. m_RawBuffer.ClearData();
  339. m_RawBuffer.Thread_UnLock();
  340. return 0;
  341. }
  342. m_RawBuffer.Del(stop);
  343. }
  344. else {
  345. m_RawBuffer.ClearData();
  346. }
  347. }
  348. }
  349. m_RawBuffer.Thread_UnLock();
  350. return bret;
  351. }
  352. int SCF::ReceivetoNotify() {
  353. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_Lock();
  354. while (static_cast<CircleBuff*>(m_pReceivebuffer)->GetPacketCount()) {
  355. uint32_t packetLen = static_cast<CircleBuff*>(m_pReceivebuffer)->GetFrontPacketSize();
  356. if (m_pTransferBuffer->SetBuffLen(packetLen)) {
  357. int ret = static_cast<CircleBuff*>(m_pReceivebuffer)->Pop((char*)(*m_pTransferBuffer), m_pTransferBuffer->GetBuffLen());
  358. if (ret > 0) {
  359. m_pTransferBuffer->SetPacketLen(ret);
  360. ret = static_cast<CircleBuff*>(m_pNotifybuffer)->Push((char*)(*m_pTransferBuffer), m_pTransferBuffer->GetPacketLen());
  361. if (ret == -1) {
  362. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_UnLock();
  363. return SCF_NOMEMORY;
  364. }
  365. m_hNotifyhandle->SetEvent();
  366. }
  367. }
  368. else {
  369. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_UnLock();
  370. return SCF_NOMEMORY;
  371. }
  372. }
  373. static_cast<CircleBuff*>(m_pReceivebuffer)->Thread_UnLock();
  374. return SCF_SUCCEED;
  375. }
  376. int SCF::ReceivetoNotify_Ex()
  377. {
  378. ((CircleBuff*)m_pReceivebuffer)->Thread_Lock();
  379. m_hReceivehandle->ResetEvent();
  380. while (((CircleBuff*)m_pReceivebuffer)->GetPacketCount())
  381. {
  382. DWORD packetLen = ((CircleBuff*)m_pReceivebuffer)->GetFrontPacketSize();
  383. if (m_pRtoNBuffer->SetBuffLen(packetLen))
  384. {
  385. int ret = ((CircleBuff*)m_pReceivebuffer)->Pop((char*)(*m_pRtoNBuffer), m_pRtoNBuffer->GetBuffLen());
  386. if (ret > 0)
  387. {
  388. //copy it
  389. m_pRtoNBuffer->SetPacketLen((DWORD)ret);
  390. ret = ((CircleBuff*)m_pNotifybuffer)->Push((char*)(*m_pRtoNBuffer), m_pRtoNBuffer->GetPacketLen());
  391. if (ret == (DWORD)-1)
  392. {
  393. ((CircleBuff*)m_pReceivebuffer)->Thread_UnLock();
  394. //PRINTA_ERROR(m_pSCFLogger, "No memory for push Notify ");
  395. return SCF_NOMEMORY;
  396. }
  397. m_hNotifyhandle->SetEvent();
  398. }
  399. //ret == 0,ignore it
  400. //ret == -1,not gonna happen
  401. }
  402. else
  403. {
  404. ((CircleBuff*)m_pReceivebuffer)->Thread_UnLock();
  405. //PRINTA_ERROR(m_pSCFLogger, "No memory for push Notify ");
  406. return SCF_NOMEMORY;
  407. }
  408. }
  409. ((CircleBuff*)m_pReceivebuffer)->Thread_UnLock();
  410. return SCF_SUCCEED;
  411. }
  412. int SCF::WritetoNotify(const char* pPacket, int length) {
  413. uint32_t ret = static_cast<CircleBuff*>(m_pNotifybuffer)->Push(pPacket, length);
  414. if (ret == (uint32_t)-1) {
  415. return SCF_NOMEMORY;
  416. }
  417. m_hNotifyhandle->SetEvent();
  418. return SCF_SUCCEED;
  419. }
  420. bool SCF::Exec() {
  421. if (!isConnected()|| m_TransferType == SCF_NORMAL_TRANSFER) return false;
  422. if (SCFLock(0) == WAIT_OBJECT_0) {
  423. if (ReceivetoNotify() < 0) {
  424. Disconnect();
  425. SCFUnLock();
  426. return false;
  427. }
  428. SCFUnLock();
  429. }
  430. int ret = ReadData((char*)(*m_pTransferBuffer),
  431. m_pTransferBuffer->GetBuffLen(),
  432. ReadWaitTime);
  433. if (ret > 0) {
  434. ret = ProcessCmd((char*)(*m_pTransferBuffer), ret);
  435. if (ret < 0) {
  436. Disconnect();
  437. return false;
  438. }
  439. }
  440. else if (ret != SCF_TIMEOUT) {
  441. return false;
  442. }
  443. return true;
  444. }
  445. int SCF::SendPacket(const char* pPacket, uint32_t length, uint32_t timeout) {
  446. return SCF_FAILED;
  447. }
  448. int SCF::SendPacket(SCFPacket* pPacket, uint32_t timeout) {
  449. return SCF_FAILED;
  450. }
  451. int SCF::ReadData(char* pPacket, uint32_t length, uint32_t timeout) {
  452. // std::cout << "SCF::ReadData" << std::endl;
  453. return SCF_FAILED;
  454. }
  455. uint32_t SCF::GetReceivePacketSize() {
  456. if (!m_pReceivebuffer) return 0;
  457. if (static_cast<CircleBuff*>(m_pReceivebuffer)->GetPacketCount() > 0) {
  458. return static_cast<CircleBuff*>(m_pReceivebuffer)->GetFrontPacketSize();
  459. }
  460. return 0;
  461. }
  462. uint32_t SCF::GetNotifyPacketSize() {
  463. if (!m_pNotifybuffer) return 0;
  464. if (static_cast<CircleBuff*>(m_pNotifybuffer)->GetPacketCount() > 0) {
  465. return static_cast<CircleBuff*>(m_pNotifybuffer)->GetFrontPacketSize();
  466. }
  467. return 0;
  468. }
  469. int SCF::ReceivePacket(char* pPacket, uint32_t length, uint32_t timeout) {
  470. if (m_TransferType == SCF_NORMAL_TRANSFER) {
  471. return ReadData(pPacket, length, timeout);
  472. }
  473. return DeQueNotifyPacket(pPacket, length,timeout);
  474. /*int ret = static_cast<CircleBuff*>(m_pReceivebuffer)->Pop(pPacket, length);
  475. if (ret > 0) return ret;
  476. if (ret == 0) {
  477. if (!m_hReceivehandle->Wait(timeout)) {
  478. return SCF_TIMEOUT;
  479. }
  480. ret = static_cast<CircleBuff*>(m_pReceivebuffer)->Pop(pPacket, length);
  481. if (ret > 0) return ret;
  482. if (ret == 0) return SCF_NOPACKET;
  483. }
  484. return SCF_OVERFLOW;*/
  485. }
  486. int SCF::ReceivePacket(SCFPacket* pPacket, uint32_t timeout) {
  487. if (m_TransferType == SCF_NORMAL_TRANSFER) {
  488. int ret = ReadData((char*)(*pPacket), pPacket->GetBuffLen(), timeout);
  489. if (ret > 0) pPacket->SetPacketLen(ret);
  490. else pPacket->SetPacketLen(0);
  491. return ret;
  492. }
  493. int ret = ReceivePacket((char*)(*pPacket), pPacket->GetBuffLen(), timeout);
  494. if (ret >= 0) pPacket->SetPacketLen(ret);
  495. else pPacket->SetPacketLen(0);
  496. if (ret == SCF_OVERFLOW) {
  497. if (pPacket->SetBuffLen(GetReceivePacketSize())) {
  498. ret = ReceivePacket((char*)(*pPacket), pPacket->GetBuffLen(), 0);
  499. if (ret >= 0) pPacket->SetPacketLen(ret);
  500. else pPacket->SetPacketLen(0);
  501. }
  502. else {
  503. ret = SCF_NOMEMORY;
  504. }
  505. }
  506. return ret;
  507. }
  508. int SCF::QueNotifyPacket(const char* pPacket, uint32_t length) {
  509. if (!m_pNotifybuffer) return SCF_FAILED;
  510. return WritetoNotify(pPacket, length);
  511. }
  512. int SCF::QueNotifyPacket(SCFPacket* pPacket) {
  513. return QueNotifyPacket((char*)(*pPacket), pPacket->GetPacketLen());
  514. }
  515. int SCF::DeQueNotifyPacket(char* pPacket, uint32_t length, uint32_t timeout) {
  516. int ret = static_cast<CircleBuff*>(m_pNotifybuffer)->Pop(pPacket, length);
  517. if (ret > 0) return ret;
  518. if (ret == 0) {
  519. if (!m_hNotifyhandle->Wait(timeout)) {
  520. return SCF_TIMEOUT;
  521. }
  522. ret = static_cast<CircleBuff*>(m_pNotifybuffer)->Pop(pPacket, length);
  523. if (ret > 0) return ret;
  524. if (ret == 0) return SCF_NOPACKET;
  525. }
  526. return SCF_OVERFLOW;
  527. }
  528. int SCF::DeQueNotifyPacket(SCFPacket* pPacket, uint32_t timeout) {
  529. int ret = DeQueNotifyPacket((char*)(*pPacket), pPacket->GetBuffLen(), timeout);
  530. if (ret >= 0) pPacket->SetPacketLen(ret);
  531. else pPacket->SetPacketLen(0);
  532. if (ret == SCF_OVERFLOW) {
  533. if (pPacket->SetBuffLen(GetNotifyPacketSize())) {
  534. ret = DeQueNotifyPacket((char*)(*pPacket), pPacket->GetBuffLen(), 0);
  535. if (ret >= 0) pPacket->SetPacketLen(ret);
  536. else pPacket->SetPacketLen(0);
  537. }
  538. else {
  539. ret = SCF_NOMEMORY;
  540. }
  541. }
  542. return ret;
  543. }
  544. uint32_t SCF::SCFLock(uint32_t timeout) {
  545. uint32_t ret = WAIT_OBJECT_0;
  546. ret = Thread_Lock(timeout);
  547. return ret;
  548. }
  549. void SCF::SCFUnLock() {
  550. if (ReceivetoNotify_Ex() < 0) {
  551. Disconnect();
  552. }
  553. Thread_UnLock();
  554. }
  555. SCF* LoadScf(const char* pFilename) {
  556. SCF* pScf = nullptr;
  557. if (pFilename) {
  558. void* module = dlopen(pFilename, RTLD_LAZY);
  559. if (module) {
  560. GetSCF_Proc getscf = (GetSCF_Proc)dlsym(module, "GetSCF");
  561. if (getscf) {
  562. pScf = getscf();
  563. if (pScf) {
  564. pScf->m_LibHandle = module;
  565. }
  566. }
  567. }
  568. }
  569. return pScf;
  570. }
  571. void UnLoadScf(SCF* pHandle) {
  572. if (pHandle) {
  573. void* mod = pHandle->m_LibHandle;
  574. if (mod) {
  575. ReleaseSCF_Proc releasescf = (ReleaseSCF_Proc)dlsym(pHandle->m_LibHandle, "ReleaseSCF");
  576. if (releasescf) {
  577. releasescf(pHandle);
  578. }
  579. dlclose(mod);
  580. }
  581. }
  582. }