riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
SocketConnector.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #include "SocketConnector.h"
15 
16 int SocketConnector::read_data(char *buffer, int data_size)
17 {
18  int bytes_payload_total = 0;
19 
20  while (bytes_payload_total < data_size)
21  {
22  int bytes_payload_count = recv(client_socket,
23  &buffer[bytes_payload_total],
24  data_size - bytes_payload_total,
25  0);
26  std::cout << bytes_payload_count << std::endl;
27  if (bytes_payload_count == -1)
28  {
29  perror("Error reading Data!");
30  return -1;
31  }
32  else if (bytes_payload_count == 0)
33  {
34  std::cout << "Unexpected end of transmission" << std::endl;
35  return -1;
36  }
37  bytes_payload_total += bytes_payload_count;
38  }
39  return 0;
40 }
41 
43 {
44  close_socket();
46  char *buffer = {0};
47  int bytes_total = 0;
48 
49  while (true)
50  {
51  int bytes_count = recv(client_socket, &buffer[0], 1, 0);
52 
53  if (bytes_count <= 0)
54  {
55  std::cout << "Socket flushed (" << bytes_total / 1024 << " kB)" << std::endl;
56  break;
57  }
58  bytes_total += bytes_count;
59  }
60  close_socket();
61 }
62 
64  client_socket = -1;
65  socklen_t addrlen = sizeof(c_address);
66  while (client_socket == -1) {
67  client_socket = accept(rc_socket, (struct sockaddr*)&c_address, &addrlen);
68  }
69 }
70 
71 void SocketConnector::init_listen(){
72  int bindResult = bind(rc_socket, (struct sockaddr*)&address, sizeof(address));
73  if (bindResult == -1) {
74  handle_socket_errors("binding the Socket");
75  }
76 
77  int listenResult = listen(rc_socket, 1); // Queue up to 5 pending connections
78  if (listenResult == -1) {
79  handle_socket_errors("listening on the Socket");
80  }
81  else
82  {
83  b_connected = true;
84  }
85 }
86 
87 void SocketConnector::init_connect(){
88  // Connecting socket to the port
89  int error_counter = 0;
90  while (true)
91  {
92  if (connect(rc_socket, (struct sockaddr *)&address, sizeof(address)) == SOCKET_ERROR)
93  {
94  if (error_counter < 1)
95  {
96  handle_socket_errors("connecting to Socket");
97  }
98  error_counter++;
99  }
100  else
101  {
102  std::cout << "Connected by " << inet_ntoa(address.sin_addr) << "\n";
103  b_connected = true;
104  break;
105  }
106  }
107 }
108 
110 {
111 #ifdef WIN32
112  int error = WSAStartup(0x0202, &w);
113  if (error)
114  {
115  exit(EXIT_FAILURE);
116  }
117 #endif
118 
119  // Creating socket file descriptor
120  rc_socket = socket(AF_INET, SOCK_STREAM, 0);
121 
122  if (rc_socket == INVALID_SOCKET)
123  {
124  handle_socket_errors("intitializing Socket");
125  }
126 
127  if (setsockopt(rc_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == SOCKET_ERROR)
128  {
129  handle_socket_errors("setting socket options");
130  }
131 
132  address.sin_family = AF_INET;
133  address.sin_addr.s_addr = inet_addr(ip.c_str());
134  address.sin_port = htons(port);
135 
137  {
138  init_connect();
139  }
140  else if (socket_type == Socket_type::SERVER)
141  {
142  init_listen();
143  }
144 }
145 
146 #ifdef WIN32
147 void SocketConnector::handle_socket_errors(const std::string &raised_at)
148 {
149  wchar_t *s = NULL;
150  FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
151  NULL, WSAGetLastError(),
152  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
153  (LPWSTR)&s, 0, NULL);
154  std::cout << "Error occured while " << raised_at << "." << std::endl;
155  fprintf(stderr, "%S\n", s);
156  LocalFree(s);
157 }
158 
160 {
161  closesocket(rc_socket);
162  b_connected = false;
163  WSACleanup();
164 }
165 #else
166 void SocketConnector::handle_socket_errors(const std::string &raised_at)
167 {
168  std::cout << "Error occured while " << raised_at << "." << std::endl;
169  std::cout << std::strerror(errno) << std::endl;
170 }
171 
173 {
174  close(rc_socket);
175  b_connected = false;
176 }
177 #endif
#define INVALID_SOCKET
#define SOCKET_ERROR
@ CLIENT
@ SERVER
Socket_type socket_type
std::string ip
int read_data(char *buffer, int data_size)