vrpn
07.33
Virtual Reality Peripheral Network
|
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <cctype>
#include <stdlib.h>
#include "vrpn_Connection.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 | SOCK_CAST |
#define | GSN_CAST |
#define | INVALID_SOCKET -1 |
#define | RSH (char *) "/usr/local/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 | |
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. More... | |
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. More... | |
int | vrpn_udp_request_lob_packet (SOCKET udp_sock, const char *machine, const int remote_port, const int local_port, const char *NIC_IP=NULL) |
This section deals with implementing a method of connection termed a UDP request. More... | |
int | write_vrpn_cookie (char *buffer, size_t length, long remote_log_mode) |
Writes the magic cookie into buffer with given length. More... | |
int | check_vrpn_cookie (const char *buffer) |
Checks to see if the given buffer has the magic cookie. More... | |
int | check_vrpn_file_cookie (const char *buffer) |
size_t | vrpn_cookie_size (void) |
Returns the size of the magic cookie buffer, plus any alignment overhead. More... | |
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). More... | |
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). More... | |
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. More... | |
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. More... | |
Variables | |
const char * | vrpn_MAGIC = (const char *)"vrpn: ver. 07.33" |
const char * | vrpn_FILE_MAGIC = (const char *)"vrpn: ver. 04.00" |
const int | vrpn_MAGICLEN = 16 |
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. More... | |
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.) More... | |
#define GSN_CAST |
Definition at line 89 of file vrpn_Connection.C.
#define INVALID_SOCKET -1 |
Definition at line 112 of file vrpn_Connection.C.
#define RSH (char *) "/usr/local/bin/rsh" |
Definition at line 227 of file vrpn_Connection.C.
#define SERVCOUNT (20) |
Definition at line 239 of file vrpn_Connection.C.
#define SERVWAIT (120 / SERVCOUNT) |
Definition at line 240 of file vrpn_Connection.C.
#define SOCK_CAST |
Definition at line 79 of file vrpn_Connection.C.
#define UDP_CALL_RETRIES (5) |
Definition at line 233 of file vrpn_Connection.C.
#define UDP_CALL_TIMEOUT (2) |
Definition at line 232 of file vrpn_Connection.C.
#define vrpn_closeSocket close |
Definition at line 35 of file vrpn_Connection.C.
#define vrpn_CONNECTION_MAX_XLATION_TABLE_SIZE 2000 |
Definition at line 245 of file vrpn_Connection.C.
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 2557 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 2590 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 2630 of file vrpn_Connection.C.
References vrpn_ALIGN, and vrpn_MAGICLEN.
Referenced by vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_File_Connection::read_cookie(), vrpn_Log::saveLogSoFar(), vrpn_Log::setCookie(), vrpn_Endpoint_IP::setup_new_connection(), and vrpn_Log::vrpn_Log().
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 6372 of file vrpn_Connection.C.
Referenced by vrpn_File_Connection::vrpn_File_Connection().
char* vrpn_copy_machine_name | ( | const char * | hostspecifier | ) |
Definition at line 6434 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::vrpn_Connection_IP(), and vrpn_create_server_connection().
char* vrpn_copy_rsh_arguments | ( | const char * | hostspecifier | ) |
Definition at line 6509 of file vrpn_Connection.C.
char* vrpn_copy_rsh_program | ( | const char * | hostspecifier | ) |
Definition at line 6484 of file vrpn_Connection.C.
char* vrpn_copy_service_location | ( | const char * | fullname | ) |
Definition at line 6353 of file vrpn_Connection.C.
Referenced by vrpn_Connection_IP::server_check_for_incoming_connections(), vrpn_create_server_connection(), and vrpn_set_service_name().
char* vrpn_copy_service_name | ( | const char * | fullname | ) |
Definition at line 6335 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 5358 of file vrpn_Connection.C.
References vrpn_Connection::addReference(), vrpn_Connection::setAutoDeleteStatus(), vrpn_copy_machine_name(), vrpn_copy_service_location(), vrpn_DEFAULT_LISTEN_PORT_NO, and vrpn_get_port_number().
Referenced by vrpn_ConnectionPtr::create_server_connection(), vrpn_Forwarder_Server::start_remote_forwarding(), and vrpn_create_server_connection().
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 5277 of file vrpn_Connection.C.
References vrpn_Connection::addReference(), vrpn_ConnectionManager::getByName(), vrpn_ConnectionManager::instance(), vrpn_Connection::setAutoDeleteStatus(), vrpn_File_Connection, and vrpn_get_port_number().
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().
int vrpn_get_port_number | ( | const char * | hostspecifier | ) |
Definition at line 6464 of file vrpn_Connection.C.
References vrpn_DEFAULT_LISTEN_PORT_NO.
Referenced by vrpn_create_server_connection(), and 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 1757 of file vrpn_Connection.C.
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 1854 of file vrpn_Connection.C.
References vrpn_gettimeofday, vrpn_noint_select(), 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 1721 of file vrpn_Connection.C.
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 1615 of file vrpn_Connection.C.
References vrpn_gettimeofday, 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 6541 of file vrpn_Connection.C.
References vrpn_copy_service_location().
int vrpn_udp_request_lob_packet | ( | SOCKET | udp_sock, |
const char * | machine, | ||
const int | remote_port, | ||
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 2246 of file vrpn_Connection.C.
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 2542 of file vrpn_Connection.C.
References vrpn_ALIGN, vrpn_MAGIC, and vrpn_MAGICLEN.
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 190 of file vrpn_Connection.C.
Referenced by vrpn_PeerMutex::addPeer(), vrpn_Endpoint_IP::drop_connection(), and vrpn_Connection::init().
const char* vrpn_dropped_connection = "VRPN_Connection_Dropped_Connection" |
Definition at line 186 of file vrpn_Connection.C.
Referenced by vrpn_PeerMutex::addPeer(), vrpn_BaseClassUnique::client_mainloop(), vrpn_Endpoint_IP::drop_connection(), vrpn_Connection::init(), and vrpn_Imager_Remote::vrpn_Imager_Remote().
const char* vrpn_dropped_last_connection |
Definition at line 187 of file vrpn_Connection.C.
Referenced by vrpn_Endpoint_IP::drop_connection(), vrpn_Connection::init(), vrpn_Retrolink::init_hid(), vrpn_Logitech_Controller_Raw::init_hid(), vrpn_CHProducts_Controller_Raw::init_hid(), vrpn_Saitek_Controller_Raw::init_hid(), vrpn_Griffin::init_hid(), vrpn_Contour::init_hid(), vrpn_Futaba::init_hid(), vrpn_Xkeys::init_hid(), vrpn_Microsoft_Controller_Raw::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 181 of file vrpn_Connection.C.
Referenced by check_vrpn_file_cookie().
const char* vrpn_got_connection = "VRPN_Connection_Got_Connection" |
Definition at line 185 of file vrpn_Connection.C.
Referenced by vrpn_Connection::init(), vrpn_Retrolink::init_hid(), vrpn_Logitech_Controller_Raw::init_hid(), vrpn_Saitek_Controller_Raw::init_hid(), vrpn_CHProducts_Controller_Raw::init_hid(), vrpn_Contour::init_hid(), vrpn_Futaba::init_hid(), vrpn_Griffin::init_hid(), vrpn_Xkeys::init_hid(), vrpn_Microsoft_Controller_Raw::init_hid(), vrpn_Analog_Output::register_types(), vrpn_FunctionGenerator::register_types(), 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 184 of file vrpn_Connection.C.
Referenced by vrpn_Connection::init(), vrpn_GlobalHapticsOrb::vrpn_GlobalHapticsOrb(), vrpn_Imager_Stream_Buffer::vrpn_Imager_Stream_Buffer(), vrpn_raw_SGIBox::vrpn_raw_SGIBox(), vrpn_Tracker_AnalogFly::vrpn_Tracker_AnalogFly(), and vrpn_Tracker_ButtonFly::vrpn_Tracker_ButtonFly().
const char* vrpn_MAGIC = (const char *)"vrpn: ver. 07.33" |
Definition at line 180 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 182 of file vrpn_Connection.C.
Referenced by check_vrpn_cookie(), check_vrpn_file_cookie(), vrpn_Endpoint_IP::finish_new_connection_setup(), vrpn_cookie_size(), and write_vrpn_cookie().