DESERT 3.6.0
Loading...
Searching...
No Matches
uwApplication_UDP_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 // Create socket for incoming connections
48 if ((servSockDescr = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
49 printOnLog(Logger::LogLevel::ERROR,
50 "UWAPPLICATION",
51 "openConnectionUDP()::Socket creation failed");
52
53 return false;
54 }
55
56 if (setsockopt(servSockDescr,
57 SOL_SOCKET,
58 SO_REUSEADDR,
59 &sockoptval,
60 sizeof(int)) == -1) {
61 printOnLog(Logger::LogLevel::ERROR,
62 "UWAPPLICATION",
63 "openConnectionUDP()::Set socket failed");
64 }
65
66 printOnLog(Logger::LogLevel::INFO,
67 "UWAPPLICATION",
68 "openConnectionUDP()::Socket created");
69
70 memset(&servAddr, 0, sizeof(servAddr));
71 servAddr.sin_family = AF_INET;
72 servAddr.sin_port = htons(servPort);
73 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
74
75 if (::bind(servSockDescr, (struct sockaddr *) &servAddr, sizeof(servAddr)) <
76 0) {
77 printOnLog(Logger::LogLevel::ERROR,
78 "UWAPPLICATION",
79 "openConnectionUDP()::Socket binding failed: " +
80 std::string(strerror(errno)));
81
82 return false;
83 }
84
85 return true;
86}
87
88void
90{
91 char buffer_msg[MAX_LENGTH_PAYLOAD];
92 int recvMsgSize;
93 socklen_t clnLen = sizeof(sockaddr_in);
94
95 while (receiving.load()) {
96 clnLen = sizeof(clnAddr);
97 for (int i = 0; i < MAX_LENGTH_PAYLOAD; i++) {
98 buffer_msg[i] = 0;
99 }
100
101 if ((recvMsgSize = recvfrom(servSockDescr,
102 buffer_msg,
104 0,
105 (struct sockaddr *) &(clnAddr),
106 &clnLen)) < 0) {
107
108 printOnLog(Logger::LogLevel::ERROR,
109 "UWAPPLICATION",
110 "readFromUDP()::Receive from socket failed");
111
112 continue;
113 }
114
115 if (recvMsgSize > 0) {
116 std::unique_lock<std::mutex> lk(socket_mutex);
117 Packet *p = Packet::alloc();
118 hdr_cmn *ch = HDR_CMN(p);
120
121 printOnLog(Logger::LogLevel::DEBUG,
122 "UWAPPLICATION",
123 "readFromUDP()::Socket payload received : " +
124 std::string(buffer_msg, recvMsgSize));
125
126 for (int i = 0; i < recvMsgSize; i++)
127 hdr_Appl->payload_msg[i] = buffer_msg[i];
128 ch->size() = recvMsgSize;
129 hdr_Appl->payload_size() = recvMsgSize;
130
131 queuePckReadUDP.push(p);
133 }
134 }
135}
std::atomic< bool > receiving
virtual void readFromUDP()
Method that waits for UDP packets from external application and converts it to a Packet.
virtual bool openConnectionUDP()
When socket communication is used, this method establish a connection between client and server.
int servSockDescr
Socket descriptor for server.
std::queue< Packet * > queuePckReadUDP
Queue that store the DATA packets recevied from the client by the server using a UDP protocol.
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.
struct sockaddr_in servAddr
Server address.
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.
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.