-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClass.cpp
More file actions
107 lines (83 loc) · 2.53 KB
/
SocketClass.cpp
File metadata and controls
107 lines (83 loc) · 2.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
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
#include "SocketClass.h"
SocketClass::SocketClass()
{
/*
Constructor for SocketClass, creating the connection, Constructor for first time log in.
*/
std::cout << "[LOG] Hello World From SocketClass Constructor " << std::endl;
this->address = "";
this->port = "";
this->read_server_info();
// Init boost connection
boost::asio::io_context io_context;
tcp::socket client_sock_conn(io_context);
tcp::resolver resolver(io_context);
this->socket = &client_sock_conn;
// Connect to server.
boost::asio::connect(*this->socket, resolver.resolve(this->address, this->port));
}
bool SocketClass::send(const char* data_to_send, const int len)
{
try {
boost::asio::write(*this->socket, boost::asio::buffer(data_to_send, len));
return true;
}
catch (std::exception& e)
{
std::cerr << "[LOG] Error at sending section -> soket error: " << e.what() << std::endl;
return false;
}
}
std::string SocketClass::read(const int len)
{
//char recive_buffer[len];
std::string data;
size_t length_of_read;
try {
length_of_read = boost::asio::read(*this->socket, boost::asio::buffer(data, len));
return data;
}
catch (std::exception& e)
{
std::cerr << "[LOG] Error at sending section -> soket error: " << e.what() << std::endl;
return data;
}
}
void SocketClass::printing_Stuff(int i)
{
std::cout << i << std::endl;
}
void SocketClass::read_server_info()
{
FILE *server_info_file;
server_info_file = fopen("server.info", "r");
if (!server_info_file) {
std::cout << "[LOG]Error: server.info opening failed " << std::endl;
}
else{
char tmp;
while ((tmp = fgetc(server_info_file)) != ':' and tmp != EOF)
{
this->address += tmp; // add char to ip until reaching ':'
}
while ((tmp = fgetc(server_info_file)) != EOF)
{
this->port += tmp; // add char to port until reaching EOF
}
}
}
/*std::ifstream inf{ "me.info" };
if (!inf)
{
std::cout << "[LOG] User dont exsits [couldn't find me.info therefore it's empty] " << std::endl;
this->old_user = false;
return false;
}
// add tests to make sure all data is intact
std::getline(inf, client_name_tmp);
strcpy_s(this->client_name, NAME_BUFFER_LEANGTH, client_name_tmp.c_str());
std::getline(inf, str_uuid);
strcpy_s(this->Client_ID, UUID_LEANGTH, str_uuid.c_str());
std::getline(inf, private_key_str);
strcpy_s(this->private_key, KEY_BUFFER_LEANGTH, private_key_str.c_str());
strcpy_s(this->req_header.Client_ID, UUID_LEANGTH, this->Client_ID);*/