-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
99 lines (89 loc) · 2.71 KB
/
main.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <netinet/in.h>
#include <thread>
#include <iostream>
#include "ISock.h"
#include "ThreadPool.h"
#include "LOG.h"
#include "LOGDefines.h"
#include "Timer.h"
#include "MsgDispatcher.h"
#include "PidAllocator.h"
#include "RspMessage.h"
#include "Connection.h"
#include "EpollService.h"
#include "DBOperator.h"
#include "UserInfoTable.h"
using namespace NetIO;
using namespace Service;
// Server打开8000监听端口->遇到连接请求,分配新的socket与客户端通信->继续监听
const int BUFSIZE = 512;
const char* SERVER_ADDR = "127.0.0.1";
const int SERVER_PORT = 8080;
const int BACKLOG = 10;
Timer::Timer timer;
ThreadUtils::ThreadPool pool(10);
using namespace MessageAdapter;
void MsgBuilder(ConnectRspMessage &msg, unsigned int pid) {
msg.msgType = CONNECTION;
msg.statusCode = OK;
msg.pid = 0;
msg.allocPid = pid;
}
void Serve(std::shared_ptr<Socket> psock) {
unsigned int pid = Utils::PidAlloc();
ConnectRspMessage msg{};
MsgBuilder(msg, pid);
auto connection = std::make_shared<Connection>(psock, pid);
MessageHandler::MsgDispatcher::Instance().RegisterConnection(pid, connection);
Service::EpollService::Instance().AddConnectionListener(connection);
connection->Send(reinterpret_cast<const char *> (&msg), sizeof(msg));
}
void LogBackEnd() {
GETLOG.WriteFile();
}
void HandleMessage() {
MessageHandler::MsgDispatcher::Instance().HandleMessage();
}
void RecordTest() {
using DBAdapter::UserInfoTableRecord;
using DBAdapter::TableOperator;
using DBAdapter::UserInfoKey;
std::string u_name = "test1";
std::string u_passwd = "12345678";
auto record = std::make_shared<UserInfoTableRecord>(u_name, u_passwd);
// TableOperator<UserInfoTableRecord>::AddRecord(record);
auto query_record = TableOperator<UserInfoTableRecord>::QueryRecord(UserInfoKey{"test1"});
// TableOperator<UserInfoTableRecord>::DeleteRecord(UserInfoKey{"test1"});
}
int main() {
RecordTest();
// sockaddr_in m_addr;
// Socket serverSocket;
//
// SockAddr serverAddr(AF_INET, SERVER_ADDR, SERVER_PORT);
// serverSocket.Bind(serverAddr);
//
// int backlog = 10;
//
// int msgHandlerNum = 3;
//
// for (int i = 0; i < msgHandlerNum; ++i) {
// pool.AddTask(HandleMessage);
// }
// // 启用新的线程用于定时器计数
// std::thread th(&Timer::Timer::Start, &timer, 10000);
//
//
// serverSocket.Listen(3);
// int accept_num = 100;
// pool.init();
// pool.AddTask(LogBackEnd);
// std::cout << "success" << std::endl;
// while(accept_num--) {
// auto psock = serverSocket.Accept();
// pool.AddTask(Serve, psock);
// }
// // todo: 需要等待所有任务执行完
// GETLOG.ShutDown();
// th.join();
}