#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"
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) |
#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_Connection * | vrpn_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_Connection * | vrpn_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.) | |
#define GSN_CAST |
Definition at line 113 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::server_check_for_incoming_connections().
#define INVALID_SOCKET -1 |
Definition at line 136 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::drop_connection(), vrpn_Endpoint_IP::drop_connection(), vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_Endpoint_IP::init(), vrpn_Connection_IP::vrpn_Connection_IP(), vrpn_Connection_IP::vrpn_Connection_IP(), vrpn_Endpoint_IP::vrpn_Endpoint_IP(), vrpn_Connection_IP::~vrpn_Connection_IP(), and vrpn_Endpoint_IP::~vrpn_Endpoint_IP().
#define RSH "/usr/bin/rsh" |
Definition at line 240 of file vrpn_Connection.C.
#define SERVCOUNT (20) |
Definition at line 252 of file vrpn_Connection.C.
#define SERVWAIT (120 / SERVCOUNT) |
Definition at line 253 of file vrpn_Connection.C.
#define SOCK_CAST |
Definition at line 103 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::connect_tcp_to().
#define UDP_CALL_RETRIES (5) |
Definition at line 246 of file vrpn_Connection.C.
#define UDP_CALL_TIMEOUT (2) |
Definition at line 245 of file vrpn_Connection.C.
#define vrpn_closeSocket close |
Definition at line 57 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::connect_tcp_to(), vrpn_Endpoint_IP::drop_connection(), vrpn_Endpoint_IP::mainloop(), vrpn_udp_request_lob_packet(), vrpn_Connection_IP::~vrpn_Connection_IP(), and vrpn_Endpoint_IP::~vrpn_Endpoint_IP().
#define vrpn_CONNECTION_MAX_XLATION_TABLE_SIZE 2000 |
Definition at line 258 of file vrpn_Connection.C.
#define vrpn_EINTR EINTR |
Definition at line 61 of file vrpn_Connection.C.
Referenced by vrpn_noint_block_read(), vrpn_noint_block_read_timeout(), vrpn_noint_block_write(), and vrpn_noint_select().
#define vrpn_socket_error errno |
Definition at line 59 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::mainloop(), vrpn_Endpoint_IP::send_pending_reports(), vrpn_noint_block_read(), vrpn_noint_block_read_timeout(), vrpn_noint_block_write(), and vrpn_noint_select().
#define vrpn_socket_error_to_chars | ( | x | ) |
Definition at line 60 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::mainloop(), and vrpn_Endpoint_IP::send_pending_reports().
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().
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().
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().
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().
char * vrpn_copy_machine_name | ( | const char * | hostspecifier | ) |
Definition at line 6642 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::vrpn_Connection_IP(), and vrpn_Connection_IP::vrpn_create_server_connection.
char * vrpn_copy_rsh_arguments | ( | const char * | hostspecifier | ) |
Definition at line 6727 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::vrpn_Connection_IP().
char * vrpn_copy_rsh_program | ( | const char * | hostspecifier | ) |
Definition at line 6697 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::vrpn_Connection_IP().
char * vrpn_copy_service_location | ( | const char * | fullname | ) |
Definition at line 6558 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::server_check_for_incoming_connections(), vrpn_Connection_IP::vrpn_create_server_connection, and vrpn_set_service_name().
char * vrpn_copy_service_name | ( | const char * | fullname | ) |
Definition at line 6534 of file vrpn_Connection.C.
Referenced by vrpn_Imager_Stream_Buffer::setup_handlers_for_logging_connection(), vrpn_Imager_Stream_Buffer::teardown_handlers_for_logging_connection(), vrpn_BaseClass::vrpn_BaseClass(), and vrpn_Mutex::vrpn_Mutex().
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_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_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(), vrpn_BaseClass::vrpn_BaseClass(), and vrpn_Mutex_Remote::vrpn_Mutex_Remote().
int vrpn_get_port_number | ( | const char * | hostspecifier | ) |
Definition at line 6677 of file vrpn_Connection.C.
References vrpn_DEFAULT_LISTEN_PORT_NO.
Referenced by vrpn_Connection_IP::vrpn_create_server_connection, and vrpn_Connection_IP::vrpn_get_connection_by_name.
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().
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 SOCKET, vrpn_EINTR, vrpn_gettimeofday, vrpn_noint_select(), vrpn_socket_error, vrpn_TimevalDiff(), vrpn_TimevalGreater(), and vrpn_TimevalSum().
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().
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().
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().
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 SOCKET, and vrpn_closeSocket.
Referenced by vrpn_Endpoint_IP::mainloop(), and vrpn_Connection_IP::vrpn_Connection_IP().
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().
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().
const size_t vrpn_COOKIE_SIZE = vrpn_MAGICLEN + vrpn_ALIGN |
Definition at line 210 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::finish_new_connection_setup(), and vrpn_cookie_size().
const char* vrpn_dropped_connection = "VRPN_Connection_Dropped_Connection" |
Definition at line 215 of file vrpn_Connection.C.
Referenced by vrpn_PeerMutex::addPeer(), vrpn_BaseClassUnique::client_mainloop(), vrpn_Endpoint_IP::drop_connection(), and vrpn_Imager_Remote::vrpn_Imager_Remote().
const char* vrpn_dropped_last_connection |
Definition at line 216 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::drop_connection(), vrpn_CHProducts_Controller_Raw::init_hid(), vrpn_Contour::init_hid(), vrpn_Futaba::init_hid(), vrpn_Griffin::init_hid(), vrpn_Logitech_Controller_Raw::init_hid(), vrpn_Microsoft_Controller_Raw::init_hid(), vrpn_Retrolink::init_hid(), vrpn_Saitek_Controller_Raw::init_hid(), vrpn_Vality::init_hid(), vrpn_Xkeys::init_hid(), vrpn_Auxiliary_Logger_Server::vrpn_Auxiliary_Logger_Server(), vrpn_Imager_Server::vrpn_Imager_Server(), vrpn_Mutex_Server::vrpn_Mutex_Server(), vrpn_WiiMote::vrpn_WiiMote(), and vrpn_Mutex_Server::~vrpn_Mutex_Server().
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().
const char* vrpn_got_connection = "VRPN_Connection_Got_Connection" |
Definition at line 214 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_CHProducts_Controller_Raw::init_hid(), vrpn_Contour::init_hid(), vrpn_Futaba::init_hid(), vrpn_Griffin::init_hid(), vrpn_Logitech_Controller_Raw::init_hid(), vrpn_Microsoft_Controller_Raw::init_hid(), vrpn_Retrolink::init_hid(), vrpn_Saitek_Controller_Raw::init_hid(), vrpn_Vality::init_hid(), vrpn_Xkeys::init_hid(), vrpn_Analog_Output::register_types(), vrpn_FunctionGenerator::register_types(), vrpn_Button_Filter::vrpn_Button_Filter(), vrpn_Imager_Server::vrpn_Imager_Server(), vrpn_ImagerPose_Server::vrpn_ImagerPose_Server(), vrpn_Mutex_Remote::vrpn_Mutex_Remote(), vrpn_Mutex_Server::vrpn_Mutex_Server(), vrpn_Tracker_JoyFly::vrpn_Tracker_JoyFly(), vrpn_Tracker_WiimoteHead::vrpn_Tracker_WiimoteHead(), vrpn_Mutex_Remote::~vrpn_Mutex_Remote(), vrpn_Mutex_Server::~vrpn_Mutex_Server(), and vrpn_SharedObject::~vrpn_SharedObject().
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.
Definition at line 213 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_GlobalHapticsOrb::vrpn_GlobalHapticsOrb(), vrpn_Imager_Stream_Buffer::vrpn_Imager_Stream_Buffer(), vrpn_raw_SGIBox::vrpn_raw_SGIBox(), and vrpn_Tracker_AnalogFly::vrpn_Tracker_AnalogFly().
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().
const int vrpn_MAGICLEN = 16 |
Definition at line 206 of file vrpn_Connection.C.
Referenced by check_vrpn_cookie(), check_vrpn_file_cookie(), and vrpn_Endpoint_IP::finish_new_connection_setup().