-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoserial.cc
211 lines (180 loc) · 5.2 KB
/
autoserial.cc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* !----- THIS IS AN AUTOGENERATED CODE DON'T MODIFY -------! */
#include "autoserial.hh"
void err::warning_err(const char * string, int errnumb)
{
if(verbosity)
cout<<string<<" "<<strerror(errnumb)<<endl;
log<<string<<" "<<strerror(errnumb)<<endl;
log.flush();
}
void err::exit_err(const char * string, int errnumb)
{
if(verbosity)
cout<<string<<" "<<strerror(errnumb)<<endl;
log<<string<<" "<<strerror(errnumb)<<endl;
exit(EXIT_FAILURE);
}
err::err(const char* namefile, bool verbosity)
{
system("mkdir -p ./log");
this->namefile = "./log/" + std::string(namefile);
cout<<namefile<<endl;
this->verbosity = verbosity;
log.open(namefile,std::ofstream::app);
}
int server::server_init()
{
int fd_s;
struct sockaddr_in myserver;
opt = SO_REUSEADDR;
bzero(&myserver, sizeof(myserver));
myserver.sin_family = AF_INET; // host byte order
myserver.sin_port = htons(port); // short, network byte order
myserver.sin_addr.s_addr = htonl(INADDR_ANY); // automatically fill with my IP
if ((fd_s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
log_file-> exit_err("socket",errno);
}
log_file-> warning_err("socket",errno);
if (setsockopt(fd_s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
log_file-> exit_err("set socket",errno);
}
log_file-> warning_err("set socket",errno);
if (bind(fd_s, (struct sockaddr *)&myserver, sizeof(struct sockaddr)) == -1) {
log_file-> exit_err("bind",errno);
}
log_file-> warning_err("bind",errno);
if (listen(fd_s, 10) == -1) {
log_file-> exit_err("listen",errno);
}
log_file-> warning_err("listen",errno);
general_descriptor = fd_s;
return fd_s;
}
int server::server_accept(int fd_s)
{
socklen_t sin_size = sizeof(struct sockaddr_in);
if((session_descriptor = accept(fd_s, (struct sockaddr *)&client, &sin_size))==-1);
log_file-> warning_err("session accept",errno);
log_file-> warning_err("session accept",errno);
return session_descriptor;
}
void server::accept_close(int fd_s)
{
if(close(fd_s)==-1){
log_file-> warning_err("session close",errno);
}
log_file-> warning_err("session close",errno);
}
void server::server_close()
{
if(close(general_descriptor)==-1){
log_file-> exit_err("server close",errno);
}
log_file-> warning_err("server close",errno);
}
int autoserial::mysend(int fd, void* buf, int size, err* log_file)
{
size_t len = size;
int size_tot = 0, send_size;
if((send_size = send(fd, &size, sizeof(size), MSG_NOSIGNAL)) <= 0)
{
if(send_size == -1)
log_file-> warning_err("send error",errno);
return 0;
}
while(len > 0)
{
if((send_size = send(fd, buf, len, MSG_NOSIGNAL)) <= 0)
{
if(send_size == -1)
log_file-> warning_err("send error",errno);
return 0;
}
len -= send_size;
size_tot += send_size;
}
return size_tot;
}
int autoserial::receive(int fd, void* buf, err* log_file)
{
int size, size_tot=0, recv_size;
size_t len;
if((recv_size = recv(fd, &size, sizeof(int), 0)) <= 0)
{
if(recv_size == -1)
log_file-> warning_err("receive",errno);
return 0;
}
len = size;
while(len > 0)
{
if((recv_size = recv(fd, buf, len, 0)) <= 0){
if(recv_size == -1)
log_file-> warning_err("receive",errno);
return 0;
}
len -= recv_size;
size_tot += recv_size;
}
return size_tot;
}
client::client(){
std::string str = HOSTNAME ;
hostname = new char[str.size() + 1];
std::copy(str.begin(), str.end(), hostname);
hostname[str.size()] = '\0';
port = PORT;
}
client::client(char* hname, char* p){
hostname = hname;
port = atoi(p);
}
int client::client_init()
{
int fd;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
log_file-> exit_err("socket",errno);
}
log_file-> warning_err("socket",errno);
general_descriptor = fd;
return fd;
}
void client::client_connect(int fd)
{
if ((he = gethostbyname(hostname)) == NULL) {
log_file-> exit_err("error host name",errno);
}
log_file-> warning_err("host name",errno);
bzero(&myserver, sizeof(myserver));
myserver.sin_family = AF_INET;
myserver.sin_port = htons(port);
myserver.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(fd, (struct sockaddr *)&myserver, sizeof(struct sockaddr)) == -1) {
log_file-> exit_err("session connect",errno);
}
log_file-> warning_err("session connect",errno);
}
void client::client_close()
{
if(close(general_descriptor)==-1){
log_file-> exit_err("connect",errno);
}
log_file-> warning_err("connect",errno);
}
//function client
--function--
void server::request_handler()
{
string msg;
char nameF[20];
while(receive(session_descriptor, nameF, log_file) != 0)
{
int c = atoi(nameF);
switch(c){
//function server
--function_switch--
default:
cout<< "Non esiste questa funzione"<<endl;
}
}
}