This repository was archived by the owner on Sep 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNiconamaClient.cpp
More file actions
189 lines (155 loc) · 4.62 KB
/
NiconamaClient.cpp
File metadata and controls
189 lines (155 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright (c) 2011, Yota Ichino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "NiconamaClient.h"
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <iostream>
#include <queue>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
class NiconamaClient::impl {
public:
/*
\brief ニコ生のコメントサーバへの接続
\param addr アドレス
<addr> msgNNN.live.nicovideo.jp </addr>
\param port ポート番号
<port> ポート番号 </port>
\retval 接続できたらtrue 接続できなかったらfalseを返却
ニコ生のホスト名を含んでいないとfalseを返却
*/
bool connect(const std::string &addr, const std::string &port)
{
bool ret = true;
// ニコ生専用なので、ニコ生のホスト名を含んでいないと接続させない
if(!boost::contains(addr, "live.nicovideo.jp")) {
ret = false;
goto end;
}
socket.connect(addr, port);
if(socket.fail()) {
ret = false;
}
end:
return ret;
}
/*
\brief コメントの要請
\param thread コメントのスレッドID
\param numOfComments 初めに取得するコメントの個数
<thread> コメントのスレッドID </thread>
*/
void requestComment(const std::string &thread_elem, const std::string &numOfComments)
{
socket << "<thread thread=\"" << thread_elem << "\" version=\"20061206\" res_from=\"-" << numOfComments << "\"/>" << '\0';
socket.flush();
// boost::thread_group::create_threadの実装を参考にした
com_thread = new boost::thread(boost::bind(&NiconamaClient::impl::getCommentsThread, this));
}
/*
\brief コメントの取得
\retval コメント (ユーザ情報などを含んだxml形式)
*/
std::string getComment()
{
std::string ret;
boost::mutex::scoped_lock lk(guard);
if(comments_qu.empty()) {
ret = "";
goto end;
}
ret = comments_qu.front();
comments_qu.pop();
end:
return ret;
}
/*
\brief コメントサーバとの切断
*/
void close()
{
boost::mutex::scoped_lock lk(guard);
end_flag = true;
socket.close();
}
impl() : end_flag(false), com_thread(NULL)
{
}
~impl()
{
close();
if(com_thread) {
com_thread->join();
delete com_thread;
com_thread = NULL;
}
}
private:
boost::asio::ip::tcp::iostream socket;
std::queue<std::string> comments_qu; // コメントを格納
boost::mutex guard; // スレッドの
volatile bool end_flag;
boost::thread *com_thread;
void getCommentsThread()
{
while(1) {
// スレッドを起動中、socketメンバはこの関数内からしか呼ばれないのでmutexはいらない
if(socket.fail()) {
// comments_quは他のメンバ関数から呼ばれるのでmutexが必要
boost::mutex::scoped_lock lk(guard);
comments_qu.push("/failure");
break;
}
std::string comment_xml; // 取得したコメントのXML 戻り値
getline(socket, comment_xml, '\0');
// *****</chat>\0<chat thread=******
// '\0'で1コメントの区切りだから
{
boost::mutex::scoped_lock lk(guard);
if(end_flag) {
break;
}
comments_qu.push(comment_xml);
}
}
}
};
NiconamaClient::NiconamaClient() : pImpl(new NiconamaClient::impl())
{
}
bool NiconamaClient::connect(const std::string &addr, const std::string &port)
{
return pImpl->connect(addr, port);
}
void NiconamaClient::requestComment(const std::string &thread_elem, const std::string &numOfComments)
{
pImpl->requestComment(thread_elem, numOfComments);
}
std::string NiconamaClient::getComment()
{
return pImpl->getComment();
}
void NiconamaClient::close()
{
pImpl->close();
}