vrpn 07.35
Virtual Reality Peripheral Network
Loading...
Searching...
No Matches
vrpn_Connection.C File Reference
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <cctype>
#include <stdlib.h>
#include "vrpn_Connection.h"
#include <string>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/wait.h>
#include <netinet/tcp.h>
#include "vrpn_FileConnection.h"
#include "vrpn_Log.h"
Include dependency graph for vrpn_Connection.C:

Go to the source code of this file.

Macros

#define vrpn_closeSocket   close
 
#define vrpn_socket_error   errno
 
#define vrpn_socket_error_to_chars(x)   strerror(x)
 
#define vrpn_EINTR   EINTR
 
#define SOCK_CAST
 
#define GSN_CAST
 
#define INVALID_SOCKET   -1
 
#define RSH   "/usr/bin/rsh"
 
#define UDP_CALL_TIMEOUT   (2)
 
#define UDP_CALL_RETRIES   (5)
 
#define SERVCOUNT   (20)
 
#define SERVWAIT   (120 / SERVCOUNT)
 
#define vrpn_CONNECTION_MAX_XLATION_TABLE_SIZE   2000
 

Functions

size_t vrpn_cookie_size (void)
 Returns the size of the magic cookie buffer, plus any alignment overhead.
 
int vrpn_noint_select (int width, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 This routine will perform like a normal select() call, but it will restart if it quit because of an interrupt.
 
int vrpn_noint_block_write (int outfile, const char buffer[], size_t length)
 
int vrpn_noint_block_read (int infile, char buffer[], size_t length)
 
int vrpn_noint_block_read_timeout (SOCKET infile, char buffer[], size_t length, struct timeval *timeout)
 This routine will read in a block from the file descriptor.
 
int vrpn_udp_request_lob_packet (SOCKET udp_sock, const char *, const int, const int local_port, const char *NIC_IP=NULL)
 This section deals with implementing a method of connection termed a UDP request.
 
int write_vrpn_cookie (char *buffer, size_t length, long remote_log_mode)
 Writes the magic cookie into buffer with given length.
 
int check_vrpn_cookie (const char *buffer)
 Checks to see if the given buffer has the magic cookie.
 
int check_vrpn_file_cookie (const char *buffer)
 
vrpn_Connectionvrpn_get_connection_by_name (const char *cname, const char *local_in_logfile_name, const char *local_out_logfile_name, const char *remote_in_logfile_name, const char *remote_out_logfile_name, const char *NIC_IPaddress, bool force_connection)
 Create a client connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).
 
vrpn_Connectionvrpn_create_server_connection (const char *cname, const char *local_in_logfile_name, const char *local_out_logfile_name)
 Create a server connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).
 
char * vrpn_copy_service_name (const char *fullname)
 
char * vrpn_copy_service_location (const char *fullname)
 
char * vrpn_copy_file_name (const char *filespecifier)
 Utility routines to parse file specifiers FROM service locations.
 
char * vrpn_copy_machine_name (const char *hostspecifier)
 
int vrpn_get_port_number (const char *hostspecifier)
 
char * vrpn_copy_rsh_program (const char *hostspecifier)
 
char * vrpn_copy_rsh_arguments (const char *hostspecifier)
 
char * vrpn_set_service_name (const char *specifier, const char *newServiceName)
 Utility routine to rename the service name of a given host specifier.
 

Variables

const char * vrpn_MAGIC = (const char *)"vrpn: ver. 07.35"
 
const char * vrpn_FILE_MAGIC = (const char *)"vrpn: ver. 04.00"
 
const int vrpn_MAGICLEN = 16
 
const size_t vrpn_COOKIE_SIZE = vrpn_MAGICLEN + vrpn_ALIGN
 
const char * vrpn_got_first_connection = "VRPN_Connection_Got_First_Connection"
 These are the strings that define the system-generated message types that tell when connections are received and dropped.
 
const char * vrpn_got_connection = "VRPN_Connection_Got_Connection"
 
const char * vrpn_dropped_connection = "VRPN_Connection_Dropped_Connection"
 
const char * vrpn_dropped_last_connection
 
const char * vrpn_CONTROL = "VRPN Control"
 vrpn_CONTROL is the sender used for notification messages sent to the user from the local VRPN implementation (got_first_connection, etc.) and for control messages sent by auxiliary services. (Such as class vrpn_Controller, which will be introduced in a future revision.)
 

Macro Definition Documentation

◆ GSN_CAST

#define GSN_CAST

Definition at line 113 of file vrpn_Connection.C.

◆ INVALID_SOCKET

#define INVALID_SOCKET   -1

Definition at line 136 of file vrpn_Connection.C.

◆ RSH

#define RSH   "/usr/bin/rsh"

Definition at line 240 of file vrpn_Connection.C.

◆ SERVCOUNT

#define SERVCOUNT   (20)

Definition at line 252 of file vrpn_Connection.C.

◆ SERVWAIT

#define SERVWAIT   (120 / SERVCOUNT)

Definition at line 253 of file vrpn_Connection.C.

◆ SOCK_CAST

#define SOCK_CAST

Definition at line 103 of file vrpn_Connection.C.

◆ UDP_CALL_RETRIES

#define UDP_CALL_RETRIES   (5)

Definition at line 246 of file vrpn_Connection.C.

◆ UDP_CALL_TIMEOUT

#define UDP_CALL_TIMEOUT   (2)

Definition at line 245 of file vrpn_Connection.C.

◆ vrpn_closeSocket

#define vrpn_closeSocket   close

Definition at line 57 of file vrpn_Connection.C.

◆ vrpn_CONNECTION_MAX_XLATION_TABLE_SIZE

#define vrpn_CONNECTION_MAX_XLATION_TABLE_SIZE   2000

Definition at line 258 of file vrpn_Connection.C.

◆ vrpn_EINTR

#define vrpn_EINTR   EINTR

Definition at line 61 of file vrpn_Connection.C.

◆ vrpn_socket_error

#define vrpn_socket_error   errno

Definition at line 59 of file vrpn_Connection.C.

◆ vrpn_socket_error_to_chars

#define vrpn_socket_error_to_chars (   x)    strerror(x)

Definition at line 60 of file vrpn_Connection.C.

Function Documentation

◆ check_vrpn_cookie()

int check_vrpn_cookie ( const char *  buffer)

Checks to see if the given buffer has the magic cookie.

Checks the buffer to see if it is a valid VRPN header cookie. Returns -1 on total mismatch, 1 on minor version mismatch or other acceptable difference, and 0 on exact match.

Returns -1 on a mismatch, 0 on an exact match, 1 on a minor version difference.

Definition at line 2689 of file vrpn_Connection.C.

References vrpn_MAGIC, and vrpn_MAGICLEN.

Referenced by vrpn_Endpoint_IP::finish_new_connection_setup().

◆ check_vrpn_file_cookie()

int check_vrpn_file_cookie ( const char *  buffer)

Definition at line 2722 of file vrpn_Connection.C.

References vrpn_FILE_MAGIC, vrpn_MAGIC, and vrpn_MAGICLEN.

Referenced by vrpn_File_Connection::read_cookie().

◆ vrpn_cookie_size()

size_t vrpn_cookie_size ( void  )

Returns the size of the magic cookie buffer, plus any alignment overhead.

Definition at line 211 of file vrpn_Connection.C.

References vrpn_COOKIE_SIZE.

Referenced by vrpn_File_Connection::read_cookie(), vrpn_Log::saveLogSoFar(), vrpn_Log::setCookie(), vrpn_Endpoint_IP::setup_new_connection(), vrpn_Log::vrpn_Log(), and write_vrpn_cookie().

◆ vrpn_copy_file_name()

char * vrpn_copy_file_name ( const char *  filespecifier)

Utility routines to parse file specifiers FROM service locations.

file:<filename>

file://<hostname>/<filename>

file:///<filename>

Definition at line 6579 of file vrpn_Connection.C.

Referenced by vrpn_File_Connection::vrpn_File_Connection().

◆ vrpn_copy_machine_name()

char * vrpn_copy_machine_name ( const char *  hostspecifier)

Definition at line 6642 of file vrpn_Connection.C.

◆ vrpn_copy_rsh_arguments()

char * vrpn_copy_rsh_arguments ( const char *  hostspecifier)

Definition at line 6727 of file vrpn_Connection.C.

◆ vrpn_copy_rsh_program()

char * vrpn_copy_rsh_program ( const char *  hostspecifier)

Definition at line 6697 of file vrpn_Connection.C.

◆ vrpn_copy_service_location()

char * vrpn_copy_service_location ( const char *  fullname)

◆ vrpn_copy_service_name()

◆ vrpn_create_server_connection()

vrpn_Connection * vrpn_create_server_connection ( const char *  cname,
const char *  local_in_logfile_name = NULL,
const char *  local_out_logfile_name = NULL 
)

Create a server connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).

Returns NULL if the name is not understood or the connection cannot be created. WARNING: May not be thread safe. To create a VRPN TCP/UDP server, use a name like: vrpn:machine_name_or_ip:port machine_name_or_ip:port machine_name_or_ip :port (This port on any network card.) To create an MPI server, use a name like: mpi:MPI_COMM_WORLD mpi:comm_number When done with the object, call removeReference() on it (which will delete it if there are no other references).

Definition at line 5507 of file vrpn_Connection.C.

Referenced by vrpn_ConnectionPtr::create_server_connection(), vrpn_Forwarder_Server::start_remote_forwarding(), vrpn_Tracker_DeadReckoning_Rotation::test(), vrpn_create_server_connection(), and vrpn_PeerMutex::vrpn_PeerMutex().

◆ vrpn_get_connection_by_name()

vrpn_Connection * vrpn_get_connection_by_name ( const char *  cname,
const char *  local_in_logfile_name = NULL,
const char *  local_out_logfile_name = NULL,
const char *  remote_in_logfile_name = NULL,
const char *  remote_out_logfile_name = NULL,
const char *  NIC_IPaddress = NULL,
bool  force_reopen = false 
)

Create a client connection of arbitrary type (VRPN UDP/TCP, TCP, File, Loopback, MPI).

WARNING: May not be thread safe. If no IP address for the NIC to use is specified, uses the default NIC. If the force_reopen flag is set, a new connection will be made even if there was already one to that server. When done with the object, call removeReference() on it (which will delete it if there are no other references).

Definition at line 5417 of file vrpn_Connection.C.

Referenced by vrpn_PeerMutex::addPeer(), vrpn_Auxiliary_Logger_Server_Generic::handle_request_logging(), vrpn_Imager_Stream_Buffer::open_new_log_connection(), and vrpn_BaseClass::vrpn_BaseClass().

◆ vrpn_get_port_number()

int vrpn_get_port_number ( const char *  hostspecifier)

Definition at line 6677 of file vrpn_Connection.C.

References vrpn_DEFAULT_LISTEN_PORT_NO.

◆ vrpn_noint_block_read()

int vrpn_noint_block_read ( int  infile,
char  buffer[],
size_t  length 
)
 This routine will read in a block from the file descriptor.

It acts just like the read() routine does on normal files, so that it hides the fact that the descriptor may point to a socket. This will also take care of problems caused by interrupted system calls, retrying the read when they occur. This routine will either read the requested number of bytes and return that or return -1 (in the case of an error) or 0 (in the case of EOF being reached before all the data arrives).

Definition at line 1864 of file vrpn_Connection.C.

References vrpn_EINTR, and vrpn_socket_error.

Referenced by vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_Endpoint_IP::getOneTCPMessage(), and vrpn_3DConnexion::mainloop().

◆ vrpn_noint_block_read_timeout()

int vrpn_noint_block_read_timeout ( SOCKET  infile,
char  buffer[],
size_t  length,
struct timeval *  timeout 
)

This routine will read in a block from the file descriptor.

It acts just like the read() routine on normal files, except that it will time out if the read takes too long. This will also take care of problems caused by interrupted system calls, retrying the read when they occur. This routine will either read the requested number of bytes and return that or return -1 (in the case of an error) or 0 (in the case of EOF being reached before all the data arrives), or return the number of characters read before timeout (in the case of a timeout).

Definition at line 1961 of file vrpn_Connection.C.

References vrpn_EINTR, vrpn_gettimeofday, vrpn_noint_select(), vrpn_socket_error, vrpn_TimevalDiff(), vrpn_TimevalGreater(), and vrpn_TimevalSum().

Here is the call graph for this function:

◆ vrpn_noint_block_write()

int vrpn_noint_block_write ( int  outfile,
const char  buffer[],
size_t  length 
)
 This routine will write a block to a file descriptor.  It acts just

like the write() system call does on files, but it will keep sending to a socket until an error or all of the data has gone. This will also take care of problems caused by interrupted system calls, retrying the write when they occur. It will also work when sending large blocks of data through socket connections, since it will send all of the data before returning. This routine will either write the requested number of bytes and return that or return -1 (in the case of an error) or 0 (in the case of EOF being reached before all the data is sent).

Definition at line 1829 of file vrpn_Connection.C.

References vrpn_EINTR, and vrpn_socket_error.

Referenced by vrpn_Endpoint_IP::setup_new_connection().

◆ vrpn_noint_select()

int vrpn_noint_select ( int  width,
fd_set *  readfds,
fd_set *  writefds,
fd_set *  exceptfds,
struct timeval *  timeout 
)

This routine will perform like a normal select() call, but it will restart if it quit because of an interrupt.

This makes it more robust in its function, and allows this code to perform properly on pxpl5, which sends USER1 interrupts while rendering an image.

Definition at line 1723 of file vrpn_Connection.C.

References vrpn_EINTR, vrpn_gettimeofday, vrpn_socket_error, vrpn_TimevalGreater(), and vrpn_TimevalSum().

Referenced by vrpn_Endpoint_IP::handle_tcp_messages(), vrpn_Endpoint_IP::handle_udp_messages(), vrpn_3DConnexion::mainloop(), vrpn_Endpoint_IP::mainloop(), vrpn_Endpoint_IP::poll_for_cookie(), vrpn_Endpoint_IP::send_pending_reports(), vrpn_Connection_IP::server_check_for_incoming_connections(), and vrpn_noint_block_read_timeout().

Here is the call graph for this function:

◆ vrpn_set_service_name()

char * vrpn_set_service_name ( const char *  specifier,
const char *  newServiceName 
)

Utility routine to rename the service name of a given host specifier.

Definition at line 6759 of file vrpn_Connection.C.

References vrpn_copy_service_location().

Here is the call graph for this function:

◆ vrpn_udp_request_lob_packet()

int vrpn_udp_request_lob_packet ( SOCKET  udp_sock,
const char *  ,
const int  ,
const int  local_port,
const char *  NIC_IP = NULL 
)

This section deals with implementing a method of connection termed a UDP request.

This works by having the client open a TCP socket that it listens on. It then lobs datagrams to the server asking to be called back at the socket. This allows it to timeout on waiting for a connection request, resend datagrams in case some got lost, or give up at any time. The whole algorithm is implemented in the vrpn_udp_request_call() function; the functions before that are helper functions that have been broken out to allow a subset of the algorithm to be run by a connection whose server has dropped and they want to re-establish it.

This routine will lob a datagram to the given port on the given machine asking it to call back at the port on this machine that is also specified. It returns 0 on success and -1 on failure.

Definition at line 2360 of file vrpn_Connection.C.

References vrpn_closeSocket.

Referenced by vrpn_Endpoint_IP::mainloop().

◆ write_vrpn_cookie()

int write_vrpn_cookie ( char *  buffer,
size_t  length,
long  remote_log_mode 
)

Writes the magic cookie into buffer with given length.

On failure (if the buffer is too short), returns -1; otherwise returns 0.

This is exposed in vrpn_Connection.h so that we can write add_vrpn_cookie.

Definition at line 2674 of file vrpn_Connection.C.

References vrpn_cookie_size(), and vrpn_MAGIC.

Referenced by vrpn_Endpoint_IP::setup_new_connection(), and vrpn_Log::vrpn_Log().

Here is the call graph for this function:

Variable Documentation

◆ vrpn_CONTROL

const char* vrpn_CONTROL = "VRPN Control"

vrpn_CONTROL is the sender used for notification messages sent to the user from the local VRPN implementation (got_first_connection, etc.) and for control messages sent by auxiliary services. (Such as class vrpn_Controller, which will be introduced in a future revision.)

Definition at line 219 of file vrpn_Connection.C.

Referenced by vrpn_PeerMutex::addPeer(), vrpn_Endpoint_IP::drop_connection(), and vrpn_Endpoint_IP::finish_new_connection_setup().

◆ vrpn_COOKIE_SIZE

const size_t vrpn_COOKIE_SIZE = vrpn_MAGICLEN + vrpn_ALIGN

◆ vrpn_dropped_connection

const char* vrpn_dropped_connection = "VRPN_Connection_Dropped_Connection"

◆ vrpn_dropped_last_connection

◆ vrpn_FILE_MAGIC

const char* vrpn_FILE_MAGIC = (const char *)"vrpn: ver. 04.00"

Definition at line 205 of file vrpn_Connection.C.

Referenced by check_vrpn_file_cookie().

◆ vrpn_got_connection

◆ vrpn_got_first_connection

const char* vrpn_got_first_connection = "VRPN_Connection_Got_First_Connection"

◆ vrpn_MAGIC

const char* vrpn_MAGIC = (const char *)"vrpn: ver. 07.35"

Definition at line 204 of file vrpn_Connection.C.

Referenced by check_vrpn_cookie(), check_vrpn_file_cookie(), and write_vrpn_cookie().

◆ vrpn_MAGICLEN

const int vrpn_MAGICLEN = 16