1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 /* Manager which acts as client for the chat cleaner service. */
32 
33 #ifndef _CHATCLEANERMANAGER_H_
34 #define _CHATCLEANERMANAGER_H_
35 
36 #include <boost/asio.hpp>
37 #include <boost/enable_shared_from_this.hpp>
38 #include <string>
39 #include <net/chatcleanercallback.h>
40 
41 #define CLEANER_NET_HEADER_SIZE		4
42 #define MAX_CLEANER_PACKET_SIZE		512
43 #define CLEANER_PROTOCOL_VERSION	2
44 
45 class AsioSendBuffer;
46 class ChatCleanerMessage;
47 
48 class ChatCleanerManager : public boost::enable_shared_from_this<ChatCleanerManager>
49 {
50 public:
51 	ChatCleanerManager(ChatCleanerCallback &cb, boost::shared_ptr<boost::asio::io_service> ioService);
52 	virtual ~ChatCleanerManager();
53 
54 	void Init(const std::string &serverAddr, int port, bool ipv6,
55 			  const std::string &clientSecret, const std::string &serverSecret);
56 	void ReInit();
57 	void HandleLobbyChatText(unsigned playerId, const std::string &name, const std::string &text);
58 	void HandleGameChatText(unsigned gameId, unsigned playerId, const std::string &name, const std::string &text);
59 
60 protected:
61 
62 	void HandleResolve(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
63 	void HandleConnect(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
64 	void HandleRead(const boost::system::error_code &ec, size_t bytesRead);
65 	bool HandleMessage(ChatCleanerMessage &msg);
66 
67 	void SendMessageToServer(ChatCleanerMessage &msg);
68 	unsigned GetNextRequestId();
69 
70 private:
71 
72 	ChatCleanerCallback &m_callback;
73 	boost::shared_ptr<boost::asio::io_service> m_ioService;
74 	boost::shared_ptr<boost::asio::ip::tcp::resolver> m_resolver;
75 	boost::shared_ptr<boost::asio::ip::tcp::socket> m_socket;
76 	boost::shared_ptr<AsioSendBuffer> m_sendManager;
77 
78 	bool m_connected;
79 	unsigned m_curRequestId;
80 	std::string m_serverAddr;
81 	int m_serverPort;
82 	bool m_useIpv6;
83 	std::string m_clientSecret;
84 	std::string m_serverSecret;
85 	unsigned char m_recvBuf[2*MAX_CLEANER_PACKET_SIZE];
86 	size_t m_recvBufUsed;
87 };
88 
89 #endif
90