-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (81 loc) · 2.69 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// main.cpp
// agent
//
// Created by 袁睿 on 16/6/28.
// Copyright © 2016年 number201724. All rights reserved.
//
#include <iostream>
#include "cpp.hpp"
#include "branch.hpp"
#include "forward.hpp"
#include "remotes.hpp"
static uint32_t encrypt_key[4] = {0x42970C86,0xA0B3A057,0x51B97B3C,0x70F8891E};
struct pt_server *server;
struct pt_client *client;
void pt_srv_send(struct pt_sclient *user)
{
char helloworld[32] = "helloworld";
struct pt_buffer *buff;
struct net_header hdr = pt_create_nethdr(ID_TRANSMIT_KEEPALIVE);
buff = pt_create_package(hdr, (unsigned char*)&helloworld, sizeof(helloworld));
pt_server_send(user, buff);
}
qboolean pt_srv_connect(struct pt_sclient *user)
{
printf("server connected\n");
return true;
}
void pt_srv_receive(struct pt_sclient *user, struct pt_buffer *buff)
{
buffer_reader reader;
struct net_header hdr;
buffer_reader_init(&reader, buff);
buffer_reader_read(&reader, (unsigned char*)&hdr, sizeof(hdr));
buffer_reader_ignore_bytes(&reader, 4);
char *s = (char*)buffer_reader_cur_pos(&reader);
printf("%s\n",s);
//printf("pt_srv_receive => %d\n",user->serial);
pt_srv_send(user);
}
void pt_srv_disconnect(struct pt_sclient *user)
{
printf("server disconnected\n");
}
void pt_cli_sent(struct pt_client *client)
{
char helloworld[32] = "helloworld";
struct pt_buffer *buff;
struct net_header hdr = pt_create_nethdr(ID_TRANSMIT_KEEPALIVE);
buff = pt_create_encrypt_package(&client->encrypt_ctx, &client->serial, hdr, (unsigned char*)&helloworld, sizeof(helloworld));
pt_client_send(client, buff);
}
void pt_cli_connect(struct pt_client *client)
{
printf("client on connected\n");
pt_cli_sent(client);
}
void pt_cli_receive(struct pt_client *client, struct pt_buffer *buff)
{
// char *s = (char*)pt_get_packet_buffer(buff);
// printf("%s\n",s);
pt_cli_sent(client);
}
void pt_cli_disconnect(struct pt_client *client)
{
printf("client disconnected\n");
}
int main(int argc, const char * argv[]) {
uv_loop_t *loop = uv_default_loop();
server = pt_server_new();
client = pt_client_new();
pt_server_init(server, loop, 10000, 30, pt_srv_connect, pt_srv_receive, pt_srv_disconnect);
pt_server_set_encrypt(server, encrypt_key);
pt_server_start_pipe(server, "/var/tmp/agent.sock");
pt_client_init(loop, client, pt_cli_connect, pt_cli_receive, pt_cli_disconnect);
pt_client_set_encrypt(client,encrypt_key);
pt_client_connect_pipe(client, "/var/tmp/agent.sock");
uv_run(loop, UV_RUN_DEFAULT);
printf("hello world\n");
return 0;
}