CARMA C++
NetBuf.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_NETBUF_H
2 #define SZA_UTIL_NETBUF_H
3 
11 namespace sza {
12  namespace util {
13 
14  class NetBuf {
15  public:
16 
20  enum NetReadState {
21  NET_READ_PREFIX, // Reading msg prefix
22  NET_READ_DATA, // Reading data
23  NET_READ_DONE, // Read done
24  NET_READ_ERROR, // Error occurred reading
25  NET_READ_CLOSED, // Connection closed while reading
26  };
27 
31  enum NetSendState {
32  NET_SEND_DATA, // Sending data
33  NET_SEND_DONE, // Send done
34  NET_SEND_ERROR, // Error occurred sending
35  NET_SEND_CLOSED, // Connection closed while sending
36  };
37 
41  NetBuf();
42 
46  NetBuf(unsigned int size, unsigned char* extBuf=0);
47 
51  NetBuf(int fd, unsigned int size, unsigned char* extBuf=0);
52 
56  virtual ~NetBuf();
57 
58  // Allocate an internal buffer, or set it to point to external
59  // memory
60 
61  void setBuffer(unsigned int size, unsigned char* extBuf=0);
62 
63  // Attach this buffer to a file descriptor
64 
65  void attach(int fd);
66 
67  // Read data into the buffer
68 
69  NetReadState read(int fd=-1);
70 
71  // Send data in the buffer
72 
73  NetSendState send(int fd=-1);
74 
75  // The size of the message prefix
76 
77  virtual unsigned int msgPrefixSize();
78 
79  // Parse the message prefix, and return the byte count
80 
81  virtual unsigned int parseMsgPrefix();
82 
83  // Write a message prefix
84 
85  virtual void putMsgPrefix(unsigned int size);
86 
87  //------------------------------------------------------------
88  // Utility methods for putting data into the buffer
89  //------------------------------------------------------------
90 
91  void startPut();
92 
93  void endPut();
94 
95  virtual void putUchar(unsigned n, unsigned char* ptr);
96 
97  virtual void putChar(unsigned n, char* ptr);
98 
99  virtual void putUshort(unsigned n, unsigned short* ptr);
100 
101  virtual void putShort(unsigned n, short* ptr);
102 
103  virtual void putUint(unsigned n, unsigned int* ptr);
104 
105  virtual void putInt(unsigned n, int* ptr);
106 
107  virtual void putFloat(unsigned n, float* ptr);
108 
109  virtual void putDouble(unsigned n, double* ptr);
110 
111  static float floatToIeee(float fval);
112 
113  static double doubleToIeee(double dval);
114 
115  //------------------------------------------------------------
116  // Utility methods for extracting data from the buffer
117  //------------------------------------------------------------
118 
119  void startGet();
120 
121  void endGet();
122 
123  virtual void getUchar(unsigned n, unsigned char* ptr);
124 
125  virtual void getChar(unsigned n, char* ptr);
126 
127  virtual void getUshort(unsigned n, unsigned short* ptr);
128 
129  virtual void getShort(unsigned n, short* ptr);
130 
131  virtual void getUint(unsigned n, unsigned int* ptr);
132 
133  virtual void getInt(unsigned n, int* ptr);
134 
135  virtual void getFloat(unsigned n, float* ptr);
136 
137  virtual void getDouble(unsigned n, double* ptr);
138 
139  virtual double netToHost(double netDouble);
140 
141  static float ieeeToFloat(unsigned int ieee);
142 
143  static double ieeeToDouble(double ieee);
144 
145  protected:
146 
147  // The file descriptor to which this buffer is attached
148 
149  int fd_;
150 
151  // Maximum size, in bytes, of a message, exclusive of any message prefix
152 
153  unsigned int maxMsgSize_;
154 
155  // Length of the message to be sent/read
156 
157  unsigned int msgLen_;
158 
159  // An internal/external buffer which will be used for
160  // reading/writing data
161 
162  unsigned char* buffer_;
163 
164  // True if the buffer is externally allocated
165 
166  bool external_;
167 
168  // The number of bytes extracted from the buffer
169 
170  unsigned nGet_;
171 
172  // The number of bytes put into the buffer
173 
174  unsigned nPut_;
175 
176  // The last read state
177 
178  NetReadState lastReadState_;
179 
180  // The last send state
181 
182  NetSendState lastSendState_;
183 
184  // Private constructor
185 
186  void privateConstructor(int fd, unsigned int size, unsigned char* extBuf);
187 
188  NetReadState setReadState(NetReadState state);
189  NetSendState setSendState(NetSendState state);
190 
191  }; // End class NetBuf
192 
193  } // End namespace util
194 } // End namespace sza
195 
196 
197 
198 #endif // End #ifndef SZA_UTIL_NETBUF_H