mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Initial checkin of unified hierarchy of WPILib 2015
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* FDIOStream.cpp
|
||||
*
|
||||
* Created on: Sep 27, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
#include "networktables2/stream/FDIOStream.h"
|
||||
#include "networktables2/util/IOException.h"
|
||||
#include "networktables2/util/EOFException.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef _WRS_KERNEL
|
||||
#include <iolib.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
FDIOStream::FDIOStream(int _fd){
|
||||
fd = _fd;
|
||||
// f = fdopen(_fd, "rbwb");
|
||||
// if(f==NULL)
|
||||
// throw IOException("Could not open stream from file descriptor", errno);
|
||||
}
|
||||
FDIOStream::~FDIOStream(){
|
||||
close();
|
||||
}
|
||||
|
||||
int FDIOStream::read(void* ptr, int numbytes){
|
||||
if(numbytes==0)
|
||||
return 0;
|
||||
char* bufferPointer = (char*)ptr;
|
||||
int totalRead = 0;
|
||||
|
||||
struct timeval timeout;
|
||||
fd_set fdSet;
|
||||
|
||||
while (totalRead < numbytes) {
|
||||
FD_ZERO(&fdSet);
|
||||
FD_SET(fd, &fdSet);
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
int select_result = select(FD_SETSIZE, &fdSet, NULL, NULL, &timeout);
|
||||
if ( select_result < 0)
|
||||
throw IOException("Select returned an error on read");
|
||||
|
||||
int numRead = 0;
|
||||
if (FD_ISSET(fd, &fdSet)) {
|
||||
numRead = ::read(fd, bufferPointer, numbytes-totalRead);
|
||||
|
||||
if(numRead == 0){
|
||||
throw EOFException();
|
||||
}
|
||||
else if (numRead < 0) {
|
||||
perror("read error: ");
|
||||
fflush(stderr);
|
||||
throw IOException("Error on FDIO read");
|
||||
}
|
||||
bufferPointer += numRead;
|
||||
totalRead += numRead;
|
||||
}
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
int FDIOStream::write(const void* ptr, int numbytes){
|
||||
int numWrote = ::write(fd, (char*)ptr, numbytes);//TODO: this is bad
|
||||
//int numWrote = fwrite(ptr, 1, numbytes, f);
|
||||
if(numWrote==numbytes)
|
||||
return numWrote;
|
||||
perror("write error: ");
|
||||
fflush(stderr);
|
||||
throw IOException("Could not write all bytes to fd stream");
|
||||
|
||||
}
|
||||
void FDIOStream::flush(){
|
||||
//if(fflush(f)==EOF)
|
||||
// throw EOFException();
|
||||
}
|
||||
void FDIOStream::close(){
|
||||
//fclose(f);//ignore any errors closing
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* SocketServerStreamProvider.cpp
|
||||
*
|
||||
* Created on: Sep 27, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
#include "networktables2/stream/SocketServerStreamProvider.h"
|
||||
#include "networktables2/stream/FDIOStream.h"
|
||||
#include "networktables2/util/IOException.h"
|
||||
|
||||
#include <strings.h>
|
||||
#include <cstring>
|
||||
#include <errno.h>
|
||||
#ifdef _WRS_KERNEL
|
||||
#include <inetLib.h>
|
||||
#include <selectLib.h>
|
||||
#include <sockLib.h>
|
||||
#include <taskLib.h>
|
||||
#include <usrLib.h>
|
||||
#include <ioLib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <winsock.h>
|
||||
#include <winsock2.h>
|
||||
#include <wininet.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef _WRS_KERNEL
|
||||
#define ERROR -1
|
||||
#endif
|
||||
|
||||
#if defined(WIN32) || defined(_WRS_KERNEL)
|
||||
typedef int addrlen_t;
|
||||
#else
|
||||
typedef socklen_t addrlen_t;
|
||||
#endif
|
||||
|
||||
|
||||
SocketServerStreamProvider::SocketServerStreamProvider(int port){
|
||||
struct sockaddr_in serverAddr;
|
||||
int sockAddrSize = sizeof(serverAddr);
|
||||
memset(&serverAddr, 0, sockAddrSize);
|
||||
|
||||
#ifdef _WRS_KERNEL
|
||||
serverAddr.sin_len = (u_char)sockAddrSize;
|
||||
#endif
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_port = htons(port);
|
||||
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
|
||||
{
|
||||
throw IOException("Error creating server socket", errno);
|
||||
}
|
||||
|
||||
// Set the TCP socket so that it can be reused if it is in the wait state.
|
||||
int reuseAddr = 1;
|
||||
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseAddr, sizeof(reuseAddr));
|
||||
|
||||
// Bind socket to local address.
|
||||
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sockAddrSize) == ERROR)
|
||||
{
|
||||
::close(serverSocket);
|
||||
throw IOException("Could not bind server socket", errno);
|
||||
}
|
||||
|
||||
if (listen(serverSocket, 1) == ERROR)
|
||||
{
|
||||
::close(serverSocket);
|
||||
throw IOException("Could not listen on server socket", errno);
|
||||
}
|
||||
}
|
||||
SocketServerStreamProvider::~SocketServerStreamProvider(){
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
IOStream* SocketServerStreamProvider::accept(){
|
||||
struct timeval timeout;
|
||||
// Check for a shutdown once per second
|
||||
while (true)
|
||||
{
|
||||
fd_set fdSet;
|
||||
|
||||
FD_ZERO(&fdSet);
|
||||
FD_SET(serverSocket, &fdSet);
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
int select_result = select(FD_SETSIZE, &fdSet, NULL, NULL, &timeout);
|
||||
if ( select_result < 0)
|
||||
return NULL;
|
||||
|
||||
if (FD_ISSET(serverSocket, &fdSet))
|
||||
{
|
||||
struct sockaddr clientAddr;
|
||||
memset(&clientAddr, 0, sizeof(struct sockaddr));
|
||||
addrlen_t clientAddrSize = sizeof(clientAddr);
|
||||
int connectedSocket = ::accept(serverSocket, &clientAddr, &clientAddrSize);
|
||||
if (connectedSocket == ERROR)
|
||||
return NULL;
|
||||
|
||||
return new FDIOStream(connectedSocket);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void SocketServerStreamProvider::close(){
|
||||
::close(serverSocket);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* SocketStreamFactory.cpp
|
||||
*
|
||||
* Created on: Nov 3, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
|
||||
#include <cstring>
|
||||
#ifdef _WRS_KERNEL
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#ifdef WIN32
|
||||
#include <winsock.h>
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#endif
|
||||
#endif
|
||||
#include "networktables2/stream/FDIOStream.h"
|
||||
#include "networktables2/stream/SocketStreamFactory.h"
|
||||
|
||||
|
||||
SocketStreamFactory::SocketStreamFactory(const char* _host, int _port):host(_host), port(_port){}
|
||||
|
||||
SocketStreamFactory::~SocketStreamFactory(){}
|
||||
|
||||
IOStream* SocketStreamFactory::createStream(){
|
||||
#ifdef _WRS_KERNEL
|
||||
//crio client not supported
|
||||
return NULL;
|
||||
#else
|
||||
struct sockaddr_in serv_addr;
|
||||
struct hostent *server;
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0){
|
||||
//error("ERROR opening socket");
|
||||
return NULL;
|
||||
}
|
||||
server = gethostbyname(host);
|
||||
if (server == NULL) {
|
||||
//fprintf(stderr,"ERROR, no such host\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
|
||||
serv_addr.sin_port = htons(port);
|
||||
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
|
||||
//error("ERROR connecting");
|
||||
return NULL;
|
||||
}//TODO close fd if an error occured
|
||||
|
||||
//int on = 1;
|
||||
//setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
|
||||
|
||||
return new FDIOStream(sockfd);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* SocketStreams.cpp
|
||||
*
|
||||
* Created on: Sep 27, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
#include "networktables2/stream/SocketStreams.h"
|
||||
#include "networktables2/stream/SocketStreamFactory.h"
|
||||
#include "networktables2/stream/SocketServerStreamProvider.h"
|
||||
|
||||
|
||||
|
||||
IOStreamFactory& SocketStreams::newStreamFactory(const char* host, int port){
|
||||
return *new SocketStreamFactory(host, port);
|
||||
}
|
||||
|
||||
IOStreamProvider& SocketStreams::newStreamProvider(int port){
|
||||
return *new SocketServerStreamProvider(port);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* DefaultThreadManager.cpp
|
||||
* Desktop
|
||||
* Author: Mitchell Wills
|
||||
*
|
||||
*/
|
||||
|
||||
#include "networktables2/thread/DefaultThreadManager.h"
|
||||
#include <exception>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
PeriodicNTThread::PeriodicNTThread(PeriodicRunnable* _r, const char* name) : r(_r), run(true){
|
||||
if(pthread_create(&thread, NULL, (void* (*)(void*))PeriodicNTThread::taskMain, (void*)this))
|
||||
throw std::exception();
|
||||
}
|
||||
PeriodicNTThread::~PeriodicNTThread(){
|
||||
stop();
|
||||
if(thread == pthread_self()){
|
||||
fprintf(stderr, "WARNING: thread destructor called from this thread\n");
|
||||
}
|
||||
pthread_join(thread, NULL);
|
||||
}
|
||||
void* PeriodicNTThread::taskMain(PeriodicNTThread* o){//static wrapper
|
||||
o->_taskMain();
|
||||
return 0;
|
||||
}
|
||||
void PeriodicNTThread::_taskMain(){
|
||||
while(run){
|
||||
r->run();
|
||||
}
|
||||
}
|
||||
void PeriodicNTThread::stop() {
|
||||
run = false;
|
||||
//pthread_cancel(thread);
|
||||
}
|
||||
bool PeriodicNTThread::isRunning() {
|
||||
return pthread_kill(thread, 0) == 0;
|
||||
}
|
||||
|
||||
NTThread* DefaultThreadManager::newBlockingPeriodicThread(PeriodicRunnable* r, const char* name) {
|
||||
return new PeriodicNTThread(r, name);
|
||||
}
|
||||
Reference in New Issue
Block a user