~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Original author: Paul McCullagh (H&G2JCtL)
 * Continued development: Barry Leslie
 *
 * 2007-05-24
 *
 * CORE SYSTEM:
 * Basic file I/O.
 *
 */

#ifndef __CSSOCKET_H__
#define __CSSOCKET_H__

#include <stdio.h>

#include "CSDefs.h"
#include "CSPath.h"
#include "CSException.h"
#include "CSMemory.h"
#include "CSMutex.h"

using namespace std;

class CSOutputStream;
class CSInputStream;

class CSSocket : public CSRefObject {
public:
	CSSocket() { }

	virtual ~CSSocket() { }

	CSOutputStream *getOutputStream();

	CSInputStream *getInputStream();

	virtual void formatAddress(size_t size, char *address) = 0;

	/*
	 * Publish a listener:
	 */
	virtual void publish(char *service, int default_port) = 0;

	/*
	 * Accept a connection from a listening socket:
	 */
	virtual void open(CSSocket *listener) = 0;

	/*
	 * Connect to a listening socket.
	 */
	virtual void open(char *address, int default_port) = 0;

	/*
	 * Close the socket.
	 */
	virtual void close() { }

	/*
	 * Read at least one byte from the socket.
	 * This function returns 0 on EOF.
	 * If the function returns at least
	 * one byte, then you must call the function
	 * again, there may be more data available.
	 *
	 * Note: Errors on the socket do not cause
	 * an exception!
	 */
	virtual size_t read(void *data, size_t size) = 0;

	/*
	 * Returns -1 on EOF!
	 * Otherwize it returns a character value >= 0
	 * Just like read, error on the socket do
	 * not throw an exception.
	 */
	virtual int read() = 0;

	/*
	 * Look at the next character in the file without
	 * taking from the input.
	 */
	virtual int peek() = 0;

	/*
	 * Write the given number of bytes.
	 * Throws IOException if an error occurs.
	 */
	virtual void write(const  void *data, size_t size) = 0;

	/*
	 * Write a character to the file.
	 */
	virtual void write(char ch) = 0;

	/*
	 * Flush the data written.
	 */
	virtual void flush() = 0;

	static CSSocket	*newSocket();

	friend class SCSocket;

private:
};

#define CS_SOCKET_ADDRESS_SIZE		300

class SCSocket : public CSSocket {
public:
	SCSocket(): CSSocket(), iHandle(-1), iHost(NULL), iService(NULL), iPort(0) { }

	virtual ~SCSocket() {
		close();
	}

	virtual void formatAddress(size_t size, char *address);

	virtual void publish(char *service, int default_port);

	virtual void open(CSSocket *listener);

	virtual void open(char *address, int default_port);

	virtual void close();

	virtual size_t read(void *data, size_t size);

	virtual int read();

	virtual int peek();

	virtual void write(const void *data, size_t size);

	virtual void write(char ch);

	virtual void flush();

private:
	void throwError(const char *func, const char *file, int line, char *address, int err);
	void throwError(const char *func, const char *file, int line, int err);
	void setInternalOptions();
	void openInternal();

	int iHandle;
	char *iHost;
	char *iService;
	int iPort;
};

#endif