37
37
#include "CSMemory.h"
38
38
#include "CSMutex.h"
42
40
class CSOutputStream;
43
41
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)
45
60
class CSSocket : public CSRefObject {
49
virtual ~CSSocket() { }
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);
51
74
CSOutputStream *getOutputStream();
53
76
CSInputStream *getInputStream();
55
virtual void formatAddress(size_t size, char *address) = 0;
78
virtual void formatAddress(size_t size, char *address);
58
81
* Publish a listener:
60
virtual void publish(char *service, int default_port) = 0;
83
virtual void publish(char *service, int default_port);
63
86
* Accept a connection from a listening socket:
65
virtual void open(CSSocket *listener) = 0;
88
virtual void open(CSSocket *listener);
68
91
* Connect to a listening socket.
70
virtual void open(char *address, int default_port) = 0;
93
virtual void open(char *address, int default_port);
73
96
* Close the socket.
75
virtual void close() { }
78
101
* Read at least one byte from the socket.
92
115
* Just like read, error on the socket do
93
116
* not throw an exception.
95
virtual int read() = 0;
98
121
* Look at the next character in the file without
99
122
* taking from the input.
101
virtual int peek() = 0;
104
127
* Write the given number of bytes.
105
128
* Throws IOException if an error occurs.
107
virtual void write(const void *data, size_t size) = 0;
130
virtual void write(const void *data, size_t size);
110
133
* Write a character to the file.
112
virtual void write(char ch) = 0;
135
virtual void write(char ch);
115
138
* Flush the data written.
117
virtual void flush() = 0;
140
virtual void flush();
142
virtual const char *identify();
144
static void initSockets();
119
146
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);
154
virtual void write(char ch);
156
virtual void flush();
159
149
void throwError(const char *func, const char *file, int line, char *address, int err);
160
150
void throwError(const char *func, const char *file, int line, int err);
161
void setInternalOptions();
152
void setNonBlocking();
162
154
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];