首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >“跟踪”与Thrift的连接

“跟踪”与Thrift的连接
EN

Stack Overflow用户
提问于 2010-04-14 19:14:30
回答 2查看 711关注 0票数 1

我正在尝试使用Thrift创建一个游戏,这样客户端就是玩家,服务器管理棋盘,就像this一样。然而,我不明白Facebook的Thrift服务器如何“跟踪”用户,也就是说,当我在their service上调用attack()时,我不需要再次表明自己的身份。

根据生成的服务器存根的建议,没有办法做到这一点:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<ConnectFourHandler> handler(new ConnectFourHandler());
  shared_ptr<TProcessor> processor(new ConnectFourProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}

在该示例中,只为服务器创建了一个处理程序,服务器接受连接。

如果所有请求都只通过每个服务器的一个处理程序进行路由,那么Facebook如何能够跟踪连接到服务器的客户端?

EN

回答 2

Stack Overflow用户

发布于 2011-04-13 11:59:05

试试server.setServerEventHandler。它将在新客户端连接时调用您的代码,让您有机会创建特定于连接的上下文对象。

票数 0
EN

Stack Overflow用户

发布于 2018-07-31 19:06:38

因为thrift为每个连接使用唯一的线程,所以可以使用线程id将两者链接在一起。据我所知,节俭本身是无法做到这一点的。如果thrift将上下文字段向下传递到每个处理程序函数,这将是可能的。

下面是一个使用线程id的示例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <map>
#include <thread>

typedef struct {
    // you would put your connection specific variables here
} ConnectionContext;

// You could reverse these types if you had more than one
// context per thrift connection, eg. your service involved opening
// or connecting to more than one thing per thrift connection
std::map<std::thread::id, ConnectionContext> threadContextMap;

class OurEventHandler : public server::TServerEventHandler {
public:
OurEventHandler() :
    NumClients_(0)
{}

//Called before the server begins -
//virtual void preServe() {}

//createContext may return a user-defined context to aid in cleaning
//up client connections upon disconnection. This example dispenses
//with contextual information and returns NULL.
virtual void* createContext(shared_ptr<protocol::TProtocol> input, 
shared_ptr<protocol::TProtocol> output)
{
    printf("Client connected (total %d)\n", ++NumClients_);

    auto this_id = std::this_thread::get_id();
    std::cout << "connected thread " << this_id << std::endl;

    return NULL;
}

//Called when an client has disconnected, either naturally or by error.
virtual void deleteContext(void* serverContext, 
shared_ptr<protocol::TProtocol>input, shared_ptr<protocol::TProtocol>output)
{
    printf("Client disconnected (total %d)\n", --NumClients_);

    auto this_id = std::this_thread::get_id();
    std::cout << "disconnected thread " << this_id << std::endl;

    auto context = threadContextMap[this_id];

    // TODO: Perform your context specific cleanup code here
}

protected:
    uint32_t NumClients_;
};


class yourRpcHandler : virtual public service_rpcIf
{
public:
    yourRpcHandler() :
    {
        // Your initialization goes here
    }

    void SomeMethod()
    {
        auto context = threadContextMap[std::this_thread::get_id()];

        // TODO: use the context as you see fit
    }
};

int main(int argc, char **argv)
{
    int port = 9090;
    printf("Listening on port %d\n", port);

    auto rpcHandler = new yourRpcHandler();
    shared_ptr<yourRpcHandler> handler(rpcHandler);
    shared_ptr<TProcessor> processor(new yourRpcProcessor(handler));
    shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    TThreadedServer server(processor, serverTransport, transportFactory, protocolFactory);
    shared_ptr<OurEventHandler> EventHandler(new OurEventHandler());

    server.setServerEventHandler(EventHandler);
    server.serve();
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2640317

复制
相关文章
python列表、元组、字典
列表是由一序列特定顺序排列的元素组成的。可以把字符串,数字,字典等都可以任何东西加入到列表中,列表中的元素之间没有任何关系。列表也是自带下标的,默认也还是从0开始。列表常用方括号表示,即:[],元素用逗号隔开。
py3study
2020/01/15
1.2K0
Python列表、元组、字典操作
#!/usr/bin/python # -*- coding: UTF-8 -*- list1 = [1,2,'a', 'b'] seq = (1,4,5) print list1[2] #获取第三个元素(下标是从0开始) print list1[-3] #获取倒数第三个元素 print list1[2:] #获取第三个元素到最后一个 print len(list1) #获取列表元素个数 print max(list1) #获取最大的元素 print min(list1) #
苦咖啡
2018/04/28
1.7K0
python之列表、元组、字典
3 深拷贝和浅拷贝 1 浅拷贝: 当列表中存在有个列表时,其修改这个列表中列表的某一个元素时,其他被拷贝的列表中的对应元素也将被拷贝,其在拷贝这个列表中的列表时,拷贝的是这个内嵌列表的内存位置。
py3study
2020/01/14
2.8K0
Python 列表&元组&字典&集合
列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片、合并、删除等操作 可通过索引来向指定位置插入元素 可通过pop()方法删除末尾元素,pop(索引位置)来删除指定位置元素 替换元素可直接通过赋值给对应的的索引位置 1 classMates = ['zhangsan','lisi','wangwu'] 2 print(classMates[0]) #通过索引来获取元素 3 print(classMates[-1]
py3study
2020/01/19
1.3K0
python_列表_元组_字典
insert(index, object) 在指定位置index前插入元素object
以某
2023/03/07
2.3K0
python_列表_元组_字典
python-元组,字典,列表
由于会处理一些json数据,内部字典,列表,元租傻傻分不清,所以这里总结一下他们的特点,便于提取数据 想要知道跟多看官方文档,很详细 https://www.runoob.com/python/python-lists.html 我是看了官方文档后总结后我自己的
全栈程序员站长
2021/05/19
1.1K0
python列表、元组、字典、集合的简单
一、列表、元组 1、常用操作函数 1 #Author:CGQ 2 import copy 3 #列表 4 ''' 5 names=["ZhangYang","XiaoHei","XiaoHei","LiSan"] 6 print(names[0])#某一个 7 #切片 8 print(names[1:3]) 9 print(names[0:-1:2])#2代表步长 10 print(names[-1]) 11 print(names[-2:]) 12 len(names)#字符串
py3study
2020/01/19
1.6K0
python列表、元组、字典、集合的简单
python中星号的意义(**字典,*列表或元组)
传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊的语法:*、** 。 调用函数时使用* ,** test(*args)中 * 的作用:其实就是把序列 args 中的每个元素,当作位置参数传进去。比如上面这个代码,如果 args 等于 (1,2,3) ,那么这个代码就等价于 test(1, 2, 3) 。 test(**kwargs)中** 的作用:则是把字典 kwargs 变成关键字参数传递。比如上面这个代码,如果 kwargs 等于 {
用户1214487
2018/01/24
3.7K0
python中列表排序,字典排序,列表中的字典排序
key= lambda dict1:dict1[0] #dict1[0]表示按键,dict1[1]表示按值。
用户8346838
2021/03/10
9.1K0
python_列表——元组——字典——集合
列表——元组——字典——集合: 列表: # 一:基本使用 # 1、用途:存放多个值 # 定义方式:[]内以逗号为分隔多个元素,列表内元素无类型限制 # l=['a','b','c'] #l=list(['a','b','c']) # l1=list('hello') # print(l1) # 常用操作+内置的方法 # 按索引存取值(正向存取+反向存取):即可改也可以取 l = ['a', 'b', 'c'] print(id(l)) print(l[-1]) l[0] = 'A' print(id
py3study
2020/01/22
1.2K0
9. python 列表、元组、字典
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n')
py3study
2020/01/15
5940
list(列表)、tuple(元组)、dict(字典)的回顾
list使用的是方括号,类似与数组结构, tuple元组使用的是圆括号,跟list类似但是数据不能进行修改, 所以应用的场景基本上用在需要数据存储,或者是需要保证数据安全无法修改的情况, 字典用的是大括号,是key-value结构的, 操作数据的时候,是操作key,而不是索引。
benym
2022/07/14
6150
python列表、字典、元组、集合学习笔记
列 表 列表在python里是有序集合对象类型。 列表里的对象可以是任何对象:数字,字符串,列表或者字典,元组。与字符串不同,列表是可变对象,支持原处修改的操作 python的列表是:
没有故事的陈师傅
2019/07/28
2.3K0
python3 列表 元组 字典 文件
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
py3study
2020/01/06
9030
Python 列表、元组、字典及集合操作
注意:当索引超出范围时,Python会报一个IndexError错误,所以,要确保索引不要越界,记得最后一个元素的索引是len(list1) - 1。
py3study
2020/01/19
1.5K0
python3(元组,列表,集合,字典)
1.列表 1)创建列表 数组:存储同一种数据类型的集合 scores=[12,13,14] 列表:(打了激素的数组):可以存储任意数据类型的集合
py3study
2020/01/12
8130
python3 列表、元组、字典、集合
#使用方括号定义列表 my_list = [ ’ crazyit ’, 20,’ Python ’] print (my list) #使用圆括号定义元组 my_ tuple = (’ crazyit’, 20 ,’ Python’) print(my_tuple)
用户5760343
2019/12/12
6560
Python 列表、字典、元组的一些小技巧
我们知道 Python 的内置 dictionary 数据类型是无序的,通过 key 来获取对应的 value。可是有时我们需要对 dictionary 中的 item 进行排序输出,可能根据 key,也可能根据 value 来排。到底有多少种方法可以实现对 dictionary 的内容进行排序输出呢?下面摘取了使用 sorted 函数实现对 dictionary 的内容进行排序输出一些精彩的解决办法。
章鱼猫先生
2021/10/15
1.2K0
Python 列表、字典、元组的一些小技巧
Python - 从字典列表中删除字典
字典是python的一个非常常用的功能,用于根据用户需要在其中存储数据。另一个典型的过程涉及编辑或操作此数据。要成为一名高效且快速的程序员,您必须弄清楚如何从字典列表中删除字典。有许多技术可以从词典列表中删除字典,本文将介绍这些技术。
很酷的站长
2023/08/11
2760
Python - 从字典列表中删除字典
python---基础之模块,列表,元组,字典
1、 模块 写模块的时候尽量不要和系统自带的模块的名字相同 调用模块的时候,会先在当前目录下查找是否有这个模块,然后再会如python的环境变量中查找 a.模块1:sys 代码如下: 1 import
coders
2018/01/04
1.2K0
python---基础之模块,列表,元组,字典

相似问题

包含以列表作为值的元组的字典

20

以值作为元组列表的字典

26

创建包含值列表的单独字典

12

迭代字典值,这些值是列表,以创建新的字典列表。

41

如何创建包含元组和列表的值字典

24
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文