OSDN Git Service

try Ajax-powered popup window of mp4 files on mode=list
[rec10/rec10-git.git] / b25-remote / CasServer.cpp
1 // CasServer.cpp: CCasServer クラスのインプリメンテーション
2 //
3 //////////////////////////////////////////////////////////////////////
4
5
6 #include "CasServer.h"
7 #include "BCasCard.h"
8
9 #define FALSE 0
10 #define TRUE 1
11
12 CCasServer::CCasServer(ICasServerHandler *pEventHandler)
13         : m_pEventHandler(pEventHandler)
14         , m_hServerThread(NULL)
15 {
16
17 }
18
19 CCasServer::~CCasServer(void)
20 {
21         CloseServer();
22 }
23
24 const BOOL CCasServer::OpenServer(const WORD wServerPort)
25 {
26         // カードリーダ存在チェック
27         CBcasCard BCasCard;
28         if(!BCasCard.OpenCard())return FALSE;
29
30         // サーバソケットオープン
31         if(!m_pSocket.Listen(wServerPort))return FALSE;
32
33         // サーバスレッド起動
34         pthread_create(&m_hServerThread, NULL, CCasServer::ServerThreadRaw, (LPVOID)this);
35         if(!m_hServerThread){
36                 m_pSocket.Close();
37                 return FALSE;
38         }
39
40         return TRUE;
41 }
42
43 void CCasServer::CloseServer(void)
44 {
45         // サーバソケットクローズ
46         m_pSocket.Close();
47
48         // スレッドハンドル開放
49         if(m_hServerThread){
50                 pthread_join(m_hServerThread, NULL);
51                 m_hServerThread = NULL;
52         }
53
54         // 全クライアント切断
55         m_Lock.Lock();
56         
57         for(ClientList::iterator It = m_ClientList.begin() ; It != m_ClientList.end() ; It++){
58                 It->first->CloseClient();
59                 }
60
61         m_Lock.Unlock();
62         
63         // 全クライアントの終了を待つ
64         while(m_ClientList.size()) sleep(1UL);
65 }
66
67 const DWORD CCasServer::GetClientNum(void) const
68 {
69         // 接続中のクライアント数を返す
70         return m_ClientList.size();
71 }
72
73 void CCasServer::OnCasClientEvent(CCasClient *pClient, const DWORD dwEventID, PVOID pParam)
74 {
75         CBlockLock AutoLock(&m_Lock);
76
77         // クライアントイベント
78         switch(dwEventID){
79                 case CCasClient::EID_CONNECTED :
80                         // リストに追加
81                         m_ClientList[pClient] = pClient;
82                         if (m_pEventHandler) m_pEventHandler->OnCasServerEvent(this, CSEI_CONNECTED);
83                         break;
84                         
85                 case CCasClient::EID_DISCONNECTED :
86                         // リストから削除
87                         m_ClientList.erase(pClient);
88                         if (m_pEventHandler) m_pEventHandler->OnCasServerEvent(this, CSEI_DISCONNECTED);
89                         break;
90                 }
91 }
92
93 void CCasServer::ServerThread(void)
94 {
95         // アクセプトループ
96         CSmartSock *pNewSocket;
97         
98         while(pNewSocket = m_pSocket.Accept()){
99                 // クライアントインスタンス生成
100                 new CCasClient(this, pNewSocket);
101                 }
102 }
103
104 void* CCasServer::ServerThreadRaw(LPVOID pParam)
105 {
106         // サーバスレッド
107         static_cast<CCasServer *>(pParam)->ServerThread();
108
109         return 0UL;
110 }