~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSSocket.h

Merge Joe, plus I updated the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
2
2
 *
3
3
 * PrimeBase Media Stream for MySQL
4
4
 *
14
14
 *
15
15
 * You should have received a copy of the GNU General Public License
16
16
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
 *
19
19
 * Original author: Paul McCullagh (H&G2JCtL)
20
20
 * Continued development: Barry Leslie
37
37
#include "CSMemory.h"
38
38
#include "CSMutex.h"
39
39
 
 
40
using namespace std;
 
41
 
40
42
class CSOutputStream;
41
43
class CSInputStream;
42
44
 
43
 
#define CS_SOCKET_ADDRESS_SIZE          300
44
 
 
45
 
/* This is only required if you do
46
 
 * not use an output buffer stream!
47
 
 */
48
 
//#define CS_USE_OUTPUT_BUFFER
49
 
 
50
 
#ifdef CS_USE_OUTPUT_BUFFER
51
 
#ifdef DEBUG
52
 
#define CS_OUTPUT_BUFFER_SIZE           80
53
 
#define CS_MIN_WRITE_SIZE                       40
54
 
#else
55
 
#define CS_OUTPUT_BUFFER_SIZE           (4*1024)
56
 
#define CS_MIN_WRITE_SIZE                       (1500)
57
 
#endif
58
 
#endif
59
 
 
60
45
class CSSocket : public CSRefObject {
61
46
public:
62
 
        CSSocket(): iHandle(-1), iHost(NULL), iService(NULL), iIdentity(NULL), iPort(0), iTimeout(0) {
63
 
#ifdef CS_USE_OUTPUT_BUFFER
64
 
        iDataLen = 0;
65
 
#endif
66
 
        }
67
 
 
68
 
        virtual ~CSSocket() {
69
 
                close();
70
 
        }
71
 
 
72
 
        void setTimeout(uint32_t milli_sec);
 
47
        CSSocket() { }
 
48
 
 
49
        virtual ~CSSocket() { }
73
50
 
74
51
        CSOutputStream *getOutputStream();
75
52
 
76
53
        CSInputStream *getInputStream();
77
54
 
78
 
        virtual void formatAddress(size_t size, char *address);
 
55
        virtual void formatAddress(size_t size, char *address) = 0;
79
56
 
80
57
        /*
81
58
         * Publish a listener:
82
59
         */
83
 
        virtual void publish(char *service, int default_port);
 
60
        virtual void publish(char *service, int default_port) = 0;
84
61
 
85
62
        /*
86
63
         * Accept a connection from a listening socket:
87
64
         */
88
 
        virtual void open(CSSocket *listener);
 
65
        virtual void open(CSSocket *listener) = 0;
89
66
 
90
67
        /*
91
68
         * Connect to a listening socket.
92
69
         */
93
 
        virtual void open(char *address, int default_port);
 
70
        virtual void open(char *address, int default_port) = 0;
94
71
 
95
72
        /*
96
73
         * Close the socket.
97
74
         */
98
 
        virtual void close();
 
75
        virtual void close() { }
99
76
 
100
77
        /*
101
78
         * Read at least one byte from the socket.
107
84
         * Note: Errors on the socket do not cause
108
85
         * an exception!
109
86
         */
110
 
        virtual size_t read(void *data, size_t size);
 
87
        virtual size_t read(void *data, size_t size) = 0;
111
88
 
112
89
        /*
113
90
         * Returns -1 on EOF!
115
92
         * Just like read, error on the socket do
116
93
         * not throw an exception.
117
94
         */
118
 
        virtual int read();
 
95
        virtual int read() = 0;
119
96
 
120
97
        /*
121
98
         * Look at the next character in the file without
122
99
         * taking from the input.
123
100
         */
124
 
        virtual int peek();
 
101
        virtual int peek() = 0;
125
102
 
126
103
        /*
127
104
         * Write the given number of bytes.
128
105
         * Throws IOException if an error occurs.
129
106
         */
130
 
        virtual void write(const  void *data, size_t size);
 
107
        virtual void write(const  void *data, size_t size) = 0;
131
108
 
132
109
        /*
133
110
         * Write a character to the file.
134
111
         */
 
112
        virtual void write(char ch) = 0;
 
113
 
 
114
        /*
 
115
         * Flush the data written.
 
116
         */
 
117
        virtual void flush() = 0;
 
118
 
 
119
        static CSSocket *newSocket();
 
120
 
 
121
        friend class SCSocket;
 
122
 
 
123
private:
 
124
};
 
125
 
 
126
#define CS_SOCKET_ADDRESS_SIZE          300
 
127
 
 
128
class SCSocket : public CSSocket {
 
129
public:
 
130
        SCSocket(): CSSocket(), iHandle(-1), iHost(NULL), iService(NULL), iPort(0) { }
 
131
 
 
132
        virtual ~SCSocket() {
 
133
                close();
 
134
        }
 
135
 
 
136
        virtual void formatAddress(size_t size, char *address);
 
137
 
 
138
        virtual void publish(char *service, int default_port);
 
139
 
 
140
        virtual void open(CSSocket *listener);
 
141
 
 
142
        virtual void open(char *address, int default_port);
 
143
 
 
144
        virtual void close();
 
145
 
 
146
        virtual size_t read(void *data, size_t size);
 
147
 
 
148
        virtual int read();
 
149
 
 
150
        virtual int peek();
 
151
 
 
152
        virtual void write(const void *data, size_t size);
 
153
 
135
154
        virtual void write(char ch);
136
155
 
137
 
        /*
138
 
         * Flush the data written.
139
 
         */
140
156
        virtual void flush();
141
157
 
142
 
        virtual const char *identify();
143
 
 
144
 
        static void initSockets();
145
 
 
146
 
        static CSSocket *newSocket();
147
 
 
148
158
private:
149
159
        void throwError(const char *func, const char *file, int line, char *address, int err);
150
160
        void throwError(const char *func, const char *file, int line, int err);
151
 
        void setNoDelay();
152
 
        void setNonBlocking();
153
 
        void setBlocking();
 
161
        void setInternalOptions();
154
162
        void openInternal();
155
 
        void writeBlock(const void *data, size_t len);
156
 
        int timeoutRead(CSThread *self, void *buffer, size_t length);
157
 
 
158
 
        int                     iHandle;
159
 
        char            *iHost;
160
 
        char            *iService;
161
 
        char            *iIdentity;
162
 
        int                     iPort;
163
 
        uint32_t        iTimeout;
164
 
 
165
 
#ifdef CS_USE_OUTPUT_BUFFER
166
 
        char    iOutputBuffer[CS_OUTPUT_BUFFER_SIZE];
167
 
        size_t  iDataLen;
168
 
#endif
169
 
 
 
163
 
 
164
        int iHandle;
 
165
        char *iHost;
 
166
        char *iService;
 
167
        int iPort;
170
168
};
171
169
 
172
170
#endif