riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Public Member Functions | Public Attributes | List of all members
SocketConnector Class Reference

#include <SocketConnector.h>

Public Member Functions

int read_data (char *buffer, int data_size)
 
void flush_socket ()
 
void connect_socket ()
 
void close_socket ()
 
void accept_socket ()
 
 SocketConnector ()
 

Public Attributes

SOCKET rc_socket
 
SOCKET client_socket
 
Socket_type socket_type
 
bool b_connected
 
std::string ip
 
int port
 
std::string connection_information
 

Detailed Description

Definition at line 37 of file SocketConnector.h.

Constructor & Destructor Documentation

◆ SocketConnector()

SocketConnector::SocketConnector ( )
inline

Definition at line 54 of file SocketConnector.h.

55  b_connected(false),
56  ip("127.0.0.1"),
57  // port(6342){}; // merlin
58  port(8451){}; // cheetah
#define INVALID_SOCKET
std::string ip

Member Function Documentation

◆ accept_socket()

void SocketConnector::accept_socket ( )

Definition at line 63 of file SocketConnector.cpp.

63  {
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 }

◆ close_socket()

void SocketConnector::close_socket ( )

Definition at line 172 of file SocketConnector.cpp.

173 {
174  close(rc_socket);
175  b_connected = false;
176 }

◆ connect_socket()

void SocketConnector::connect_socket ( )

Definition at line 109 of file SocketConnector.cpp.

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 }
#define SOCKET_ERROR
@ CLIENT
@ SERVER
Socket_type socket_type

◆ flush_socket()

void SocketConnector::flush_socket ( )

Definition at line 42 of file SocketConnector.cpp.

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 }

◆ read_data()

int SocketConnector::read_data ( char *  buffer,
int  data_size 
)

Definition at line 16 of file SocketConnector.cpp.

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 }

Member Data Documentation

◆ b_connected

bool SocketConnector::b_connected

Definition at line 44 of file SocketConnector.h.

◆ client_socket

SOCKET SocketConnector::client_socket

Definition at line 41 of file SocketConnector.h.

◆ connection_information

std::string SocketConnector::connection_information

Definition at line 48 of file SocketConnector.h.

◆ ip

std::string SocketConnector::ip

Definition at line 45 of file SocketConnector.h.

◆ port

int SocketConnector::port

Definition at line 46 of file SocketConnector.h.

◆ rc_socket

SOCKET SocketConnector::rc_socket

Definition at line 40 of file SocketConnector.h.

◆ socket_type

Socket_type SocketConnector::socket_type

Definition at line 42 of file SocketConnector.h.


The documentation for this class was generated from the following files: