thrift、多线程、锁、匹配池、游戏匹配机制实现(实现游戏节点、实现匹配节点)

thrift 官网文档
https://thrift.apache.org/ 

匹配机制

thrift 就是有向边,模块A调用模块B函数时利用thrift实现,即服务器之间的通信利用thrift实现
thrift 也称为 rpc 框架(远程函数调用)(调用其他服务器上的函数)
1. 定义接口
2. server
3. client
mkdir thrift_lesson
cd thrift_lesson
vim readme.md
git init  创建仓库
gti add .
git commit -m "init repo"
在远程仓库创建项目
git remote add origin git@git.acwing.com:YANGGER/thrift_lesson.git   将本地仓库链接到云端
git push -u origin master  将本地分支复制到云端
tmux  进入tmux 防止代码丢失
cd thrift_lesson
mkdir match_system      用两个文件夹表示两个节点 : 匹配系统节点
mkdir game     游戏节点
mkdir thrift    创建thrift 文件夹存放接口
cd thrift
vim match.thrift
添加和删除玩家的接口
定义命名空间
namespace cpp match_service   
定义结构体存储用户信息(id,姓名,分值(依照分值进行匹配))
struct User
{
	1: i32 id , 
	2: string name,
	3: i32 score
}
定义函数
service Match
{
	i32 add_user(1:User user 2: string info) ,       添加用户函数(用户(id,name,score), 额外信息)

	i32 remove_user(1: User user 2: string info) ,      删除用户函数(用户(id,name,score), 额外信息)
}
git add .
git commit -m "add match thrift"    
git push
用接口实现匹配系统(服务端)节点(c++代码)
cd  match_systm
mkdir src    存储源文件
cd src
thrift -r --gen cpp  ../../thrift/match.thrift    根据match.thrift内定义的接口生成c++版本的服务器代码 gen-cpp/Match_server.skeleton.cpp
cd gen-cpp
vim Match_server.skeleton.cpp
thrift 好用:定义完接口match.thrift后,c++代码不用自己去实现,thrift会帮我们实现好
cd ..
mv gen-cpp match_server    改名字
mv  match_server/Match_server.skeleton.cpp main.cpp   把实现好的Match_server.skeleton.cpp 移动到文件src中改名为main.cpp
vim main.cpp            
修改main.cpp添加业务逻辑(server端)
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include "match_server/Match.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

#include<iostream>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using namespace  ::match_service;
using namespace std;
接口函数
class MatchHandler : virtual public MatchIf {
 public:
  MatchHandler() {
    // Your initialization goes here
  }
  int32_t add_user(const User& user, const std::string& info) {
    // Your implementation goes here
    printf("add_user\n");

	return 0;
  }

  int32_t remove_user(const User& user, const std::string& info) {
    // Your implementation goes here          
    printf("remove_user\n");

	return 0;
  }

};
int main(int argc, char **argv) {
  int port = 9090;
  ::std::shared_ptr<MatchHandler> handler(new MatchHandler());
  ::std::shared_ptr<TProcessor> processor(new MatchProcessor(handler));
  ::std::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  ::std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  ::std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);

  cout<<"Start Match Server"<<endl;

  server.serve();
  return 0;
}
编译c++代码: 编译 + 链接
g++ -c main.cpp match_server/*.cpp  编译
g++ *.o -o main -lthrift   链接(-lthrift  链接thrift动态链接库)
./main   运行程序
每次改变 main.cpp 都需要重新编译和链接
git add .                             将所有工作区文件加到暂存区
git restore --staged *.o              将暂存区内 .o 文件移到工作区
git commit -m "add match server"      提交到版本库
git push     推送到远程仓库
用接口实现客户端节点(python代码)
cd game
mkdir src
cd src
thrift -r --gen py ../../thrift/match.thrift         在thrift官网生成python代码
mv gen-py match_client
cd match_client
cd match
Match-remote 可执行文件是利用python代码实现服务端而不是客户端,因此可以删掉
rm Match-remote
cd ../..  回到src文件夹下
客户端client在thrift官网有例子,直接复制回来修改
vim client.py
client代码(.py)
~~import sys~~ 
~~import glob~~ 
~~sys.path.append('gen-py')~~ 
~~sys.path.insert(0,
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值