Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 144 lines (121 sloc) 3.879 kb
b0ad2b2 @braddr phobos 0.86
braddr authored
1 /*
04e8517 tabs to spaces
Walter Bright authored
2 Copyright (C) 2004 Christopher E. Miller
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
b0ad2b2 @braddr phobos 0.86
braddr authored
19 */
20
f3114b3 @braddr phobos 0.155
braddr authored
21 /**************
22 * <b>SocketStream</b> is a stream for a blocking,
23 * connected <b>Socket</b>.
24 *
25 * For Win32 systems, link with <tt>ws2_32.lib</tt>.
26 *
27 * Example:
04e8517 tabs to spaces
Walter Bright authored
28 * See <tt>/dmd/samples/d/htmlget.d</tt>
f3114b3 @braddr phobos 0.155
braddr authored
29 * Authors: Christopher E. Miller
30 * References:
04e8517 tabs to spaces
Walter Bright authored
31 * $(LINK2 std_stream.html, std.stream)
8320534 add source annotations
Walter Bright authored
32 * Source: $(PHOBOSSRC std/_socketstream.d)
f3114b3 @braddr phobos 0.155
braddr authored
33 * Macros: WIKI=Phobos/StdSocketstream
34 */
b0ad2b2 @braddr phobos 0.86
braddr authored
35
36 module std.socketstream;
37
38 private import std.stream;
39 private import std.socket;
40
f3114b3 @braddr phobos 0.155
braddr authored
41 /**************
42 * <b>SocketStream</b> is a stream for a blocking,
43 * connected <b>Socket</b>.
44 */
b0ad2b2 @braddr phobos 0.86
braddr authored
45 class SocketStream: Stream
46 {
f3114b3 @braddr phobos 0.155
braddr authored
47 private:
04e8517 tabs to spaces
Walter Bright authored
48 Socket sock;
49
f3114b3 @braddr phobos 0.155
braddr authored
50 public:
51
04e8517 tabs to spaces
Walter Bright authored
52 /**
53 * Constructs a SocketStream with the specified Socket and FileMode flags.
54 */
55 this(Socket sock, FileMode mode)
56 {
57 if(mode & FileMode.In)
58 readable = true;
59 if(mode & FileMode.Out)
60 writeable = true;
61
62 this.sock = sock;
63 }
64
65 /**
66 * Uses mode <b>FileMode.In | FileMode.Out</b>.
67 */
68 this(Socket sock)
69 {
70 writeable = readable = true;
71 this.sock = sock;
72 }
73
74 /**
75 * Property to get the <b>Socket</b> that is being streamed.
76 */
77 Socket socket()
78 {
79 return sock;
80 }
81
82 /**
83 * Attempts to read the entire block, waiting if necessary.
84 */
85 override size_t readBlock(void* _buffer, size_t size)
86 {
87 ubyte* buffer = cast(ubyte*)_buffer;
88 assertReadable();
89
90 if (size == 0)
91 return size;
92
93 auto len = sock.receive(buffer[0 .. size]);
94 readEOF = cast(bool)(len == 0);
95 if (len == sock.ERROR)
96 len = 0;
97 return len;
98 }
99
100 /**
101 * Attempts to write the entire block, waiting if necessary.
102 */
103 override size_t writeBlock(void* _buffer, size_t size)
104 {
105 ubyte* buffer = cast(ubyte*)_buffer;
106 assertWriteable();
107
108 if (size == 0)
109 return size;
110
111 auto len = sock.send(buffer[0 .. size]);
112 readEOF = cast(bool)(len == 0);
113 if (len == sock.ERROR)
114 len = 0;
115 return len;
116 }
117
118 /**
119 *
120 */
121 override ulong seek(long offset, SeekPos whence)
122 {
123 throw new SeekException("Cannot seek a socket.");
124 }
125
126 /**
127 * Does not return the entire stream because that would
128 * require the remote connection to be closed.
129 */
130 override char[] toString()
131 {
132 return sock.toString();
133 }
134
135 /**
136 * Close the <b>Socket</b>.
137 */
138 override void close()
139 {
140 sock.close();
141 }
b0ad2b2 @braddr phobos 0.86
braddr authored
142 }
143
Something went wrong with that request. Please try again.