以下代码摘自muduo库, 实现的很漂亮, 非常值得学习 , 具体说明可以参考(书<<Linux多线程服务端编程–使用muduo C++网络库>>的P234-P235)
- dispatcher.h
// Copyright 2011, Shuo Chen. All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#ifndef MUDUO_EXAMPLES_PROTOBUF_CODEC_DISPATCHER_H
#define MUDUO_EXAMPLES_PROTOBUF_CODEC_DISPATCHER_H
#include "muduo/base/noncopyable.h"
#include "muduo/net/Callbacks.h"
#include <google/protobuf/message.h>
#include <map>
#include <type_traits>
typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
class Callback : muduo::noncopyable
{
public:
virtual ~Callback() = default;
virtual void onMessage(const muduo::net::TcpConnectionPtr&,
const MessagePtr& message,
muduo::Timestamp) const = 0;
};
template <typename T>
class CallbackT : public Callback
{
static_assert(std::is_base_of<google::protobuf::Message, T>::value,
"T must be derived from gpb::Message.");
public:
typedef std::function<void (const muduo::net::TcpConnectionPtr&,
const std::shared_ptr<T>& message,
muduo::Timestamp)> ProtobufMessageTCallback;
CallbackT(const ProtobufMessageTCallback& callback)
: callback_(callback)
{
}
void onMessage(const muduo::net::TcpConnectionPtr& conn,
const MessagePtr& message,
muduo::Timestamp receiveTime) const override
{
std::shared_ptr<T> concrete = muduo::down_pointer_cast<T>(message);
assert(concrete != NULL);
callback_(conn, concrete, receiveTime);
}
private:
ProtobufMessageTCallback callback_;
};
class ProtobufDispatcher
{
public:
typedef std::function<void (const muduo::net::TcpConnectionPtr&,
const MessagePtr& message,
muduo::Timestamp)> ProtobufMessageCallback;
explicit ProtobufDispatcher(const ProtobufMessageCallback& defaultCb)
: defaultCallback_(defaultCb)
{
}
void onProtobufMessage(const muduo::net::TcpConnectionPtr& conn,
const MessagePtr& message,
muduo::Timestamp receiveTime) const
{
CallbackMap::const_iterator it = callbacks_.find(message->GetDescriptor());
if (it != callbacks_.end())
{
it->second->onMessage(conn, message, receiveTime);
}
else
{
defaultCallback_(conn, message, receiveTime);
}
}
template<typename T>
void registerMessageCallback(const typename CallbackT<T>::ProtobufMessageTCallback& callback)
{
// 直接保存了具体类型CallbackT
std::shared_ptr<CallbackT<T> > pd(new CallbackT<T>(callback));
callbacks_[T::descriptor()] = pd;
}
private:
typedef std::map<const google::protobuf::Descriptor*, std::shared_ptr<Callback> > CallbackMap;
CallbackMap callbacks_;
ProtobufMessageCallback defaultCallback_;
};
#endif // MUDUO_EXAMPLES_PROTOBUF_CODEC_DISPATCHER_H
- main.cc
#include "examples/protobuf/codec/dispatcher.h"
#include "examples/protobuf/codec/query.pb.h"
#include <iostream>
using std::cout;
using std::endl;
typedef std::shared_ptr<muduo::Query> QueryPtr;
typedef std::shared_ptr<muduo::Answer> AnswerPtr;
void test_down_pointer_cast()
{
::std::shared_ptr<google::protobuf::Message> msg(new muduo::Query);
::std::shared_ptr<muduo::Query> query(muduo::down_pointer_cast<muduo::Query>(msg));
assert(msg && query);
if (!query)
{
abort();
}
}
void onQuery(const muduo::net::TcpConnectionPtr&,
const QueryPtr& message,
muduo::Timestamp)
{
cout << "onQuery: " << message->GetTypeName() << endl;
}
void onAnswer(const muduo::net::TcpConnectionPtr&,
const AnswerPtr& message,
muduo::Timestamp)
{
cout << "onAnswer: " << message->GetTypeName() << endl;
}
void onUnknownMessageType(const muduo::net::TcpConnectionPtr&,
const MessagePtr& message,
muduo::Timestamp)
{
cout << "onUnknownMessageType: " << message->GetTypeName() << endl;
}
int main()
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
test_down_pointer_cast();
ProtobufDispatcher dispatcher(onUnknownMessageType);
dispatcher.registerMessageCallback<muduo::Query>(onQuery);
dispatcher.registerMessageCallback<muduo::Answer>(onAnswer);
muduo::net::TcpConnectionPtr conn;
muduo::Timestamp t;
std::shared_ptr<muduo::Query> query(new muduo::Query);
std::shared_ptr<muduo::Answer> answer(new muduo::Answer);
std::shared_ptr<muduo::Empty> empty(new muduo::Empty);
dispatcher.onProtobufMessage(conn, query, t);
dispatcher.onProtobufMessage(conn, answer, t);
dispatcher.onProtobufMessage(conn, empty, t);
google::protobuf::ShutdownProtobufLibrary();
}