udp_server.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Yaskawa America, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Yaskawa America, Inc., nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef FLATHEADERS
35 #else
36 #include "udp_server.h"
37 #include "log_wrapper.h"
38 #endif
39 
40 using namespace industrial::byte_array;
41 
42 namespace industrial
43 {
44 namespace udp_server
45 {
46 
47 UdpServer::UdpServer()
48 {
49  this->setConnected(false);
50 }
51 
52 UdpServer::~UdpServer()
53 {
54 }
55 
56 
57 
58 bool UdpServer::init(int port_num)
59 {
60  int rc = this->SOCKET_FAIL;
61  bool rtn;
62  SOCKLEN_T addrSize = 0;
63 
64  /* Create a socket using:
65  * AF_INET - IPv4 internet protocol
66  * SOCK_DGRAM - UDP type
67  * protocol (0) - System chooses
68  */
69  rc = SOCKET(AF_INET, SOCK_DGRAM, 0);
70  if (this->SOCKET_FAIL != rc)
71  {
72  this->setSockHandle(rc);
73  LOG_DEBUG("Socket created, rc: %d", rc);
74  LOG_DEBUG("Socket handle: %d", this->getSockHandle());
75 
76  // Initialize address data structure
77  memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
78  this->sockaddr_.sin_family = AF_INET;
79  this->sockaddr_.sin_addr.s_addr = INADDR_ANY;
80  this->sockaddr_.sin_port = HTONS(port_num);
81 
82  // This set the socket to be non-blocking (NOT SURE I WANT THIS) - sme
83  //fcntl(sock_handle, F_SETFL, O_NONBLOCK);
84 
85  addrSize = sizeof(this->sockaddr_);
86  rc = BIND(this->getSockHandle(), (sockaddr *)&(this->sockaddr_), addrSize);
87 
88  if (this->SOCKET_FAIL != rc)
89  {
90  rtn = true;
91  LOG_INFO("Server socket successfully initialized");
92  }
93  else
94  {
95  LOG_ERROR("Failed to bind socket, rc: %d", rc);
96  CLOSE(this->getSockHandle());
97  rtn = false;
98  }
99  }
100  else
101  {
102  LOG_ERROR("Failed to create socket, rc: %d", rc);
103  rtn = false;
104  }
105  return rtn;
106 }
107 
108 
109 bool UdpServer::makeConnect()
110 {
111  ByteArray send;
112  char sendHS = this->CONNECT_HANDSHAKE;
113  char recvHS = 0;
114  int bytesRcvd = 0;
115  const int timeout = 1000; // Time (ms) between handshake sends
116  bool rtn = false;
117 
118  send.load((void*)&sendHS, sizeof(sendHS));
119 
120  if (!this->isConnected())
121  {
122  this->setConnected(false);
123 
124  // Listen for handshake. Once received, break
125  // listen loop.
126  do
127  {
128  ByteArray recv;
129  recvHS = 0;
130  if (this->isReadyReceive(timeout))
131  {
132  bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
133 
134  if (bytesRcvd > 0)
135  {
136  LOG_DEBUG("UDP server received %d bytes while waiting for handshake", bytesRcvd);
137  recv.init(&this->buffer_[0], bytesRcvd);
138  recv.unload((void*)&recvHS, sizeof(recvHS));
139  }
140  }
141 
142  }
143  while(recvHS != sendHS);
144 
145  // copy to local array, since ByteArray no longer supports
146  // direct pointer-access to data values
147  const int sendLen = send.getBufferSize();
148  std::vector<char> localBuffer(sendLen);
149  send.unload(localBuffer.data(), sendLen);
150 
151  // Send a reply handshake
152  this->rawSendBytes(localBuffer.data(), sendLen);
153  this->setConnected(true);
154  rtn = true;
155 
156  }
157  else
158  {
159  LOG_WARN("Tried to connect when socket already in connected state");
160  rtn = true;
161  }
162 
163  return rtn;
164 }
165 
166 
167 } //udp_server
168 } //industrial
169 
LOG_INFO
#define LOG_INFO(format,...)
Definition: log_wrapper.h:132
LOG_WARN
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
industrial
Definition: byte_array.h:45
industrial::byte_array::ByteArray::unload
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
Definition: byte_array.cpp:233
udp_server.h
LOG_ERROR
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
LOG_DEBUG
#define LOG_DEBUG(format,...)
Definition: log_wrapper.h:131
industrial::byte_array::ByteArray
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
industrial::byte_array::ByteArray::init
void init()
Initializes or Reinitializes an empty buffer.
Definition: byte_array.cpp:62
log_wrapper.h
industrial::byte_array
Definition: byte_array.h:56
industrial::byte_array::ByteArray::load
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
Definition: byte_array.cpp:142
industrial::byte_array::ByteArray::getBufferSize
unsigned int getBufferSize()
gets current buffer size
Definition: byte_array.cpp:387


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53