riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Loading...
Searching...
No Matches
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 ()
 
 SocketConnector ()
 

Public Attributes

SOCKET rc_socket
 
bool b_connected
 
std::string ip
 
int port
 
std::string connection_information
 

Detailed Description

Definition at line 36 of file SocketConnector.h.

Constructor & Destructor Documentation

◆ SocketConnector()

SocketConnector::SocketConnector ( )
inline

Definition at line 50 of file SocketConnector.h.

51 b_connected(false),
52 ip("127.0.0.1"),
53 port(6342){};
#define INVALID_SOCKET

Member Function Documentation

◆ close_socket()

void SocketConnector::close_socket ( )

Definition at line 137 of file SocketConnector.cpp.

138{
139 close(rc_socket);
140}

◆ connect_socket()

void SocketConnector::connect_socket ( )

Definition at line 63 of file SocketConnector.cpp.

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}
#define SOCKET_ERROR

◆ flush_socket()

void SocketConnector::flush_socket ( )

Definition at line 42 of file SocketConnector.cpp.

43{
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 }
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(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}

Member Data Documentation

◆ b_connected

bool SocketConnector::b_connected

Definition at line 41 of file SocketConnector.h.

◆ connection_information

std::string SocketConnector::connection_information

Definition at line 45 of file SocketConnector.h.

◆ ip

std::string SocketConnector::ip

Definition at line 42 of file SocketConnector.h.

◆ port

int SocketConnector::port

Definition at line 43 of file SocketConnector.h.

◆ rc_socket

SOCKET SocketConnector::rc_socket

Definition at line 39 of file SocketConnector.h.


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