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
|
#ifndef CONNECTION_H
#define CONNECTION_H
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include <iostream>
class RawTextMessage
{
public:
RawTextMessage(const std::string& _message) : message(_message), pos(0)
{};
std::string GetWord()
{
size_t oldpos = pos;
pos = message.find_first_of(std::string("\t \n"), oldpos);
if (pos != std::string::npos)
{
return message.substr(oldpos, pos++ - oldpos);
}
else if (oldpos != std::string::npos)
{
return message.substr(oldpos);
}
else
{
return "";
}
};
int GetInt()
{
std::istringstream buf(GetWord());
int temp;
buf >> temp;
return temp;
};
private:
std::string message;
size_t pos;
};
class UserCache;
class Connection
{
public:
Connection();
~Connection();
void Connect(const std::string& server, int port);
virtual void DoneConnecting(bool succes, const std::string& err) {};
virtual void ServerGreeting(const std::string& serverVer, const std::string& springVer, int udpport, int mode) {};
void Register(const std::string& name, const std::string& password);
virtual void RegisterDenied(const std::string& reason) {};
virtual void RegisterAccept() {};
void Login(const std::string& name, const std::string& password);
virtual void Denied(const std::string& reason) {};
virtual void LoginEnd() {};
virtual void Aggreement(const std::string text) {};
void ConfirmAggreement();
virtual void Motd(const std::string text) {};
void JoinChannel(const std::string& channame, const std::string& password = "");
virtual void Joined(const std::string& channame) {};
virtual void JoinFailed(const std::string& channame, const std::string& reason) {};
virtual void Disconnected() {};
virtual void NetworkError(const std::string& msg) {};
void SendData(const std::string& msg);
void Poll();
void Run();
private:
void DataReceived(const std::string& command, const std::string& msg);
void ConnectCallback(const boost::system::error_code& error);
void ReceiveCallback(const boost::system::error_code& error, size_t bytes);
void SendCallback(const boost::system::error_code& error);
std::string aggreementbuf;
boost::asio::ip::tcp::socket sock;
boost::asio::streambuf incomeBuffer;
UserCache* users;
boost::posix_time::ptime lastPing;
};
#endif
|