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(rc_socket,
23  &buffer[bytes_payload_total],
24  data_size - bytes_payload_total,
25  0);
26 
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(rc_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 {
65 #ifdef WIN32
66  int error = WSAStartup(0x0202, &w);
67  if (error)
68  {
69  exit(EXIT_FAILURE);
70  }
71 #endif
72 
73  // Creating socket file descriptor
74  rc_socket = socket(AF_INET, SOCK_STREAM, 0);
76  {
77  handle_socket_errors("intitializing Socket");
78  }
79 
80  if (setsockopt(rc_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == SOCKET_ERROR)
81  {
82  handle_socket_errors("setting socket options");
83  }
84 
85  address.sin_family = AF_INET;
86  address.sin_addr.s_addr = inet_addr(ip.c_str());
87  address.sin_port = htons(port);
88 
89  std::cout << "Waiting for incoming connection..." << std::endl;
90 
91  // Connecting socket to the port
92  int error_counter = 0;
93  while (true)
94  {
95  if (connect(rc_socket, (struct sockaddr *)&address, sizeof(address)) == SOCKET_ERROR)
96  {
97  if (error_counter < 1)
98  {
99  handle_socket_errors("connecting to Socket");
100  }
101  error_counter++;
102  }
103  else
104  {
105  std::cout << "Connected by " << inet_ntoa(address.sin_addr) << "\n";
106  b_connected = true;
107  break;
108  }
109  }
110 }
111 
112 #ifdef WIN32
113 void SocketConnector::handle_socket_errors(const std::string &raised_at)
114 {
115  wchar_t *s = NULL;
116  FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
117  NULL, WSAGetLastError(),
118  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
119  (LPWSTR)&s, 0, NULL);
120  std::cout << "Error occured while " << raised_at << "." << std::endl;
121  fprintf(stderr, "%S\n", s);
122  LocalFree(s);
123 }
124 
126 {
127  closesocket(rc_socket);
128  WSACleanup();
129 }
130 #else
131 void SocketConnector::handle_socket_errors(const std::string &raised_at)
132 {
133  std::cout << "Error occured while " << raised_at << "." << std::endl;
134  std::cout << std::strerror(errno) << std::endl;
135 }
136 
138 {
139  close(rc_socket);
140 }
141 #endif
#define INVALID_SOCKET
#define SOCKET_ERROR
std::string ip
int read_data(char *buffer, int data_size)