21 #include <sys/types.h> 22 #include <sys/socket.h> 24 #include <netinet/in.h> 39 sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
43 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
44 struct sockaddr_in serv_addr;
45 memset(&serv_addr,0,
sizeof(serv_addr));
46 serv_addr.sin_family = AF_INET;
48 serv_addr.sin_port = htons(port);
49 if (bind(sockfd, (
struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
51 printf(
"Bind failed with error %i\n",errno);
66 ((
char*)buffer)[0] = jlcb->
buffer[jlcb->
pos];
76 struct sockaddr_in cli_addr;
77 socklen_t clilen =
sizeof(cli_addr);
79 struct pollfd fd_array[MAX_SOCKETS];
80 memset(fd_array,0,
sizeof(fd_array));
81 std::vector< InsituConnectorContainer* > con_array = std::vector< InsituConnectorContainer* >(
MAX_SOCKETS,NULL);
83 fd_array[0].fd = sockfd;
84 fd_array[0].events = POLLIN;
89 int rv = poll(fd_array, fdnum, 1000);
92 fprintf(stderr,
"Error while calling poll\n");
98 if (fd_array[0].revents == POLLIN)
101 while (newsockfd >= 0)
103 newsockfd = accept(sockfd, (
struct sockaddr *) &cli_addr, &clilen);
107 insituConnector->jlcb.
count = 0;
110 con_array[fdnum] = d;
111 fd_array[fdnum].fd = newsockfd;
112 fd_array[fdnum].events = POLLIN;
118 for (
int i = 1; i < fdnum; i++)
120 if (fd_array[i].revents & POLLIN)
124 int add = recv(fd_array[i].fd,&(con_array[i]->connector->jlcb.buffer[con_array[i]->connector->jlcb.count]),4096,MSG_DONTWAIT);
126 con_array[i]->connector->jlcb.count += add;
130 con_array[i]->connector->jlcb.pos = 0;
131 con_array[i]->connector->jlcb.buffer[con_array[i]->connector->jlcb.count] = 0;
133 if (con_array[i]->connector->jlcb.count > 0)
135 con_array[i]->connector->jlcb.buffer[con_array[i]->connector->jlcb.count] = 0;
137 int last_working_pos = 0;
138 while (json_t * content = json_load_callback(
json_load_callback_function,&con_array[i]->connector->jlcb,JSON_DISABLE_EOF_CHECK,&error))
140 last_working_pos = con_array[i]->connector->jlcb.pos;
145 json_object_set_new( message->
json_root,
"id", json_integer( con_array[i]->connector->getID() ) );
146 json_t* protocol_version = json_object_get( message->
json_root,
"protocol" );
149 json_integer_value( json_array_get( protocol_version, 0) ),
150 json_integer_value( json_array_get( protocol_version, 1) )
155 const char buffer[] =
"{ \"fatal error\": \"protocol mismatch\" }";
156 send(fd_array[i].fd,buffer,strlen(buffer),MSG_NOSIGNAL);
165 long long uid = json_integer_value( json_object_get(content,
"uid") );
166 con_array[i]->connector->clientSendMessage(message);
176 sprintf(buffer,
"{\"done\": %lld}", uid );
177 int l = strlen(buffer);
178 if ( send(fd_array[i].fd,buffer,l,MSG_NOSIGNAL) < l )
181 json_object_set_new( message->
json_root,
"type", json_string(
"exit" ) );
182 con_array[i]->connector->clientSendMessage(message);
190 if ( error.position != 1 || strcmp(error.text,
"'[' or '{' expected near end of file") != 0 )
192 for (
int j = 0; j < con_array[i]->connector->jlcb.count - last_working_pos; j++)
193 con_array[i]->connector->jlcb.buffer[j] = con_array[i]->connector->jlcb.buffer[j + last_working_pos];
194 con_array[i]->connector->jlcb.count -= last_working_pos;
197 con_array[i]->connector->jlcb.count = 0;
202 json_object_set_new( message->
json_root,
"type", json_string(
"exit" ) );
203 con_array[i]->connector->clientSendMessage(message);
208 close(fd_array[i].fd);
210 for (
int j = i; j < fdnum; j++)
212 fd_array[j] = fd_array[j+1];
213 con_array[j] = con_array[j+1];
215 memset(&(fd_array[fdnum]),0,
sizeof(fd_array[fdnum]));
234 printf(
"Waiting for Connections %i to finish... ",mom->
connector->
getID());
ThreadList< InsituConnectorContainer *> insituConnectorList
size_t json_load_callback_function(void *buffer, size_t buflen, void *data)
#define ISAAC_PROTOCOL_VERSION_MINOR
#define ISAAC_PROTOCOL_VERSION_MAJOR
static void bindInterface(in_addr_t &s_addr, std::string interface, bool ipv6=false)
InsituConnector * connector
char buffer[ISAAC_MAX_RECEIVE]
struct InsituConnectorContainer_struct InsituConnectorContainer
errorCode init(int port, std::string interface)