db_service.h
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/********************************************************************
platform:
author: kevin
copyright: All rights reserved.
purpose:
brief: 服务
*********************************************************************/
#pragma once
#include <service_if.h>
#include <lib_log.h>
#include "route.h"
#include "dbproxy.h"
#include "wait_db.h"
//服务数据
struct service_t
{
std::string ip;
uint16_t port;
el::lib_tcp_peer_info_t* peer;
service_t(){
this->init();
}
void init(){
this->peer = NULL;
this->ip.clear();
this->port = 0;
}
int send(el::lib_tcp_peer_info_t* back_peer, const void* data, uint32_t len){
if (NULL == this->peer){
//连接
this->peer = el_async::connect(this->ip, this->port);
if (NULL == this->peer){
return el::ERR_DBPROXY::DB_DISCONNECT;
}
}
if (1 == g_dbproxy.head.cmd%2){
//创建新KEY&&保存
uint32_t key = g_wait_db.gen_save_key(g_dbproxy.head, back_peer);
//修改头&&发送
key = EL_BYTE_SWAP(key);
::memcpy((char*)data + 4, &key, sizeof(key));
//TRACE_LOG("=>s[seq:%u]", key);
}
el_async::s2peer(this->peer, data, len);
return 0;
}
};
//服务数据管理器
class service_mgr_t
{
public:
service_mgr_t(){
this->start = 0;
this->end = 0;
}
uint32_t start;
uint32_t end;
int send(el::lib_tcp_peer_info_t* back_peer, USER_ID route_id, const void* data, uint32_t len){
uint32_t s_cnt = this->service_vec.size();
uint32_t idx = route_id%s_cnt;
return this->service_vec[idx].send(back_peer, data, len);
}
std::vector<service_t> service_vec;
};