PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/io/socket.d

http://github.com/wilkie/djehuty
D | 234 lines | 134 code | 76 blank | 24 comment | 16 complexity | 046ceaa6069e667bcbc006a8ce4f7442 MD5 | raw file
  1module io.socket;
  2
  3import core.stream;
  4import core.string;
  5import core.definitions;
  6
  7import platform.vars.socket;
  8
  9import scaffold.socket;
 10
 11// Section: Core/Streams
 12
 13// Description: This class wraps networking calls and represents the information stream as a Stream class.  This is a low-level implementation of a socket.  Note: no rewind or seek operations will have any affect.
 14class Socket : Stream {
 15	~this() {
 16		close();
 17	}
 18
 19	// Methods
 20
 21	// Inherited Functionality
 22
 23	alias Stream.write write;
 24	alias Stream.append append;
 25	alias Stream.read read;
 26
 27	// Core Functionality
 28
 29	// Description: Will open a connection with the host on the port given by the parameters.
 30	// hostname: The name of the host to connect.
 31	// port: The port to connect through.
 32	// Returns: Will return true when the connect is made and false if the connection cannot be made.
 33    bool connect(string hostname, ushort port) {
 34        _hostname = hostname.dup;
 35        _port = port;
 36
 37        _pos = null;
 38        _curpos = 0;
 39        bool r = SocketOpen(_pfvars, _hostname, port);
 40
 41        if (!r) {
 42            return false;
 43        }
 44
 45        _inited = true;
 46
 47        return true;
 48    }
 49
 50	// Description: Binds to a port, causes the socket to act as a server.
 51	// port: The port to listen for connection requests.
 52	// Returns: Will return false on failure.
 53    bool bind(ushort port) {
 54        _hostname = null;
 55        _port = port;
 56
 57        _pos = null;
 58        _curpos = 0;
 59        bool r = SocketBind(_pfvars, port);
 60
 61        if (!r) {
 62            return false;
 63        }
 64
 65        _inited = true;
 66
 67        return true;
 68    }
 69
 70	// Description: Will listen to a binded port.  Use bind() prior to this.  It will not return until a connection is requested from a client.
 71	// Returns: Will return false on failure.
 72    bool listen() {
 73		return SocketListen(_pfvars);
 74    }
 75
 76	// Description: Will accept a connection request from a client.  Do this after returning from a Listen() call without failure.
 77	// Returns: Will return false on failure.
 78    bool accept() {
 79		return SocketAccept(_pfvars);
 80    }
 81
 82	// Description: Will close the connection, if open.  This is also done upon deconstruction of the class, for instance when it is garbage collected.
 83    void close() {
 84		if (_inited) {
 85	        SocketClose(_pfvars);
 86
 87	        _inited = false;
 88	        _hostname = null;
 89	    }
 90    }
 91
 92
 93    // read
 94	override bool read(void* buffer, uint len) {
 95		return SocketRead(_pfvars, cast(ubyte*)buffer, len);
 96	}
 97
 98	override bool read(Stream stream, uint len) {
 99		if (_curpos + len > _length) {
100			return false;
101		}
102
103		stream.write(this, len);
104
105		_curpos += len;
106
107		return true;
108	}
109
110	override ulong readAny(void* buffer, uint len) {
111		if (len == 0) { return 0; }
112
113		return SocketReadAvailable(_pfvars, cast(ubyte*)buffer, len);
114	}
115
116	override ulong readAny(Stream stream, uint len) {
117		if (len == 0) { return 0; }
118
119		ubyte buffer[] = new ubyte[len];
120
121		len = cast(uint)SocketReadAvailable(_pfvars, buffer.ptr, len);
122
123		if (len != 0) {
124			stream.write(buffer.ptr, len);
125		}
126
127		return len;
128	}
129
130
131
132    // write
133
134	override bool write(ubyte* bytes, uint len) {
135		if (len <= 0) { return false;}
136
137		SocketWrite(_pfvars, bytes, len);
138
139		return true;
140	}
141
142	override bool write(Stream stream, uint len) {
143		if (len <= 0) { return false;}
144
145		ubyte buffer[] = new ubyte[len];
146
147		stream.read(&buffer[0], len);
148		SocketWrite(_pfvars, &buffer[0], len);
149
150		return true;
151	}
152
153
154
155    // append
156
157	override bool append(ubyte* bytes, uint len) {
158		if (len <= 0) { return false;}
159
160		SocketWrite(_pfvars, bytes, len);
161
162		return true;
163	}
164
165	override bool append(Stream stream, uint len) {
166		if (len <= 0) { return false;}
167
168		ubyte buffer[] = new ubyte[len];
169
170		stream.read(&buffer[0], len);
171		SocketWrite(_pfvars, &buffer[0], len);
172
173		return true;
174	}
175
176	// rewind
177
178	override void rewind() {
179	}
180
181	override bool rewind(ulong amount) {
182		return true;
183	}
184
185	override ulong rewindAny(ulong amount) {
186		return amount;
187	}
188
189
190	// skip
191
192	override void skip() {
193	}
194
195	override bool skip(ulong amount) {
196		return true;
197	}
198
199	override ulong skipAny(ulong amount) {
200		return amount;
201	}
202
203	// Description: Will return the String representing the host currently open, or null for when there is no open socket.
204	// Returns: The String of the host.
205    string hostname() {
206		if (_inited) {
207	        return _hostname.dup;
208	    }
209
210		return null;
211	}
212
213	ulong port() {
214		if (_inited) { return 0; }
215
216		return _port;
217    }
218
219protected:
220
221    bool _inited = false;
222    string _hostname = null;
223
224    ulong _port = 0;
225
226    SocketPlatformVars _pfvars;
227
228}
229
230class SocketReader : Socket {
231}
232
233class SocketWriter : Socket {
234}