DESERT 3.6.0
Loading...
Searching...
No Matches
uwApplication_TCP_socket.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
41
42bool
44{
45 int sockoptval = 1;
46
47 if ((servSockDescr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
48 printOnLog(Logger::LogLevel::ERROR,
49 "UWAPPLICATION",
50 "listenTCP()::Socket creation failed");
51
52 return false;
53 }
54
55 if (setsockopt(servSockDescr,
56 SOL_SOCKET,
57 SO_REUSEADDR,
58 &sockoptval,
59 sizeof(int)) == -1) {
60
61 printOnLog(Logger::LogLevel::ERROR,
62 "UWAPPLICATION",
63 "listenTCP()::Set socket failed");
64
65 return false;
66 }
67
68 printOnLog(Logger::LogLevel::INFO,
69 "UWAPPLICATION",
70 "listenTCP()::Socket created");
71
72 memset(&servAddr, 0, sizeof(servAddr));
73 servAddr.sin_family = AF_INET;
74 servAddr.sin_port = htons(servPort);
75 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
76
77 if (::bind(servSockDescr, (struct sockaddr *) &servAddr, sizeof(servAddr)) <
78 0) {
79 printOnLog(Logger::LogLevel::ERROR,
80 "UWAPPLICATION",
81 "listenTCP()::Socket binding failed: " +
82 std::string(strerror(errno)));
83
84 return false;
85 }
86
87 if (listen(servSockDescr, 1)) {
88 printOnLog(Logger::LogLevel::ERROR,
89 "UWAPPLICATION",
90 "listenTCP()::Socket listen failed");
91
92 return false;
93 }
94
95 printOnLog(Logger::LogLevel::INFO,
96 "UWAPPLICATION",
97 "listenTCP()::Socket listening");
98
99 return true;
100}
101
102void
104{
105 socklen_t clnLen = sizeof(sockaddr_in);
106
107 clnLen = sizeof(clnAddr);
108
109 while (receiving.load()) {
110 if ((clnSockDescr = accept(servSockDescr,
111 (struct sockaddr *) &(clnAddr),
112 (socklen_t *) &clnLen)) < 0) {
113 printOnLog(Logger::LogLevel::ERROR,
114 "UWAPPLICATION",
115 "acceptTCP()::Socket connection not accepted");
116
117 continue;
118 }
119
120 printOnLog(Logger::LogLevel::INFO,
121 "UWAPPLICATION",
122 "acceptTCP()::Socket accept connection from " +
123 std::string(inet_ntoa(clnAddr.sin_addr)));
124
126 }
127}
128
129void
131{
132 while (true) {
133 int recvMsgSize = 0;
134 char buffer_msg[MAX_LENGTH_PAYLOAD];
135
136 for (int i = 0; i < MAX_LENGTH_PAYLOAD; i++) {
137 buffer_msg[i] = 0;
138 }
139
140 if ((recvMsgSize = read(clnSock, buffer_msg, MAX_READ_LEN)) < 0) {
141 printOnLog(Logger::LogLevel::ERROR,
142 "UWAPPLICATION",
143 "readFromTCP(int)::Read from socket failed");
144
145 break;
146 }
147
148 if (recvMsgSize == 0) {
149 printOnLog(Logger::LogLevel::INFO,
150 "UWAPPLICATION",
151 "readFromTCP(int)::Socket disconnected");
152
153 shutdown(clnSock, SHUT_RDWR);
154 break;
155 }
156
157 std::unique_lock<std::mutex> lk(socket_mutex);
158
159 Packet *p = Packet::alloc();
160 hdr_cmn *ch = HDR_CMN(p);
162
163 printOnLog(Logger::LogLevel::DEBUG,
164 "UWAPPLICATION",
165 "readFromTCP(int)::Socket payload received : " +
166 std::string(buffer_msg, recvMsgSize));
167
168 for (int i = 0; i < recvMsgSize; i++)
169 hdr_Appl->payload_msg[i] = buffer_msg[i];
170 ch->size() = recvMsgSize;
171 hdr_Appl->payload_size() = recvMsgSize;
172
173 queuePckReadTCP.push(p);
175
176 lk.unlock();
177 }
178}
virtual void readFromTCP(int clnSock)
Method that reads a TCP byte stream from external application and converts it to a Packet.
std::atomic< bool > receiving
int servSockDescr
Socket descriptor for server.
int servPort
Socket server port.
virtual void incrPktsPushQueue()
Increase the number of DATA packets stored in the Server queue.
std::mutex socket_mutex
Mutex associated with the socket rx thread.
virtual bool listenTCP()
Method that binds the listening TCP socket.
virtual void acceptTCP()
Method that puts in place a listening TCP socket.
int clnSockDescr
Socket descriptor for client.
struct sockaddr_in servAddr
Server address.
static uint MAX_READ_LEN
Maximum size (bytes) of a single read of the socket.
struct sockaddr_in clnAddr
Client address.
virtual void printOnLog(Logger::LogLevel log_level, const std::string &module, const std::string &message) const override
Method to send the log message to the logger.
std::queue< Packet * > queuePckReadTCP
Atomic boolean variable that controls the socket rx looping thread.
Content header of DATA packet.
char payload_msg[MAX_LENGTH_PAYLOAD]
Message payload.
#define MAX_LENGTH_PAYLOAD
#define HDR_DATA_APPLICATION(p)
Alias defined to access the DATA HEADER.