Now you can download a copy of these docs so you can use them offline! Download now
Buffer.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/Buffer.h" 00008 #include "NetworkTables/InterfaceConstants.h" 00009 #include "WPIErrors.h" 00010 #include <sockLib.h> 00011 #include <string.h> 00012 00013 namespace NetworkTables 00014 { 00015 00016 Buffer::Buffer(UINT32 capacity) : 00017 m_buffer (NULL), 00018 m_size (0), 00019 m_capacity (capacity) 00020 { 00021 m_buffer = new UINT8[capacity]; 00022 } 00023 00024 Buffer::~Buffer() 00025 { 00026 delete [] m_buffer; 00027 } 00028 00029 void Buffer::WriteString(UINT32 length, const char *entry) 00030 { 00031 if (length >= kNetworkTables_BEGIN_STRING) 00032 { 00033 WriteByte(kNetworkTables_BEGIN_STRING); 00034 WriteBytes(length, (UINT8*)entry); 00035 WriteByte(kNetworkTables_END_STRING); 00036 } 00037 else 00038 { 00039 WriteByte(length); 00040 WriteBytes(length, (UINT8*)entry); 00041 } 00042 } 00043 00044 void Buffer::WriteString(const char *entry) 00045 { 00046 WriteString(strlen(entry), entry); 00047 } 00048 00049 void Buffer::WriteString(std::string entry) 00050 { 00051 WriteString(entry.length(), entry.c_str()); 00052 } 00053 00054 void Buffer::WriteDouble(double entry) 00055 { 00056 WriteBytes(sizeof(entry), (UINT8*) &entry); 00057 } 00058 00059 void Buffer::WriteInt(UINT32 entry) 00060 { 00061 WriteBytes(sizeof(entry), (UINT8*) &entry); 00062 } 00063 00064 void Buffer::WriteId(UINT32 id) 00065 { 00066 WriteVariableSize(kNetworkTables_ID, id); 00067 } 00068 00069 void Buffer::WriteTableId(UINT32 id) 00070 { 00071 WriteVariableSize(kNetworkTables_TABLE_ID, id); 00072 } 00073 00074 void Buffer::WriteBytes(UINT32 length, const UINT8 *entries) 00075 { 00076 UINT32 i; 00077 for (i = 0; i < length; i++) 00078 { 00079 WriteByte(entries[i]); 00080 } 00081 } 00082 00083 void Buffer::WriteByte(UINT8 entry) 00084 { 00085 if (m_size >= m_capacity) 00086 { 00087 wpi_setWPIError(NetworkTablesBufferFull); 00088 return; 00089 } 00090 m_buffer[m_size++] = entry; 00091 } 00092 00093 void Buffer::Flush(int socket) 00094 { 00095 #ifdef DEBUG 00096 if (m_size != 1 || m_buffer[0] != kNetworkTables_PING) 00097 { 00098 unsigned i; 00099 char buf[128]; 00100 char *pbuf = buf; 00101 pbuf = pbuf + snprintf(pbuf, 128 - (pbuf - buf), "\nO:"); 00102 for (i=0; i<m_size; i++) 00103 { 00104 pbuf = pbuf + snprintf(pbuf, 128 - (pbuf - buf), "%02X", m_buffer[i]); 00105 } 00106 snprintf(pbuf, 128 - (pbuf - buf), "\n"); 00107 printf(buf); 00108 } 00109 #endif 00110 write(socket, (char *)m_buffer, m_size); 00111 Clear(); 00112 } 00113 00114 void Buffer::Clear() 00115 { 00116 m_size = 0; 00117 } 00118 00119 void Buffer::WriteVariableSize(UINT32 tag, UINT32 id) 00120 { 00121 if (id < tag - 4) 00122 { 00123 WriteByte(tag | id); 00124 } 00125 else 00126 { 00127 int fullTag = (tag | (tag - 1)) ^ 3; 00128 if (id < (1 << 8)) 00129 { 00130 WriteByte(fullTag); 00131 WriteByte(id); 00132 } 00133 else if (id < (1 << 16)) 00134 { 00135 WriteByte(fullTag | 1); 00136 WriteByte(id >> 8); 00137 WriteByte(id); 00138 } 00139 else if (id < (1 << 24)) 00140 { 00141 WriteByte(fullTag | 2); 00142 WriteByte(id >> 16); 00143 WriteByte(id >> 8); 00144 WriteByte(id); 00145 } 00146 else 00147 { 00148 WriteByte(fullTag | 3); 00149 WriteByte(id >> 24); 00150 WriteByte(id >> 16); 00151 WriteByte(id >> 8); 00152 WriteByte(id); 00153 } 00154 } 00155 } 00156 00157 } // namespace
Generated on Thu Jan 12 2012 22:35:18 for WPILibC++ by
1.7.1