Now you can download a copy of these docs so you can use them offline! Download now
FDIOStream.cpp
1 /*
2  * FDIOStream.cpp
3  *
4  * Created on: Sep 27, 2012
5  * Author: Mitchell Wills
6  */
7 
8 #include "networktables2/stream/FDIOStream.h"
9 #include "networktables2/util/IOException.h"
10 #include "networktables2/util/EOFException.h"
11 
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <iolib.h>
15 #include <selectLib.h>
16 #include <string.h>
17 #include <stdio.h>
18 
19 
20 FDIOStream::FDIOStream(int _fd){
21  fd = _fd;
22  // f = fdopen(_fd, "rbwb");
23  // if(f==NULL)
24  // throw IOException("Could not open stream from file descriptor", errno);
25 }
26 FDIOStream::~FDIOStream(){
27  close();
28 }
29 
30 int FDIOStream::read(void* ptr, int numbytes){
31  if(numbytes==0)
32  return 0;
33  char* bufferPointer = (char*)ptr;
34  int totalRead = 0;
35 
36  struct timeval timeout;
37  fd_set fdSet;
38 
39  while (totalRead < numbytes) {
40  FD_ZERO(&fdSet);
41  FD_SET(fd, &fdSet);
42  timeout.tv_sec = 1;
43  timeout.tv_usec = 0;
44  int select_result = select(FD_SETSIZE, &fdSet, NULL, NULL, &timeout);
45  if ( select_result < 0)
46  throw IOException("Select returned an error on read");
47 
48  int numRead = 0;
49  if (FD_ISSET(fd, &fdSet)) {
50  numRead = ::read(fd, bufferPointer, numbytes-totalRead);
51 
52  if(numRead == 0){
53  throw EOFException();
54  }
55  else if (numRead < 0) {
56  perror("read error: ");
57  fflush(stderr);
58  throw IOException("Error on FDIO read");
59  }
60  bufferPointer += numRead;
61  totalRead += numRead;
62  }
63  }
64  return totalRead;
65 }
66 int FDIOStream::write(const void* ptr, int numbytes){
67  int numWrote = ::write(fd, (char*)ptr, numbytes);//TODO: this is bad
68  //int numWrote = fwrite(ptr, 1, numbytes, f);
69  if(numWrote==numbytes)
70  return numWrote;
71  perror("write error: ");
72  fflush(stderr);
73  throw IOException("Could not write all bytes to fd stream");
74 
75 }
76 void FDIOStream::flush(){
77  //if(fflush(f)==EOF)
78  // throw EOFException();
79 }
80 void FDIOStream::close(){
81  //fclose(f);//ignore any errors closing
82  ::close(fd);
83 }
84 

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6