From df82a1e33d0f060e91102b1e154418c1c0fb8e79 Mon Sep 17 00:00:00 2001 From: Benjamin Jemlich Date: Sat, 6 Mar 2010 21:53:22 +0100 Subject: [PATCH] More code reorganization and some test callbacks --- client.cc | 58 ++++++++++++++++++++++++++++++++++++++------------- client.h | 25 ++++++++++++++++++++-- client_lib.cc | 4 ++++ client_lib.h | 4 ++++ main.cc | 40 ++++++++++++++++++++++++++++++++--- messages.h | 4 ++++ settings.h | 20 +++++++++--------- 7 files changed, 125 insertions(+), 30 deletions(-) diff --git a/client.cc b/client.cc index 57864cc..7dbda4c 100644 --- a/client.cc +++ b/client.cc @@ -9,8 +9,8 @@ #include "settings.h" -using mumble_message::MessageHeader; -using mumble_message::Message; +using MumbleClient::mumble_message::MessageHeader; +using MumbleClient::mumble_message::Message; /////////////////////////////////////////////////////////////////////////////// @@ -31,6 +31,8 @@ template T ConstructProtobufObject(void* buffer, int32_t length, bool } // namespace +namespace MumbleClient { + /////////////////////////////////////////////////////////////////////////////// // MumbleClient, private: @@ -45,7 +47,7 @@ void MumbleClient::DoPing(const boost::system::error_code& error) { MumbleProto::Ping p; p.set_timestamp(std::time(NULL)); - sendMessage(PbMessageType::Ping, p, false); + SendMessage(PbMessageType::Ping, p, false); // Requeue ping if (!ping_timer_) @@ -65,6 +67,12 @@ void MumbleClient::ParseMessage(const MessageHeader& msg_header, void* buffer) { MumbleProto::Ping p = ConstructProtobufObject(buffer, msg_header.length, false); break; } + case PbMessageType::TextMessage: { + MumbleProto::TextMessage tm = ConstructProtobufObject(buffer, msg_header.length, true); + + text_message_callback_(tm.message()); + break; + } case PbMessageType::CryptSetup: { MumbleProto::CryptSetup cs = ConstructProtobufObject(buffer, msg_header.length, true); break; @@ -76,15 +84,16 @@ void MumbleClient::ParseMessage(const MessageHeader& msg_header, void* buffer) { case PbMessageType::ServerSync: { MumbleProto::ServerSync ss = ConstructProtobufObject(buffer, msg_header.length, true); state_ = kStateAuthenticated; + session_ = ss.session(); + // Enqueue ping DoPing(boost::system::error_code()); + + auth_callback_(); break; } case PbMessageType::UDPTunnel: { - std::fstream fs("udptunnel.out", std::fstream::app | std::fstream::out | std::fstream::binary); - fs.write(reinterpret_cast(&msg_header.length), 4); - fs.write(reinterpret_cast(buffer), msg_header.length); - fs.close(); + raw_udp_tunnel_callback_(msg_header.length, buffer); break; } default: @@ -121,12 +130,12 @@ MumbleClient::~MumbleClient() { delete udp_socket_; } -void MumbleClient::Connect() { +void MumbleClient::Connect(const Settings& s) { // Resolve hostname - std::cerr << "Resolving host " << Settings::getHost() << std::endl; + std::cerr << "Resolving host " << s.getHost() << std::endl; tcp::resolver resolver(*io_service_); - tcp::resolver::query query(Settings::getHost(), Settings::getPort()); + tcp::resolver::query query(s.getHost(), s.getPort()); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::resolver::iterator end; @@ -180,14 +189,14 @@ void MumbleClient::Connect() { MumbleProto::Version v; v.set_version(MUMBLE_VERSION(1, 2, 2)); v.set_release("0.0.1-dev"); - sendMessage(PbMessageType::Version, v, true); + SendMessage(PbMessageType::Version, v, true); MumbleProto::Authenticate a; - a.set_username(Settings::getUserName()); - a.set_password(Settings::getPassword()); + a.set_username(s.getUserName()); + a.set_password(s.getPassword()); // FIXME: hardcoded version number a.add_celt_versions(0x8000000b); - sendMessage(PbMessageType::Authenticate, a, true); + SendMessage(PbMessageType::Authenticate, a, true); tcp_socket_->async_read_some(boost::asio::null_buffers(), boost::bind(&MumbleClient::ReadWriteHandler, this, boost::asio::placeholders::error)); } @@ -223,7 +232,7 @@ void MumbleClient::ReadWriteHandler(const boost::system::error_code& error) { tcp_socket_->async_read_some(boost::asio::null_buffers(), boost::bind(&MumbleClient::ReadWriteHandler, this, boost::asio::placeholders::error)); } -void MumbleClient::sendMessage(PbMessageType::MessageType type, const ::google::protobuf::Message& new_msg, bool print) { +void MumbleClient::SendMessage(PbMessageType::MessageType type, const ::google::protobuf::Message& new_msg, bool print) { if (print) { std::cout << "<< ENQUEUE: " << type << std::endl; new_msg.PrintDebugString(); @@ -250,3 +259,22 @@ void MumbleClient::sendMessage(PbMessageType::MessageType type, const ::google:: std::cout << "<< ASYNC Type: " << ntohs(msg.header_.type) << " Length: 6+" << msg.msg_.size() << std::endl; } } + +void MumbleClient::SetComment(const std::string& text) { + BOOST_ASSERT(state_ >= kStateAuthenticated); + + MumbleProto::UserState us; + us.set_session(session_); + us.set_comment(text); + + SendMessage(PbMessageType::UserState, us, true); +} + +void MumbleClient::SendRawUdpTunnel(const char* buffer, int32_t len) { + MumbleProto::UDPTunnel ut; + ut.set_packet(buffer, len); + + SendMessage(PbMessageType::UDPTunnel, ut, true); +} + +} // end namespace MumbleClient diff --git a/client.h b/client.h index bd22aae..521c853 100644 --- a/client.h +++ b/client.h @@ -10,12 +10,16 @@ #include "messages.h" #include "Mumble.pb.h" +namespace MumbleClient { + using boost::asio::ip::tcp; using boost::asio::ip::udp; using boost::asio::ssl::stream; #define SSL 1 +class Settings; + namespace mumble_message { #pragma pack(push) @@ -36,6 +40,10 @@ struct Message { } // namespace mumble_message +typedef boost::function TextMessageCallbackType; +typedef boost::function AuthCallbackType; +typedef boost::function RawUdpTunnelCallbackType; + class MumbleClient { enum State { kStateNew, @@ -45,8 +53,14 @@ class MumbleClient { public: ~MumbleClient(); - void Connect(); - void sendMessage(PbMessageType::MessageType type, const ::google::protobuf::Message& msg, bool print); + void Connect(const Settings& s); + void SendMessage(PbMessageType::MessageType type, const ::google::protobuf::Message& msg, bool print); + void SetComment(const std::string& text); + void SendRawUdpTunnel(const char* buffer, int32_t len); + + void SetTextMessageCallback(TextMessageCallbackType tm) { text_message_callback_ = tm; }; + void SetAuthCallback(AuthCallbackType a) { auth_callback_ = a; }; + void SetRawUdpTunnelCallback(RawUdpTunnelCallbackType rut) { raw_udp_tunnel_callback_ = rut; }; private: friend class MumbleClientLib; @@ -67,9 +81,16 @@ class MumbleClient { #endif udp::socket* udp_socket_; boost::asio::deadline_timer* ping_timer_; + int32_t session_; + + TextMessageCallbackType text_message_callback_; + AuthCallbackType auth_callback_; + RawUdpTunnelCallbackType raw_udp_tunnel_callback_; MumbleClient(const MumbleClient&); void operator=(const MumbleClient&); }; +} // end namespace MumbleClient + #endif // CLIENT_H_ diff --git a/client_lib.cc b/client_lib.cc index d5a4058..187e467 100644 --- a/client_lib.cc +++ b/client_lib.cc @@ -5,6 +5,8 @@ #include "client.h" #include "settings.h" +namespace MumbleClient { + /////////////////////////////////////////////////////////////////////////////// // static @@ -38,3 +40,5 @@ MumbleClient* MumbleClientLib::NewClient() { void MumbleClientLib::Run() { io_service_.run(); } + +} // end namespace MumbleClient diff --git a/client_lib.h b/client_lib.h index cde7e46..fd395ce 100644 --- a/client_lib.h +++ b/client_lib.h @@ -3,6 +3,8 @@ #include +namespace MumbleClient { + class MumbleClient; class MumbleClientLib { @@ -22,4 +24,6 @@ class MumbleClientLib { void operator=(const MumbleClientLib&); }; +} // end namespace MumbleClient + #endif // CLIENT_LIB_H_ diff --git a/main.cc b/main.cc index 4b0a005..01c5c08 100644 --- a/main.cc +++ b/main.cc @@ -1,14 +1,48 @@ #include +#include #include "client.h" #include "client_lib.h" +#include "settings.h" + +bool recording = false; + +void AuthCallback() { + std::cout << "I'm authenticated" << std::endl; +} + +void TextMessageCallback(const std::string& message, MumbleClient::MumbleClient *mc) { + if (message == "record") { + recording = true; + } else if (message == "stop") { + recording = false; + } + + std::cout << "TM: " << message << std::endl; + mc->SetComment(message); +} + +void RawUdpTunnelCallback(int32_t length, void* buffer) { + if (!recording) + return; + + std::fstream fs("udptunnel.out", std::fstream::app | std::fstream::out | std::fstream::binary); + fs.write(reinterpret_cast(length), 4); + fs.write(reinterpret_cast(buffer), length); + fs.close(); +} int main(int /* argc */, char** /* argv[] */) { - MumbleClientLib* mcl = MumbleClientLib::instance(); + MumbleClient::MumbleClientLib* mcl = MumbleClient::MumbleClientLib::instance(); + MumbleClient::Settings s; // Create a new client - MumbleClient* mc = mcl->NewClient(); - mc->Connect(); + MumbleClient::MumbleClient* mc = mcl->NewClient(); + mc->Connect(s); + + mc->SetAuthCallback(boost::bind(&AuthCallback)); + mc->SetTextMessageCallback(boost::bind(&TextMessageCallback, _1, mc)); + mc->SetRawUdpTunnelCallback(boost::bind(&RawUdpTunnelCallback, _1, _2)); // Start event loop mcl->Run(); diff --git a/messages.h b/messages.h index 2131fca..cf1b817 100644 --- a/messages.h +++ b/messages.h @@ -1,6 +1,8 @@ #ifndef MESSAGES_H_ #define MESSAGES_H_ +namespace MumbleClient { + namespace PbMessageType { enum MessageType { Version, @@ -31,4 +33,6 @@ namespace PbMessageType { }; } +} // end namespace MumbleClient + #endif // MESSAGES_H_ diff --git a/settings.h b/settings.h index 00a3e5d..7f95a8d 100644 --- a/settings.h +++ b/settings.h @@ -1,33 +1,33 @@ #ifndef SETTINGS_H_ #define SETTINGS_H_ +namespace MumbleClient { + class Settings { public: - - static std::string getSSLDbPath() { - return "sql:."; - } - - static std::string getHost() { + std::string getHost() const { return "0xy.de"; } - static std::string getPort() { + std::string getPort() const { return "64739"; } - static std::string getUserName() { + std::string getUserName() const { return "testBot"; } - static std::string getPassword() { + std::string getPassword() const { return ""; } - +/* private: Settings(); Settings(const Settings&); void operator=(const Settings&); +*/ }; +} // end namespace MumbleClient + #endif // SETTINGS_H_