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
19
19
* Original author: Paul McCullagh (H&G2JCtL)
20
20
* Continued development: Barry Leslie
37
37
#include "CSMemory.h"
38
38
#include "CSMutex.h"
40
42
class CSOutputStream;
41
43
class CSInputStream;
43
#define CS_SOCKET_ADDRESS_SIZE 300
45
/* This is only required if you do
46
* not use an output buffer stream!
48
//#define CS_USE_OUTPUT_BUFFER
50
#ifdef CS_USE_OUTPUT_BUFFER
52
#define CS_OUTPUT_BUFFER_SIZE 80
53
#define CS_MIN_WRITE_SIZE 40
55
#define CS_OUTPUT_BUFFER_SIZE (4*1024)
56
#define CS_MIN_WRITE_SIZE (1500)
60
45
class CSSocket : public CSRefObject {
62
CSSocket(): iHandle(-1), iHost(NULL), iService(NULL), iIdentity(NULL), iPort(0), iTimeout(0) {
63
#ifdef CS_USE_OUTPUT_BUFFER
72
void setTimeout(uint32_t milli_sec);
49
virtual ~CSSocket() { }
74
51
CSOutputStream *getOutputStream();
76
53
CSInputStream *getInputStream();
78
virtual void formatAddress(size_t size, char *address);
55
virtual void formatAddress(size_t size, char *address) = 0;
81
58
* Publish a listener:
83
virtual void publish(char *service, int default_port);
60
virtual void publish(char *service, int default_port) = 0;
86
63
* Accept a connection from a listening socket:
88
virtual void open(CSSocket *listener);
65
virtual void open(CSSocket *listener) = 0;
91
68
* Connect to a listening socket.
93
virtual void open(char *address, int default_port);
70
virtual void open(char *address, int default_port) = 0;
96
73
* Close the socket.
75
virtual void close() { }
101
78
* Read at least one byte from the socket.
115
92
* Just like read, error on the socket do
116
93
* not throw an exception.
95
virtual int read() = 0;
121
98
* Look at the next character in the file without
122
99
* taking from the input.
101
virtual int peek() = 0;
127
104
* Write the given number of bytes.
128
105
* Throws IOException if an error occurs.
130
virtual void write(const void *data, size_t size);
107
virtual void write(const void *data, size_t size) = 0;
133
110
* Write a character to the file.
112
virtual void write(char ch) = 0;
115
* Flush the data written.
117
virtual void flush() = 0;
119
static CSSocket *newSocket();
121
friend class SCSocket;
126
#define CS_SOCKET_ADDRESS_SIZE 300
128
class SCSocket : public CSSocket {
130
SCSocket(): CSSocket(), iHandle(-1), iHost(NULL), iService(NULL), iPort(0) { }
132
virtual ~SCSocket() {
136
virtual void formatAddress(size_t size, char *address);
138
virtual void publish(char *service, int default_port);
140
virtual void open(CSSocket *listener);
142
virtual void open(char *address, int default_port);
144
virtual void close();
146
virtual size_t read(void *data, size_t size);
152
virtual void write(const void *data, size_t size);
135
154
virtual void write(char ch);
138
* Flush the data written.
140
156
virtual void flush();
142
virtual const char *identify();
144
static void initSockets();
146
static CSSocket *newSocket();
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);
152
void setNonBlocking();
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);
165
#ifdef CS_USE_OUTPUT_BUFFER
166
char iOutputBuffer[CS_OUTPUT_BUFFER_SIZE];