diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 | 
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 | 
| commit | f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch) | |
| tree | 13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/connection.h | |
| parent | 74d4c8cd27334fc7204d5a8773deb3d424565778 (diff) | |
| download | libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2 libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip | |
Ok, no code is left in src, it's all in src/old.  We'll gradually move code back
into src as it's fixed and re-org'd.  This includes tests, which, I may write a
unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/connection.h')
| -rw-r--r-- | src/connection.h | 411 | 
1 files changed, 0 insertions, 411 deletions
| diff --git a/src/connection.h b/src/connection.h deleted file mode 100644 index 0e991c7..0000000 --- a/src/connection.h +++ /dev/null | |||
| @@ -1,411 +0,0 @@ | |||
| 1 | /**\file | ||
| 2 | * Contains the Connection class. | ||
| 3 | *@author Mike Buland | ||
| 4 | */ | ||
| 5 | |||
| 6 | #ifndef CONNECTION_H | ||
| 7 | #define CONNECTION_H | ||
| 8 | |||
| 9 | #include "multilog.h" | ||
| 10 | #include "flexbuf.h" | ||
| 11 | #include "protocol.h" | ||
| 12 | |||
| 13 | /** Represents a single connection on a network. While these connections | ||
| 14 | * may be treated more or less just like files, occasionally problems arise | ||
| 15 | * when writing data at any time you feel like. Therefore you run all your | ||
| 16 | * data through a Connection, which buffers all data and makes sure no | ||
| 17 | * buffers are exceeded and nothing inappropriate for the recipient of the | ||
| 18 | * data is sent. | ||
| 19 | *@author Mike Buland | ||
| 20 | */ | ||
| 21 | class Connection | ||
| 22 | { | ||
| 23 | public: | ||
| 24 | /** | ||
| 25 | * Construct a blank and non-connected Connection. The created object is | ||
| 26 | * not yet connected to anything, and most of the functions except open are | ||
| 27 | * unusable. | ||
| 28 | */ | ||
| 29 | Connection(); | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Destroy the connection, clean up all pending data requests and close the | ||
| 33 | * contained socket. This does not send out pending data, especially since | ||
| 34 | * such an operation could take considerable time, depending on the pending | ||
| 35 | * data and state of the receiving end. | ||
| 36 | */ | ||
| 37 | virtual ~Connection(); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Open a connection to a remote server. This sets up this connection as | ||
| 41 | * a client instead of a server and does all of the work that needs to be | ||
| 42 | * done to actually open an INET_AF connection, which is a lot of work. | ||
| 43 | *@param sAddr The address to connect to. This can be in any format | ||
| 44 | * normally understood by your system to be an address, ip, domain name, | ||
| 45 | * etc. | ||
| 46 | *@param nPort The port number to connect to on the remote server. | ||
| 47 | *@returns True if the connection was successful and everything is setup, | ||
| 48 | * false if there were any of a dozen errors and the connection is not set. | ||
| 49 | *@todo Make this function add log entries to a standard MultiLog if | ||
| 50 | * something goes wrong. | ||
| 51 | */ | ||
| 52 | bool open( const char *sAddr, int nPort, int nSec=30 ); | ||
| 53 | |||
| 54 | void ensureCapacity( int nSize ); | ||
| 55 | |||
| 56 | /** Append the given data to the output. The data is presumed to be null | ||
| 57 | * terminated. To put binary data into the stream, use the other | ||
| 58 | * appendOutput function. This should be the only method used to | ||
| 59 | * communicate with the socket. | ||
| 60 | *@param lpOutput The data to add to the output queue. | ||
| 61 | *@param nSize How much data is in the lpOutput buffer. If this value | ||
| 62 | * is -1 then the program treats lpOutput as a null-terminated string. | ||
| 63 | *@returns True if everything is ok, false otherwise. | ||
| 64 | */ | ||
| 65 | bool appendOutput( const char *lpOutput, int nSize=-1 ); | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Append the character to the output. | ||
| 69 | *@param lOutput The character to add to the output queue. | ||
| 70 | *@returns True if everything is ok, false otherwise. | ||
| 71 | */ | ||
| 72 | bool appendOutput( const char lOutput ); | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Append the short to the output. | ||
| 76 | *@param lOutput The short to add to the output queue. | ||
| 77 | *@returns True if everything is ok, false otherwise. | ||
| 78 | */ | ||
| 79 | bool appendOutput( const short lOutput ); | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Append the int to the output. | ||
| 83 | *@param lOutput The int to add to the output queue. | ||
| 84 | *@returns True if everything is ok, false otherwise. | ||
| 85 | */ | ||
| 86 | bool appendOutput( const int lOutput ); | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Append the long to the output. | ||
| 90 | *@param lOutput The long to add to the output queue. | ||
| 91 | *@returns True if everything is ok, false otherwise. | ||
| 92 | */ | ||
| 93 | bool appendOutput( const long lOutput ); | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Append the float to the output. | ||
| 97 | *@param lOutput The float to add to the output queue. | ||
| 98 | *@returns True if everything is ok, false otherwise. | ||
| 99 | */ | ||
| 100 | bool appendOutput( const float lOutput ); | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Append the double to the output. | ||
| 104 | *@param lOutput The double to add to the output queue. | ||
| 105 | *@returns True if everything is ok, false otherwise. | ||
| 106 | */ | ||
| 107 | bool appendOutput( const double lOutput ); | ||
| 108 | |||
| 109 | /** | ||
| 110 | * Append the unsigned char to the output. | ||
| 111 | *@param lOutput The unsigned char to add to the output queue. | ||
| 112 | *@returns True if everything is ok, false otherwise. | ||
| 113 | */ | ||
| 114 | bool appendOutput( const unsigned char lOutput ); | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Append the unsigned short to the output. | ||
| 118 | *@param lOutput The unsigned short to add to the output queue. | ||
| 119 | *@returns True if everything is ok, false otherwise. | ||
| 120 | */ | ||
| 121 | bool appendOutput( const unsigned short lOutput ); | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Append the unsigned int to the output. | ||
| 125 | *@param lOutput The unsigned int to add to the output queue. | ||
| 126 | *@returns True if everything is ok, false otherwise. | ||
| 127 | */ | ||
| 128 | bool appendOutput( const unsigned int lOutput ); | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Append the unsigned long to the output. | ||
| 132 | *@param lOutput The unsigned long to add to the output queue. | ||
| 133 | *@returns True if everything is ok, false otherwise. | ||
| 134 | */ | ||
| 135 | bool appendOutput( const unsigned long lOutput ); | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Writes all input data in the buffer in a dual-view ascii and hex display | ||
| 139 | * to a file. There are a number of options that also help with debugging. | ||
| 140 | *@param lpPrefix Text to be added to the begining of every line written | ||
| 141 | * out. The default is a blank string. | ||
| 142 | *@param fh The file to write the data to in text mode. This is stdout by | ||
| 143 | * default, but could be any already open file handle. | ||
| 144 | *@param nBytesMax The maximum number of bytes to write to the output. The | ||
| 145 | * amount of data can be overwhelming sometimes, so you can limit it. The | ||
| 146 | * default value is -1, which is also unlimited. | ||
| 147 | */ | ||
| 148 | void printInputDebug( const char *lpPrefix="", FILE *fh=stdout, int nBytesMax=-1 ); | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Writes all output data in the buffer in a dual-view ascii and hex display | ||
| 152 | * to a file. There are a number of options that also help with debugging. | ||
| 153 | *@param lpPrefix Text to be added to the begining of every line written | ||
| 154 | * out. The default is a blank string. | ||
| 155 | *@param fh The file to write the data to in text mode. This is stdout by | ||
| 156 | * default, but could be any already open file handle. | ||
| 157 | *@param nBytesMax The maximum number of bytes to write to the output. The | ||
| 158 | * amount of data can be overwhelming sometimes, so you can limit it. The | ||
| 159 | * default value is -1, which is also unlimited. | ||
| 160 | */ | ||
| 161 | void printOutputDebug( const char *lpPrefix="", FILE *fh=stdout, int nBytesMax=-1 ); | ||
| 162 | |||
| 163 | /** | ||
| 164 | * This is the low-level generic function that is called by both | ||
| 165 | * printInputDebug and printOutputDebug. It works effectively just like | ||
| 166 | * both of them, except that you can give it a raw pointer to the data to | ||
| 167 | * print out. This probably doesn't belong in this class, but this was | ||
| 168 | * where I was when I needed it. | ||
| 169 | *@param pData A pointer to the data to write. This is not treated as a | ||
| 170 | * null terminated string, so make sure that the nDataLen param is set | ||
| 171 | * properly. | ||
| 172 | *@param nDataLen The number of bytes that are in pData and that you want to | ||
| 173 | * see. | ||
| 174 | *@param lpName The name of the data, this is used in the header where it | ||
| 175 | * says "Displaying nnn bytes of <lpName>." A good example would be input | ||
| 176 | * or output. | ||
| 177 | *@param lpPrefix Text to put before every line output. This just makes it | ||
| 178 | * easier to tell large blocks apart in the output. | ||
| 179 | *@param fh The file handle to write all data to. | ||
| 180 | *@param nBytesMax The maximum number of bytes. This parameter is stupid. | ||
| 181 | * If it is set to -1, then nDataLen is used, otherwise the smaller value is | ||
| 182 | * used as the number of bytes to output. | ||
| 183 | *@todo Put this function somewhere more deserving. | ||
| 184 | *@todo Remove the nBytesMax param, we need that in the other functions, | ||
| 185 | * not this one! | ||
| 186 | */ | ||
| 187 | void printDataDebug( const unsigned char *pData, long nDataLen, const char *lpName, const char *lpPrefix, FILE *fh, int nBytesMax ); | ||
| 188 | |||
| 189 | /** Append the given data to the input. The data is presumed to be null | ||
| 190 | * terminated. To put binary data into the stream, use the other | ||
| 191 | * appendInput function. This is mainly used by internal routines. | ||
| 192 | *@param lpInput The data to add to the input queue. | ||
| 193 | *@param nSize How much data is in the lpInput buffer. If this value | ||
| 194 | * is -1 then the program treats lpOutput as a null-terminated string. | ||
| 195 | *@returns True if everything is ok, false otherwise. | ||
| 196 | */ | ||
| 197 | bool appendInput( const char *lpInput, int nSize=-1 ); | ||
| 198 | |||
| 199 | /** Searches through the current pending input for a certain character. | ||
| 200 | * This is useful for finding out where exactly the end of a line is, for | ||
| 201 | * example, to see if a command has been entered yet. | ||
| 202 | *@param cTarget The character to search for. | ||
| 203 | *@returns The position of the target relative to the begining of the input | ||
| 204 | * or -1 if the target wasn't found. | ||
| 205 | */ | ||
| 206 | int scanInputFor( char cTarget ); | ||
| 207 | |||
| 208 | /** Gets a pointer to the output buffer. This is mainly used by internal | ||
| 209 | * routines, and is cleared every click when data is sent out again. | ||
| 210 | *@returns A pointer to the buffer holding the pending output data. | ||
| 211 | */ | ||
| 212 | const char *getOutput(); | ||
| 213 | |||
| 214 | /** Gets a pointer to the start of the input buffer's active data | ||
| 215 | * section. Use this to gain access to the input you need to do | ||
| 216 | * your job. | ||
| 217 | *@returns A pointer to the data in the input buffer. Do not delete this. | ||
| 218 | */ | ||
| 219 | const char *getInput(); | ||
| 220 | |||
| 221 | /** Clears all pending output, this is mainly just used internally. | ||
| 222 | *@returns True if operation was a success, otherwise false. | ||
| 223 | */ | ||
| 224 | bool clearOutput(); | ||
| 225 | |||
| 226 | /** Clears all pending input, weather it's been used or not. Please | ||
| 227 | * refrain from calling this during normal operation, use usedInput | ||
| 228 | * instead, it's much safer. | ||
| 229 | *@returns True if the operation was a success, false otherwise. | ||
| 230 | */ | ||
| 231 | bool clearInput(); | ||
| 232 | |||
| 233 | /** Sets the socket that should be used internally. | ||
| 234 | *@param nNewSocket The new socket to work with. | ||
| 235 | */ | ||
| 236 | void setSocket( int nNewSocket ); | ||
| 237 | |||
| 238 | /** Gets the handle (number) of the working socket. This can be a | ||
| 239 | * dangerous function to call, please refrain from calling it directly | ||
| 240 | * if any alternative can be found. | ||
| 241 | *@returns The number of the working socket. | ||
| 242 | */ | ||
| 243 | int getSocket(); | ||
| 244 | |||
| 245 | /** Determines if the connection is still active. | ||
| 246 | *@returns True if the connection is active, false otherwise. | ||
| 247 | */ | ||
| 248 | bool isActive(); | ||
| 249 | |||
| 250 | /** Clears all buffers and sets up the connection to be reused. | ||
| 251 | * Does not actually close the socket, that's handled by the | ||
| 252 | * ConnectionManager | ||
| 253 | */ | ||
| 254 | void close(); | ||
| 255 | |||
| 256 | /** Opens a socket. Really just sets up the connection for use since | ||
| 257 | * the socket itself was created and opened by the ConnectionManager. | ||
| 258 | * This also calls setSocket so you don't have to. | ||
| 259 | *@param nNewSocket The socket to assosiate with. | ||
| 260 | */ | ||
| 261 | bool open( int nNewSocket ); | ||
| 262 | |||
| 263 | /** | ||
| 264 | * Reads all pending input from the connection. If this is called outside | ||
| 265 | * of the ConnectionManager it will usually block indefinately waiting for | ||
| 266 | * new data. The only way to change this behaviour is to modify the socket | ||
| 267 | * low-level when you connect it manually, or, preferably use the other | ||
| 268 | * readInput function to control blocking time. | ||
| 269 | *@returns True socket is still connected, otherwise false. | ||
| 270 | */ | ||
| 271 | int readInput(); | ||
| 272 | |||
| 273 | /** | ||
| 274 | * Reads all pending input from the connection, blocking up to nSec | ||
| 275 | * seconds and nUSec micro-seconds for the data. This uses select to | ||
| 276 | * simulate blocking, but has the same effect as standard io blocking. | ||
| 277 | * If you don't want to block, just set both values to zero. The back | ||
| 278 | * parameters are optional, set to null to not use them. The variables | ||
| 279 | * you pass in through the back parameters will contain the remaining | ||
| 280 | * time if data arrived before the max timeout was reached. | ||
| 281 | *@param nSec Max seconds to wait. | ||
| 282 | *@param nUSec Max micro-seconds to wait. | ||
| 283 | *@param pnSecBack The number of seconds remaining. | ||
| 284 | *@param pnUSecBack The number of micro-seconds remaining. | ||
| 285 | */ | ||
| 286 | bool readInput( int nSec, int nUSec, int *pnSecBack=NULL, int *pnUSecBack=NULL ); | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Waits until at least nBytesIn are read into the input buffer and ready | ||
| 290 | * to be used. Wait at most nSec seconds plus nUSec micro seconds. | ||
| 291 | * If the timeout is exceeded, this function throws an exception. If this | ||
| 292 | * function returns normally, you are guranteed to have at least nBytesIn | ||
| 293 | * bytes in your input buffer. | ||
| 294 | *@param nBytesIn Number of bytes to read. | ||
| 295 | *@param nSec The max seconds to wait. | ||
| 296 | *@param sUSec The max microseconds to wait. | ||
| 297 | */ | ||
| 298 | void waitForInput( int nBytesIn, int nSec, int nUSec ); | ||
| 299 | |||
| 300 | /** Writes some data that is pending to the socket. | ||
| 301 | *@returns True if all data was written succesfully, false otherwise. | ||
| 302 | */ | ||
| 303 | bool writeOutput(); | ||
| 304 | |||
| 305 | /** | ||
| 306 | * Writes all data that is pending on the socekt. | ||
| 307 | */ | ||
| 308 | bool writeAllOutput(); | ||
| 309 | |||
| 310 | /** Determines if the connection has output waiting to go out. | ||
| 311 | *@returns true if there is pending output, otherwise false. | ||
| 312 | */ | ||
| 313 | bool hasOutput(); | ||
| 314 | |||
| 315 | /** Sets internal flags so that this connection will be deleted next | ||
| 316 | * time through the ConnectionManager. | ||
| 317 | */ | ||
| 318 | void disconnect(); | ||
| 319 | |||
| 320 | /** Determines if this connection is ready to be disconnected or not. | ||
| 321 | *@returns True if it is time to disconnect, false if it isn't. | ||
| 322 | */ | ||
| 323 | bool needDisconnect(); | ||
| 324 | |||
| 325 | /** Tells the caller if there is pending input waiting to be processed. | ||
| 326 | *@returns True if there is pending input that has not been used, returns | ||
| 327 | * false if there isn't. | ||
| 328 | */ | ||
| 329 | bool hasInput(); | ||
| 330 | |||
| 331 | /** Removes bytes from the begining of the input queue. Use this after | ||
| 332 | * getting the input and processing as much as you need to. | ||
| 333 | *@param nAmount The number of bytes used. | ||
| 334 | *@returns true if the update was successful, otherwise false. | ||
| 335 | */ | ||
| 336 | bool usedInput( int nAmount ); | ||
| 337 | |||
| 338 | /** Sets the protocol to be used by this connection. All data in and out | ||
| 339 | * passes through the protocol object, which may process that data to | ||
| 340 | * filter out and process any special messages that may have been | ||
| 341 | * included. Everything that isn't processed can be accessed in the | ||
| 342 | * standard method. | ||
| 343 | *@param pNewProtocol A pointer to a protocol object that you want to | ||
| 344 | * use. | ||
| 345 | */ | ||
| 346 | void setProtocol( class Protocol *pNewProtocol ); | ||
| 347 | |||
| 348 | /** Gets the number of bytes that are waiting in the input queue, the data | ||
| 349 | * that has yet to be processed. | ||
| 350 | *@returns The number of bytes in the input queue. | ||
| 351 | */ | ||
| 352 | int getInputAmnt(); | ||
| 353 | |||
| 354 | /** Gets the number of bytes that are waiting in the output queue, the data | ||
| 355 | * that has yet to be sent to the connected socket. | ||
| 356 | *@returns The number of bytes in the input queue. | ||
| 357 | */ | ||
| 358 | int getOutputAmnt(); | ||
| 359 | |||
| 360 | /** Gets a pointer to the protocol that is attatched to this connection | ||
| 361 | * object. This is useful to set modes, and send special commands in | ||
| 362 | * addition to the standard raw data reads and writes that are normally | ||
| 363 | * permitted. In fact, in everything besides a raw telnet protocol all | ||
| 364 | * data should be sent through the protocol and not the connection object. | ||
| 365 | *@returns A pointer to the Protocol assosiated with this connection. | ||
| 366 | */ | ||
| 367 | class Protocol *getProtocol(); | ||
| 368 | |||
| 369 | private: | ||
| 370 | /** | ||
| 371 | * A buffer to keep data read from the socket in. This is filled in by | ||
| 372 | * the function readInput, which is automatically called by the | ||
| 373 | * ConnectionManager whenever new data is ready. | ||
| 374 | */ | ||
| 375 | FlexBuf xInputBuf; | ||
| 376 | |||
| 377 | /** | ||
| 378 | * A buffer to keep data that should be sent to the socket. This is filled | ||
| 379 | * in by using the AppendOutput functions and is sent to the socket using | ||
| 380 | * the writeOutput function, which is automatically called every cycle by | ||
| 381 | * the ConnectionManager when there is pending data. | ||
| 382 | */ | ||
| 383 | FlexBuf xOutputBuf; | ||
| 384 | |||
| 385 | /** | ||
| 386 | * The socket that the user is connected to. This is not the same as the | ||
| 387 | * socket number of the listening socket, this is the unique socket on the | ||
| 388 | * system that the data is coming to. | ||
| 389 | */ | ||
| 390 | int nSocket; | ||
| 391 | |||
| 392 | /** | ||
| 393 | * True=active connection, False=connection lost | ||
| 394 | */ | ||
| 395 | bool bActive; | ||
| 396 | |||
| 397 | /** | ||
| 398 | * True=disconnect next cycle (after data is transmitted), Flse=keep going. | ||
| 399 | */ | ||
| 400 | bool bDisconnectMe; | ||
| 401 | |||
| 402 | /** | ||
| 403 | * A pointer to a protocol handler that can automatically process the data | ||
| 404 | * in the buffers. This is optional if you use the connections on your own | ||
| 405 | * but reccomended if you use this with the rest of the ConnectionManager | ||
| 406 | * system. | ||
| 407 | */ | ||
| 408 | class Protocol *pProtocol; | ||
| 409 | }; | ||
| 410 | |||
| 411 | #endif | ||
