Now you can download a copy of these docs so you can use them offline! Download now
Reader.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2011. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "NetworkTables/Reader.h" 00008 00009 #include "NetworkTables/BooleanEntry.h" 00010 #include "NetworkTables/Connection.h" 00011 #include "NetworkTables/DoubleEntry.h" 00012 #include "NetworkTables/IntegerEntry.h" 00013 #include "NetworkTables/StringEntry.h" 00014 #include "WPIErrors.h" 00015 #include <errnoLib.h> 00016 #include <selectLib.h> 00017 #include <sockLib.h> 00018 #include <usrLib.h> 00019 00020 namespace NetworkTables 00021 { 00022 00023 Reader::Reader(Connection *connection, int inputStreamFd) : 00024 m_connection(connection), 00025 m_inputStreamFd(inputStreamFd) 00026 { 00027 m_lastByte = -2; 00028 } 00029 00030 int Reader::Read() 00031 { 00032 fd_set readFdSet; 00033 int retval = ERROR; 00034 00035 FD_ZERO(&readFdSet); 00036 FD_SET(m_inputStreamFd, &readFdSet); 00037 if (select(FD_SETSIZE, &readFdSet, NULL, NULL, NULL) != ERROR) 00038 { 00039 if (FD_ISSET(m_inputStreamFd, &readFdSet)) 00040 { 00041 char readbuf; 00042 retval = recv(m_inputStreamFd, &readbuf, 1, 0); 00043 m_lastByte = readbuf; 00044 if (retval != ERROR && retval > 0) 00045 { 00046 #ifdef DEBUG 00047 if (m_lastByte != kNetworkTables_PING) 00048 { 00049 char pbuf[6]; 00050 snprintf(pbuf, 6, "I:%02X\n", m_lastByte); 00051 printf(pbuf); 00052 } 00053 #endif 00054 return m_lastByte; 00055 } 00056 } 00057 } 00058 else if (!m_connection->IsConnected()) 00059 { 00060 // The error came from us closing the socket 00061 return 0; 00062 } 00063 00064 // TODO: Should we ignore ECONNRESET errors? 00065 if (retval == ERROR) 00066 { 00067 char buf[32] = ""; 00068 int err = errnoGet(); 00069 snprintf(buf, 32, "errno=%d", err); 00070 wpi_setStaticWPIErrorWithContext(m_connection, NetworkTablesReadError, buf); 00071 printErrno(err); 00072 } 00073 m_connection->Close(); 00074 return 0; 00075 } 00076 00077 int Reader::Check(bool useLastValue) 00078 { 00079 return useLastValue ? m_lastByte : Read(); 00080 } 00081 00082 std::string Reader::ReadString() 00083 { 00084 Read(); 00085 std::string buffer; 00086 00087 if (m_lastByte == kNetworkTables_BEGIN_STRING) 00088 { 00089 buffer.reserve(360); 00090 buffer.clear(); 00091 while (Read() != kNetworkTables_END_STRING) 00092 buffer.append(1, (char)m_lastByte); 00093 } 00094 else 00095 { 00096 int length = m_lastByte; 00097 buffer.reserve(length + 1); 00098 buffer.clear(); 00099 for (int i = 0; i < length; i++) 00100 buffer[i] = (char)Read(); 00101 buffer[length] = '\0'; 00102 } 00103 00104 return buffer; 00105 } 00106 00107 int Reader::ReadId(bool useLastValue) 00108 { 00109 return ReadVariableSize(useLastValue, kNetworkTables_ID); 00110 } 00111 00112 int Reader::ReadTableId(bool useLastValue) { 00113 return ReadVariableSize(useLastValue, kNetworkTables_TABLE_ID); 00114 } 00115 00116 int Reader::ReadVariableSize(bool useLastValue, int tag) { 00117 int value = Check(useLastValue); 00118 value ^= tag; 00119 if (value < tag - 4) 00120 { 00121 return value; 00122 } 00123 else 00124 { 00125 int bytes = (value & 3) + 1; 00126 int id = 0; 00127 for (int i = 0; i < bytes; i++) 00128 id = (id << 8) | Read(); 00129 return id; 00130 } 00131 } 00132 00133 int Reader::ReadInt() 00134 { 00135 return (Read() << 24) | (Read() << 16) | (Read() << 8) | Read(); 00136 } 00137 00138 double Reader::ReadDouble() 00139 { 00140 long l = Read(); 00141 l = (l << 8) | Read(); 00142 l = (l << 8) | Read(); 00143 l = (l << 8) | Read(); 00144 l = (l << 8) | Read(); 00145 l = (l << 8) | Read(); 00146 l = (l << 8) | Read(); 00147 l = (l << 8) | Read(); 00148 return *((double *)&l); 00149 } 00150 00151 int Reader::ReadConfirmations(bool useLastValue) 00152 { 00153 return Check(useLastValue) ^ kNetworkTables_CONFIRMATION; 00154 } 00155 00156 int Reader::ReadDenials(bool useLastValue) 00157 { 00158 return Check(useLastValue) ^ kNetworkTables_DENIAL; 00159 } 00160 00161 std::auto_ptr<Entry> Reader::ReadEntry(bool useLastValue) 00162 { 00163 switch (Check(useLastValue)) 00164 { 00165 case kNetworkTables_BOOLEAN_FALSE: 00166 return std::auto_ptr<Entry>(new BooleanEntry(false)); 00167 case kNetworkTables_BOOLEAN_TRUE: 00168 return std::auto_ptr<Entry>(new BooleanEntry(true)); 00169 case kNetworkTables_INT: 00170 return std::auto_ptr<Entry>(new IntegerEntry(ReadInt())); 00171 case kNetworkTables_DOUBLE: 00172 return std::auto_ptr<Entry>(new DoubleEntry(ReadDouble())); 00173 case kNetworkTables_STRING: 00174 return std::auto_ptr<Entry>(new StringEntry(ReadString().c_str())); 00175 default: 00176 return std::auto_ptr<Entry>(NULL); 00177 } 00178 } 00179 00180 } // namespace
Generated on Thu Jan 12 2012 22:35:23 for WPILibC++ by
1.7.1