Drizzled Public API Documentation

CSSocket.h
00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-05-24
00023  *
00024  * CORE SYSTEM:
00025  * Basic file I/O.
00026  *
00027  */
00028 
00029 #pragma once
00030 #ifndef __CSSOCKET_H__
00031 #define __CSSOCKET_H__
00032 
00033 #include <stdio.h>
00034 
00035 #include "CSDefs.h"
00036 #include "CSPath.h"
00037 #include "CSException.h"
00038 #include "CSMemory.h"
00039 #include "CSMutex.h"
00040 
00041 class CSOutputStream;
00042 class CSInputStream;
00043 
00044 #define CS_SOCKET_ADDRESS_SIZE    300
00045 
00046 /* This is only required if you do
00047  * not use an output buffer stream!
00048  */
00049 //#define CS_USE_OUTPUT_BUFFER
00050 
00051 #ifdef CS_USE_OUTPUT_BUFFER
00052 #ifdef DEBUG
00053 #define CS_OUTPUT_BUFFER_SIZE   80
00054 #define CS_MIN_WRITE_SIZE     40
00055 #else
00056 #define CS_OUTPUT_BUFFER_SIZE   (4*1024)
00057 #define CS_MIN_WRITE_SIZE     (1500)
00058 #endif
00059 #endif
00060 
00061 class CSSocket : public CSRefObject {
00062 public:
00063   CSSocket(): iHandle(-1), iHost(NULL), iService(NULL), iIdentity(NULL), iPort(0), iTimeout(0) {
00064 #ifdef CS_USE_OUTPUT_BUFFER
00065   iDataLen = 0;
00066 #endif
00067   }
00068 
00069   virtual ~CSSocket() {
00070     close();
00071   }
00072 
00073   void setTimeout(uint32_t milli_sec);
00074 
00075   CSOutputStream *getOutputStream();
00076 
00077   CSInputStream *getInputStream();
00078 
00079   virtual void formatAddress(size_t size, char *address);
00080 
00081   /*
00082    * Publish a listener:
00083    */
00084   virtual void publish(char *service, int default_port);
00085 
00086   /*
00087    * Accept a connection from a listening socket:
00088    */
00089   virtual void open(CSSocket *listener);
00090 
00091   /*
00092    * Connect to a listening socket.
00093    */
00094   virtual void open(char *address, int default_port);
00095 
00096   /*
00097    * Close the socket.
00098    */
00099   virtual void close();
00100 
00101   /*
00102    * Read at least one byte from the socket.
00103    * This function returns 0 on EOF.
00104    * If the function returns at least
00105    * one byte, then you must call the function
00106    * again, there may be more data available.
00107    *
00108    * Note: Errors on the socket do not cause
00109    * an exception!
00110    */
00111   virtual size_t read(void *data, size_t size);
00112 
00113   /*
00114    * Returns -1 on EOF!
00115    * Otherwize it returns a character value >= 0
00116    * Just like read, error on the socket do
00117    * not throw an exception.
00118    */
00119   virtual int read();
00120 
00121   /*
00122    * Look at the next character in the file without
00123    * taking from the input.
00124    */
00125   virtual int peek();
00126 
00127   /*
00128    * Write the given number of bytes.
00129    * Throws IOException if an error occurs.
00130    */
00131   virtual void write(const  void *data, size_t size);
00132 
00133   /*
00134    * Write a character to the file.
00135    */
00136   virtual void write(char ch);
00137 
00138   /*
00139    * Flush the data written.
00140    */
00141   virtual void flush();
00142 
00143   virtual const char *identify();
00144 
00145   static void initSockets();
00146 
00147   static CSSocket *newSocket();
00148 
00149 private:
00150   void throwError(const char *func, const char *file, int line, char *address, int err);
00151   void throwError(const char *func, const char *file, int line, int err);
00152   void setNoDelay();
00153   void setNonBlocking();
00154   void setBlocking();
00155   void openInternal();
00156   void writeBlock(const void *data, size_t len);
00157   int timeoutRead(CSThread *self, void *buffer, size_t length);
00158 
00159   int     iHandle;
00160   char    *iHost;
00161   char    *iService;
00162   char    *iIdentity;
00163   int     iPort;
00164   uint32_t  iTimeout;
00165 
00166 #ifdef CS_USE_OUTPUT_BUFFER
00167   char  iOutputBuffer[CS_OUTPUT_BUFFER_SIZE];
00168   size_t  iDataLen;
00169 #endif
00170 
00171 };
00172 
00173 #endif