OSDN Git Service

stop using trunk directory in rectool
[rec10/rec10-git.git] / dist / trunk / b25-remote / CasClient.cpp
1 // CasClient.cpp: CCasClient \83N\83\89\83X\82Ì\83C\83\93\83v\83\8a\83\81\83\93\83e\81[\83V\83\87\83\93\r
2 //\r
3 //////////////////////////////////////////////////////////////////////\r
4 \r
5 #include <stdio.h>\r
6 #include "W2L.h"\r
7 #include "CasClient.h"
8 \r
9 \r
10 #define TCP_TIMEOUT             1000UL          // 1\95b\r
11 \r
12 \r
13 CCasClient::CCasClient(ICasClientHandler *pEventHandler, CSmartSock *pSocket)\r
14         : CBcasCard()\r
15         , m_pSocket(pSocket)\r
16         , m_pEventHandler(pEventHandler)\r
17         , m_hClientThread(NULL)\r
18 {\r
19         // \83N\83\89\83C\83A\83\93\83g\83X\83\8c\83b\83h\8bN\93®\r
20         pthread_create(&m_hClientThread, NULL, CCasClient::ClientThreadRaw, (LPVOID)this);\r
21         if(!m_hClientThread){\r
22                 printf("ClientThread failed\n");
23                 delete this;\r
24         }\r
25         printf("ClientThread started\n");
26 }\r
27 \r
28 CCasClient::~CCasClient(void)\r
29 {\r
30         delete m_pSocket;\r
31         if (m_hClientThread) pthread_join(m_hClientThread, NULL);\r
32 }\r
33 \r
34 void CCasClient::CloseClient(void)\r
35 {\r
36         // \83N\83\89\83C\83A\83\93\83g\82ð\90Ø\92f\82·\82é\r
37         m_pSocket->Close();\r
38 }\r
39 \r
40 void CCasClient::ClientThread(void)\r
41 {\r
42         // \83J\81[\83h\83\8a\81[\83_\82ð\8aJ\82­\r
43         if(!OpenCard())return;\r
44         \r
45         // \8eó\90M\83o\83b\83t\83@\r
46         BYTE byDataLen;\r
47         BYTE RecvBuf[256];\r
48         DWORD dwRecvSize;\r
49         \r
50         // \83\81\83b\83Z\81[\83W\8eó\90M\83\8b\81[\83v\r
51         while(1){\r
52                 // \83w\83b\83_\82ð\8eó\90M\r
53                 if(!m_pSocket->Recv(&byDataLen, 1UL, TCP_TIMEOUT)){\r
54                         if(m_pSocket->GetLastError() == CSmartSock::EC_TIMEOUT)continue;\r
55                         else break;\r
56                 }
57 \r
58                 // \83f\81[\83^\82ð\8eó\90M\r
59                 if(!m_pSocket->Recv(RecvBuf, byDataLen, TCP_TIMEOUT))break;
60         \r
61                 // \83J\81[\83h\91\97\8eó\90M\r
62                 if(!TransmitCommand(RecvBuf, byDataLen, &RecvBuf[1], sizeof(RecvBuf) - 1UL, &dwRecvSize))dwRecvSize = 0UL;
63 \r
64                 // \83f\81[\83^\82ð\91\97\90M\r
65                 RecvBuf[0] = (BYTE)dwRecvSize;\r
66                 if(!m_pSocket->Send(RecvBuf, dwRecvSize + 1UL, TCP_TIMEOUT))break;
67         }\r
68 \r
69         printf("ClientThread exited\n");
70         // \83J\81[\83h\83\8a\81[\83_\82ð\95Â\82\82é\r
71         CloseCard();\r
72 }\r
73 \r
74 void* CCasClient::ClientThreadRaw(LPVOID pParam)\r
75 {\r
76         // \83N\83\89\83C\83A\83\93\83g\83X\83\8c\83b\83h\r
77         CCasClient *pThis = static_cast<CCasClient *>(pParam);\r
78 \r
79         // \90Ú\91±\83C\83x\83\93\83g\92Ê\92m\r
80         pThis->SendEvent(EID_CONNECTED);
81 \r
82         // \83\81\83\93\83o\8aÖ\90\94\82É\83\8a\83_\83C\83\8c\83N\83g\82·\82é\r
83         pThis->ClientThread();
84 \r
85         // \90Ø\92f\83C\83x\83\93\83g\92Ê\92m\r
86         pThis->SendEvent(EID_DISCONNECTED);
87 \r
88         delete pThis;
89 \r
90         return 0UL;\r
91 }\r
92 \r
93 void CCasClient::SendEvent(const DWORD dwEventID, PVOID pParam)\r
94 {\r
95         // \83n\83\93\83h\83\89\82É\83C\83x\83\93\83g\82ð\92Ê\92m\82·\82é\r
96         if(m_pEventHandler)m_pEventHandler->OnCasClientEvent(this, dwEventID, pParam);\r
97 }\r