Skip to content

Commit

Permalink
More code reorganization and some test callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
pcgod committed Mar 6, 2010
1 parent 3fef88c commit df82a1e
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 30 deletions.
58 changes: 43 additions & 15 deletions client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

///////////////////////////////////////////////////////////////////////////////

Expand All @@ -31,6 +31,8 @@ template <class T> T ConstructProtobufObject(void* buffer, int32_t length, bool
} // namespace


namespace MumbleClient {

///////////////////////////////////////////////////////////////////////////////
// MumbleClient, private:

Expand All @@ -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_)
Expand All @@ -65,6 +67,12 @@ void MumbleClient::ParseMessage(const MessageHeader& msg_header, void* buffer) {
MumbleProto::Ping p = ConstructProtobufObject<MumbleProto::Ping>(buffer, msg_header.length, false);
break;
}
case PbMessageType::TextMessage: {
MumbleProto::TextMessage tm = ConstructProtobufObject<MumbleProto::TextMessage>(buffer, msg_header.length, true);

text_message_callback_(tm.message());
break;
}
case PbMessageType::CryptSetup: {
MumbleProto::CryptSetup cs = ConstructProtobufObject<MumbleProto::CryptSetup>(buffer, msg_header.length, true);
break;
Expand All @@ -76,15 +84,16 @@ void MumbleClient::ParseMessage(const MessageHeader& msg_header, void* buffer) {
case PbMessageType::ServerSync: {
MumbleProto::ServerSync ss = ConstructProtobufObject<MumbleProto::ServerSync>(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<const char *>(&msg_header.length), 4);
fs.write(reinterpret_cast<char *>(buffer), msg_header.length);
fs.close();
raw_udp_tunnel_callback_(msg_header.length, buffer);
break;
}
default:
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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();
Expand All @@ -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
25 changes: 23 additions & 2 deletions client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,6 +40,10 @@ struct Message {
} // namespace mumble_message


typedef boost::function<void (const std::string& text)> TextMessageCallbackType;
typedef boost::function<void ()> AuthCallbackType;
typedef boost::function<void (int32_t length, void* buffer)> RawUdpTunnelCallbackType;

class MumbleClient {
enum State {
kStateNew,
Expand All @@ -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;
Expand All @@ -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_
4 changes: 4 additions & 0 deletions client_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "client.h"
#include "settings.h"

namespace MumbleClient {

///////////////////////////////////////////////////////////////////////////////

// static
Expand Down Expand Up @@ -38,3 +40,5 @@ MumbleClient* MumbleClientLib::NewClient() {
void MumbleClientLib::Run() {
io_service_.run();
}

} // end namespace MumbleClient
4 changes: 4 additions & 0 deletions client_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <boost/asio.hpp>

namespace MumbleClient {

class MumbleClient;

class MumbleClientLib {
Expand All @@ -22,4 +24,6 @@ class MumbleClientLib {
void operator=(const MumbleClientLib&);
};

} // end namespace MumbleClient

#endif // CLIENT_LIB_H_
40 changes: 37 additions & 3 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
#include <iostream>
#include <fstream>

#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<const char *>(length), 4);
fs.write(reinterpret_cast<char *>(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();
Expand Down
4 changes: 4 additions & 0 deletions messages.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef MESSAGES_H_
#define MESSAGES_H_

namespace MumbleClient {

namespace PbMessageType {
enum MessageType {
Version,
Expand Down Expand Up @@ -31,4 +33,6 @@ namespace PbMessageType {
};
}

} // end namespace MumbleClient

#endif // MESSAGES_H_
20 changes: 10 additions & 10 deletions settings.h
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit df82a1e

Please sign in to comment.