-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
149 lines (137 loc) · 4.76 KB
/
server.cpp
File metadata and controls
149 lines (137 loc) · 4.76 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
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
#include<iostream>
#include<stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include<pthread.h>
#include"server.h"
#include"networkFunctions.h"
#include"networkStructures.h"
using namespace std;
#define BACKLOG 10
template<class T, int(T::*mem_fn)()>
void* thunk(void* p){
int *ret_val=(int *)calloc(1,sizeof(int));
*ret_val=((static_cast<T*>(p)->*mem_fn)());
return ret_val;
}
void* Server::get_in_addr(struct sockaddr *sock){
if(sock->sa_family==AF_INET){
return &(((struct sockaddr_in *)sock)->sin_addr);
}
else if(sock->sa_family==AF_INET6){
return &(((struct sockaddr_in6 *)sock)->sin6_addr);
}
}
int Server::hosting(){
struct addrinfo hints,*servinfo ,*p;
struct sockaddr_storage client_info;
this->sockets=(struct socketsOpened*)calloc(1,sizeof(struct socketsOpened));
int retval=0;
p=NULL;
int yes=1;
memset(&hints,0,sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;
getaddrinfo(NULL,this->portno,&hints,&servinfo);
sock=-1;
for(p=servinfo;p!=NULL;p=p->ai_next){
if((sock=socket(p->ai_family,p->ai_socktype,p->ai_protocol))==-1)
continue;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) {
cout<<"setsockopt\n";
close(sock);
continue;
retval=SETSOCKOPT_ERROR;
}
if(bind(sock,p->ai_addr,p->ai_addrlen)==-1){
close(sock);
continue;
}
break;
}
if(p==NULL){
cout<<"Failed to Bind.\n";
close(sock);
retval=BIND_ERROR;
return BIND_ERROR;
}
char hostname[50];
gethostname(hostname,50);
cout<<"Listening on port : "<<this->portno<<"\n";
freeaddrinfo(servinfo);
if(listen(sock,BACKLOG)==-1){
cout<<"Listening Error.\n";
close(sock);
retval=LISTEN_ERROR;
return LISTEN_ERROR;
}
socklen_t client_info_size = sizeof client_info;
cout<<"Waiting for Connections....\n";
client_sock=accept(sock,(struct sockaddr *)&client_info,&client_info_size);
if(client_sock==-1||!accept_condition){
retval=ACCEPT_ERROR;
return ACCEPT_ERROR;
}
char clientname[50];
inet_ntop(client_info.ss_family,get_in_addr((struct sockaddr *)&client_info),clientname,sizeof clientname);
cout<<"Connected to : "<<clientname<<"\n";
cout<<"Connected at port : "<<this->portno<<"\n";
struct ::socketsOpened *abc;
Server::sockets->server_socket=sock;
Server::sockets->client_socket=client_sock;
Server::sockets->function=this->function;
Server::sockets->additionalData=this->additionalData;
if(pthread_create(&recvThread,NULL,::recvData,sockets)!=0)
cout<<"Server cannot receive.\n";
if(onConnection!=NULL)
this->onConnection(this->conn_args);
return SERVER_SUCCESS;
}
Server::Server(){
accept_condition=true;
}
int Server::start(char *portno,void*(connected)(void *),void *conn_args,void *(receivedDataProcessor)(void *),void *args){
cout<<"Port is : "<<portno<<"\n";
this->portno=portno;
this->function=receivedDataProcessor;
this->additionalData=args;
this->onConnection=connected;
this->conn_args=conn_args;
pthread_create(&hostThread,NULL,thunk<Server,&Server::hosting>,this);
}
int Server::start_unthreaded(char *portno,void*(connected)(void *),void *conn_args,void *(receivedDataProcessor)(void *),void *args){
this->portno=portno;
this->function=receivedDataProcessor;
this->additionalData=args;
this->onConnection=connected;
this->conn_args=conn_args;
return hosting();
}
pthread_t *Server::getThreadID(){
return &(this->hostThread);
}
int Server::sendData(void *n,int size){
string s=(char *)n;
s+="~";
int numbytes=send(client_sock,(void *)(&s[0]),size+1,0);
if(errno==EPIPE||numbytes==-1){
close(sock);
close(client_sock);
return SEND_ERROR;
}
return SEND_SUCCESS;
}
void Server::shut(void){
cout<<"Server shutting down....\n";
close(sock);
close(client_sock);
}