~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/src/cslib/CSSocket.h

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh (H&G2JCtL)
 
20
 *
 
21
 * 2007-05-24
 
22
 *
 
23
 * CORE SYSTEM:
 
24
 * Basic file I/O.
 
25
 *
 
26
 */
 
27
 
 
28
#ifndef __CSSOCKET_H__
 
29
#define __CSSOCKET_H__
 
30
 
 
31
#include <stdio.h>
 
32
 
 
33
#include "CSDefs.h"
 
34
#include "CSPath.h"
 
35
#include "CSException.h"
 
36
#include "CSMemory.h"
 
37
#include "CSMutex.h"
 
38
 
 
39
using namespace std;
 
40
 
 
41
class CSOutputStream;
 
42
class CSInputStream;
 
43
 
 
44
class CSSocket : public CSRefObject {
 
45
public:
 
46
        CSSocket() { }
 
47
 
 
48
        virtual ~CSSocket() { }
 
49
 
 
50
        CSOutputStream *getOutputStream();
 
51
 
 
52
        CSInputStream *getInputStream();
 
53
 
 
54
        virtual void formatAddress(size_t size, char *address) = 0;
 
55
 
 
56
        /*
 
57
         * Publish a listener:
 
58
         */
 
59
        virtual void publish(char *service, int default_port) = 0;
 
60
 
 
61
        /*
 
62
         * Accept a connection from a listening socket:
 
63
         */
 
64
        virtual void open(CSSocket *listener) = 0;
 
65
 
 
66
        /*
 
67
         * Connect to a listening socket.
 
68
         */
 
69
        virtual void open(char *address, int default_port) = 0;
 
70
 
 
71
        /*
 
72
         * Close the socket.
 
73
         */
 
74
        virtual void close() { }
 
75
 
 
76
        /*
 
77
         * Read at least one byte from the socket.
 
78
         * This function returns 0 on EOF.
 
79
         * If the function returns at least
 
80
         * one byte, then you must call the function
 
81
         * again, there may be more data available.
 
82
         *
 
83
         * Note: Errors on the socket do not cause
 
84
         * an exception!
 
85
         */
 
86
        virtual size_t read(void *data, size_t size) = 0;
 
87
 
 
88
        /*
 
89
         * Returns -1 on EOF!
 
90
         * Otherwize it returns a character value >= 0
 
91
         * Just like read, error on the socket do
 
92
         * not throw an exception.
 
93
         */
 
94
        virtual int read() = 0;
 
95
 
 
96
        /*
 
97
         * Look at the next character in the file without
 
98
         * taking from the input.
 
99
         */
 
100
        virtual int peek() = 0;
 
101
 
 
102
        /*
 
103
         * Write the given number of bytes.
 
104
         * Throws IOException if an error occurs.
 
105
         */
 
106
        virtual void write(const  void *data, size_t size) = 0;
 
107
 
 
108
        /*
 
109
         * Write a character to the file.
 
110
         */
 
111
        virtual void write(char ch) = 0;
 
112
 
 
113
        /*
 
114
         * Flush the data written.
 
115
         */
 
116
        virtual void flush() = 0;
 
117
 
 
118
        static CSSocket *newSocket();
 
119
 
 
120
        friend class SCSocket;
 
121
 
 
122
private:
 
123
};
 
124
 
 
125
#define CS_SOCKET_ADDRESS_SIZE          300
 
126
 
 
127
class SCSocket : public CSSocket {
 
128
public:
 
129
        SCSocket(): CSSocket(), iHandle(-1), iHost(NULL), iService(NULL), iPort(0) { }
 
130
 
 
131
        virtual ~SCSocket() {
 
132
                close();
 
133
        }
 
134
 
 
135
        virtual void formatAddress(size_t size, char *address);
 
136
 
 
137
        virtual void publish(char *service, int default_port);
 
138
 
 
139
        virtual void open(CSSocket *listener);
 
140
 
 
141
        virtual void open(char *address, int default_port);
 
142
 
 
143
        virtual void close();
 
144
 
 
145
        virtual size_t read(void *data, size_t size);
 
146
 
 
147
        virtual int read();
 
148
 
 
149
        virtual int peek();
 
150
 
 
151
        virtual void write(const void *data, size_t size);
 
152
 
 
153
        virtual void write(char ch);
 
154
 
 
155
        virtual void flush();
 
156
 
 
157
private:
 
158
        void throwError(const char *func, const char *file, int line, char *address, int err);
 
159
        void throwError(const char *func, const char *file, int line, int err);
 
160
        void setInternalOptions();
 
161
        void openInternal();
 
162
 
 
163
        int iHandle;
 
164
        char *iHost;
 
165
        char *iService;
 
166
        int iPort;
 
167
};
 
168
 
 
169
#endif