1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/**
* @class vtkSocket
* @brief BSD socket encapsulation.
*
* This abstract class encapsulates a BSD socket. It provides an API for
* basic socket operations.
*/
#ifndef vtkSocket_h
#define vtkSocket_h
#include "vtkCommonSystemModule.h" // For export macro
#include "vtkObject.h"
VTK_ABI_NAMESPACE_BEGIN
class vtkSocketCollection;
class VTKCOMMONSYSTEM_EXPORT vtkSocket : public vtkObject
{
public:
vtkTypeMacro(vtkSocket, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
// ----- Status API ----
/**
* Check is the socket is alive.
*/
int GetConnected() { return (this->SocketDescriptor >= 0); }
/**
* Close the socket.
*/
void CloseSocket();
// ------ Communication API ---
/**
* These methods send data over the socket.
* Returns 1 on success, 0 on error and raises vtkCommand::ErrorEvent.
*/
int Send(const void* data, int length);
/**
* Receive data from the socket.
* This call blocks until some data is read from the socket.
* When readFully is set, this call will block until all the
* requested data is read from the socket.
* 0 on error, else number of bytes read is returned. On error,
* vtkCommand::ErrorEvent is raised.
*/
int Receive(void* data, int length, int readFully = 1);
/**
* Provides access to the internal socket descriptor. This is valid only when
* GetConnected() returns true.
*/
vtkGetMacro(SocketDescriptor, int);
/**
* Selects set of sockets. Returns 0 on timeout, -1 on error.
* 1 on success. Selected socket's index is returned through
* selected_index
*/
static int SelectSockets(
const int* sockets_to_select, int size, unsigned long msec, int* selected_index);
protected:
vtkSocket();
~vtkSocket() override;
int SocketDescriptor;
friend class vtkSocketCollection;
/**
* Creates an endpoint for communication and returns the descriptor.
* -1 indicates error.
*/
int CreateSocket();
/**
* Close the socket.
*/
void CloseSocket(int socketdescriptor);
/**
* Binds socket to a particular port.
* Returns 0 on success other -1 is returned.
*/
int BindSocket(int socketdescriptor, int port);
/**
* Selects a socket ie. waits for it to change status.
* Returns 1 on success; 0 on timeout; -1 on error. msec=0 implies
* no timeout.
*/
int SelectSocket(int socketdescriptor, unsigned long msec);
/**
* Accept a connection on a socket. Returns -1 on error. Otherwise
* the descriptor of the accepted socket.
*/
int Accept(int socketdescriptor);
/**
* Listen for connections on a socket. Returns 0 on success. -1 on error.
*/
int Listen(int socketdescriptor);
/**
* Connect to a server socket. Returns 0 on success, -1 on error.
*/
int Connect(int socketdescriptor, const char* hostname, int port);
/**
* Returns the port to which the socket is connected.
* 0 on error.
*/
int GetPort(int socketdescriptor);
private:
vtkSocket(const vtkSocket&) = delete;
void operator=(const vtkSocket&) = delete;
};
VTK_ABI_NAMESPACE_END
#endif
|