Skip to content

Commit

Permalink
add error callback
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
maddinat0r committed Dec 13, 2014
1 parent f7506d8 commit ab37432
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
9 changes: 9 additions & 0 deletions TSConnector.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ enum /*teamspeak channel codecs*/
CODEC_OPUS_MUSIC //5: opus music
};

enum TSC_ERROR_TYPE
{
INVALID,
CONNECTION_ERROR,
TEAMSPEAK_ERROR,
CALLBACK_ERROR
};


//server functions
native TSC_Connect(user[], pass[], hostname[], port = 9987, serverquery_port = 10011);
Expand Down Expand Up @@ -141,6 +149,7 @@ native TSC_SendClientMessage(clientid, msg[]);

//server callbacks
forward TSC_OnConnect();
forward TSC_OnError(TSC_ERROR_TYPE:error_type, error_id, const error_msg[]);

//channel callbacks
forward TSC_OnChannelCreated(channelid);
Expand Down
7 changes: 5 additions & 2 deletions src/CCallback.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "CCallback.h"

#include "format.h"


void CCallbackHandler::Process()
{
Expand Down Expand Up @@ -80,8 +82,9 @@ CCallback *CCallbackHandler::Create(string name, string format,
param_list.push(amx_GetCppString(amx, params[param_offset + param_idx]));
break;
default:
logprintf(">> plugin.TSConnector: Error while creating callback structure: \
(#1) Unrecognized format specifier '%c'", c);
CCallbackHandler::Get()->ForwardError(
EErrorType::CALLBACK_ERROR, 1,
fmt::format("unrecognized format specifier '{}' in callback \"{}({})\"", c, name, format));
return nullptr;
}
param_idx++;
Expand Down
16 changes: 15 additions & 1 deletion src/CCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ using boost::unordered_set;
#include "CSingleton.h"


enum class EErrorType
{
INVALID,
CONNECTION_ERROR,
TEAMSPEAK_ERROR,
CALLBACK_ERROR
};

class CCallback
{
friend class CCallbackHandler;
Expand Down Expand Up @@ -98,11 +106,17 @@ class CCallbackHandler : public CSingleton<CCallbackHandler>
m_Queue.push(callback);
}
template <typename... Args>
inline void Call(const char *name, Args&&... args)
inline void Call(const string &name, Args&&... args)
{
Call(new CCallback(name, std::forward<Args>(args)...));
}

inline void ForwardError(EErrorType error_type,
const unsigned int error_id, const string &error_msg)
{
Call("TSC_OnError",
static_cast<unsigned int>(error_type), error_id, error_msg);
}

inline void AddAmx(AMX *amx)
{
Expand Down
29 changes: 19 additions & 10 deletions src/CNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "CNetwork.h"
#include "CServer.h"
#include "CUtils.h"
#include "CCallback.h"

#include <istream>
#include <boost/date_time/posix_time/posix_time.hpp>
Expand Down Expand Up @@ -36,8 +37,9 @@ bool CNetwork::Connect(string hostname, unsigned short port, unsigned short quer

if (error)
{
logprintf(">> plugin.TSConnector: Error while resolving hostname \"%s\": (#%d) %s",
hostname.c_str(), error.value(), error.message().c_str());
CCallbackHandler::Get()->ForwardError(
EErrorType::CONNECTION_ERROR, error.value(),
fmt::format("error while resolving hostname \"{}\": {}", hostname, error.message()));
return false;
}

Expand Down Expand Up @@ -107,8 +109,9 @@ void CNetwork::OnConnect(const boost::system::error_code &error_code)
}
else
{
logprintf(">> plugin.TSConnector: Error while connecting to server: (#%d) %s",
error_code.value(), error_code.message().c_str());
CCallbackHandler::Get()->ForwardError(
EErrorType::CONNECTION_ERROR, error_code.value(),
fmt::format("error while connecting to server: {}", error_code.message()));
Disconnect();
}
}
Expand Down Expand Up @@ -189,10 +192,14 @@ void CNetwork::OnRead(const boost::system::error_code &error_code)
else
{
string error_str(error_rx_result[2].str());
unsigned int error_id = 0;

CUtils::Get()->UnEscapeString(error_str);
CUtils::Get()->ConvertStringToInt(error_rx_result[1].str(), error_id);

logprintf(">> plugin.TSConnector: Error while executing \"%s\": (#%s) %s",
m_CmdQueue.front().get<0>().c_str(), error_str.c_str(), error_rx_result[1].str().c_str());
CCallbackHandler::Get()->ForwardError(
EErrorType::TEAMSPEAK_ERROR, error_id,
fmt::format("error while executing \"{}\": {}", m_CmdQueue.front().get<0>(), error_str));
m_CmdQueue.pop();
}

Expand Down Expand Up @@ -250,8 +257,9 @@ void CNetwork::OnRead(const boost::system::error_code &error_code)
}
else
{
logprintf(">> plugin.TSConnector: Error while reading: (#%d) %s",
error_code.message().c_str(), error_code.value());
CCallbackHandler::Get()->ForwardError(
EErrorType::CONNECTION_ERROR, error_code.value(),
fmt::format("error while reading: {}", error_code.message()));
}
}

Expand All @@ -263,8 +271,9 @@ void CNetwork::OnWrite(const boost::system::error_code &error_code)
m_CmdWriteBuffer.clear();
if (error_code.value() != 0)
{
logprintf(">> plugin.TSConnector: Error while writing: (#%d) %s",
error_code.message().c_str(), error_code.value());
CCallbackHandler::Get()->ForwardError(
EErrorType::CONNECTION_ERROR, error_code.value(),
fmt::format("error while writing: {}", error_code.message()));
}
}

Expand Down

0 comments on commit ab37432

Please sign in to comment.