Jetson Inference
DNN Vision Library
Socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __NETWORK_SOCKET_H_
24 #define __NETWORK_SOCKET_H_
25 
26 #include <stdint.h>
27 #include <cstddef>
28 
29 
35 {
40 
45 };
46 
47 
48 #define IP_ANY 0x00000000
49 #define IP_BROADCAST 0xFFFFFFFF
50 #define IP_LOOPBACK 0x7F000001
51 
52 
63 class Socket
64 {
65 public:
69  static Socket* Create( SocketType type );
70 
74  ~Socket();
75 
80  bool Accept( uint64_t timeout=0 );
81 
88  bool Bind( const char* localIP, uint16_t port );
89 
97  bool Bind( uint32_t localIP, uint16_t port );
98 
104  bool Bind( uint16_t port=0 );
105 
110  bool Connect( const char* remoteIP, uint16_t port );
111 
116  bool Connect( uint32_t remoteIP, uint16_t port );
117 
127  size_t Recieve( uint8_t* buffer, size_t size, uint32_t* remoteIP=NULL, uint16_t* remotePort=NULL );
128 
135  size_t Recieve( uint8_t* buffer, size_t size, uint32_t* remoteIP, uint16_t* remotePort, uint32_t* localIP );
136 
140  bool Send( void* buffer, size_t size, uint32_t remoteIP, uint16_t remotePort );
141 
145  bool SetRecieveTimeout( uint64_t timeout );
146 
150  bool SetBufferSize( size_t size );
151 
155  bool EnableJumboBuffer();
156 
160  inline int GetFD() const { return mSock; }
161 
165  inline SocketType GetType() const { return mType; }
166 
170  void PrintIP() const;
171 
176  size_t GetMTU();
177 
178 private:
179 
180  Socket( SocketType type );
181  bool Init();
182 
183  int mSock;
184  SocketType mType;
185  bool mListening;
186  bool mPktInfoEnabled;
187  bool mBroadcastEnabled;
188 
189  uint32_t mLocalIP;
190  uint16_t mLocalPort;
191 
192  uint32_t mRemoteIP;
193  uint16_t mRemotePort;
194 };
195 
196 #endif
Socket::Recieve
size_t Recieve(uint8_t *buffer, size_t size, uint32_t *remoteIP=NULL, uint16_t *remotePort=NULL)
Wait for a packet to be recieved and dump it into the user-supplied buffer.
Socket::GetType
SocketType GetType() const
GetType.
Definition: Socket.h:165
Socket::Bind
bool Bind(const char *localIP, uint16_t port)
Bind the socket to a local host IP address and port.
Socket::Connect
bool Connect(const char *remoteIP, uint16_t port)
Connect to a listening server (TCP only).
Socket::Create
static Socket * Create(SocketType type)
Create.
Socket::PrintIP
void PrintIP() const
PrintIP.
Socket::SetBufferSize
bool SetBufferSize(size_t size)
Set the rx/tx buffer sizes.
Socket::GetFD
int GetFD() const
Retrieve the socket's file descriptor.
Definition: Socket.h:160
Socket::GetMTU
size_t GetMTU()
Retrieve the MTU (in bytes).
Socket::Send
bool Send(void *buffer, size_t size, uint32_t remoteIP, uint16_t remotePort)
Send message to remote host.
Socket
The Socket class provides TCP or UDP ethernet networking.
Definition: Socket.h:63
SOCKET_TCP
@ SOCKET_TCP
flag indicating TCP virtual circuit service (SOCK_STREAM)
Definition: Socket.h:44
SocketType
SocketType
TCP/UDP enumeration.
Definition: Socket.h:34
Socket::EnableJumboBuffer
bool EnableJumboBuffer()
Enable jumbo buffer.
SOCKET_UDP
@ SOCKET_UDP
flag indicating UDP datagram service (SOCK_DGRAM)
Definition: Socket.h:39
Socket::SetRecieveTimeout
bool SetRecieveTimeout(uint64_t timeout)
Set Receive() timeout (in microseconds).
Socket::Accept
bool Accept(uint64_t timeout=0)
Accept incoming connections (TCP only).
Socket::~Socket
~Socket()
Destructor.