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