首页 > 代码库 > 第三讲:asio通讯之二

第三讲:asio通讯之二

上一讲说到了protobuf有关的东西,各位看官记得下载源码,然后把google放进项目中,今天着重关注一下关于消息的hpp文件,直接上代码

#ifndef RECEIVE_MESSAGE_HPP#define RECEIVE_MESSAGE_HPP#include <cstdio>#include <cstdlib>#include <cstring>#include "cocos2d.h"#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)    #include "stdint.h"    #include <WinSock2.h>#endifclass ReceiveMessage{public:  enum { header_length = 4 };  enum { cmdid_length = 4 };  enum { max_body_length = 40960 };  ReceiveMessage()    : body_length_(0)  {  }  const char* data() const  {    return data_;  }  char* data()  {    return data_;  }  std::size_t length() const  {    return header_length + cmdid_length + body_length_;  }  const char* body() const  {    return data_ + header_length + cmdid_length ;  }  char* body()  {    return data_ + header_length + cmdid_length ;  }  std::size_t body_length() const  {    return body_length_;  }  void body_length(std::size_t new_length)  {    body_length_ = new_length;    if (body_length_ > max_body_length)      body_length_ = max_body_length;  }  bool decode_cmdid(int32_t *cmdid)  {    int32_t cmdid_int32_t ;    std::memcpy(&cmdid_int32_t, data_ + header_length,cmdid_length);    *cmdid = htonl(cmdid_int32_t);    return true;  }  bool decode_header(int32_t *cmdid)  {    int32_t header_int32_t ;    std::memcpy(&header_int32_t, data_ ,header_length);    body_length_ = htonl(header_int32_t);    decode_cmdid(cmdid);      if (body_length_ > max_body_length)    {      body_length_ = 0;      return false;    }    return true;  }private:  char data_[header_length + cmdid_length + max_body_length];  std::size_t body_length_;};#endif // RECEIVE_MESSAGE_HPP

关于协议,服务器和客户端定了一个简单的通讯协议,在封包的过程中分为3部分,前4个字节代表消息头部,中间4个自己代表消息类型,后面的为消息体

 

decode_*类型的函数,这连个函数用来转换数据,用来把主机字节序转化为网络字节序

 

发送的消息协议

//// AsioSendMessage.hpp// ~~~~~~~~~~~~~~~~//// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)//// Distributed under the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//#ifndef SEND_MESSAGE_HPP#define SEND_MESSAGE_HPP#include <cstdio>#include <cstdlib>#include <cstring>#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#include "stdint.h"#include <WinSock2.h>#endif#include <asio.hpp>class AsioSendMessage{public:        std::function< void(int res) > func_sucess;    std::function< void(asio::error_code ec) > func_fail;      enum { header_length = 4 };  enum { max_body_length = 1024 };  AsioSendMessage()    : body_length_(0)  {      func_sucess = nullptr;      func_fail = nullptr;  }  const char* data() const  {    return data_;  }  char* data()  {    return data_;  }  std::size_t length() const  {    return header_length + body_length_;  }  const char* body() const  {    return data_ + header_length;  }  char* body()  {    return data_ + header_length;  }  std::size_t body_length() const  {    return body_length_;  }  void body_length(std::size_t new_length)  {    body_length_ = new_length;    if (body_length_ > max_body_length)      body_length_ = max_body_length;  }  bool decode_header()  {    int32_t header_int32_t ;    std::memcpy(&header_int32_t, data_ ,header_length);    body_length_ = htonl(header_int32_t);    if (body_length_ > max_body_length)    {      body_length_ = 0;      return false;    }    return true;  }  void encode_header()  {    int32_t header_int32_t = htonl(body_length_);    std::memcpy(data_, &header_int32_t, header_length);  }private:  char data_[header_length + max_body_length];  std::size_t body_length_;};#endif // SEND_MESSAGE_HPP

 

第三讲:asio通讯之二