+ /* main loop */
+ while (1) {
+ if (accept_incoming_connection () == 0) {
+ usleep (1e5);
+ continue;
+ }
+
+ VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n"));
+
+ char *data = NULL;
+ char *output = NULL;
+
+ int len = receive_data (&data);
+ if (len == 0) {
+ VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n"));
+ } else if (len < 0) {
+ VERBOSE (WARNING, PRINT ("Connection in error (rx)\n"));
+ } else {
+ VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len));
+
+ // processing
+ VERBOSE (DEBUG, PRINT ("Processing %s\n", data));
+ len = processing (data, len, &conf, &output);
+
+ VERBOSE (DEBUG, PRINT ("Sending data (%d)\n%s\n", len, data));
+ int rc = send_data (output, len);
+ if (rc == 0) {
+ VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n"));
+ } else if (rc < 0) {
+ VERBOSE (WARNING, PRINT ("Connection in error (tx)\n"));
+ }
+ }
+
+ VERBOSE (DEBUG, PRINT ("Closing connection\n"));
+ if (data) {
+ free (data);
+ }
+ if (output) {
+ free (output);
+ }
+ close_connection ();
+ }
+
+ VERBOSE (DEBUG, PRINT ("Closing socket\n"));
+ terminate_network_context ();