2008-09-30 Michael Hutchinson <mhutchinson@novell.com>
[mono-project.git] / mcs / class / Mono.Posix / Mono.Unix / UnixClient.cs
blob7bebf00e84a88ae415c12016463a385a6d61e167
1 //
2 // UnixListener.cs
3 //
4 // Authors:
5 // Joe Shaw (joeshaw@novell.com)
6 //
7 // Copyright (C) 2004-2005 Novell, Inc.
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.IO;
32 using System.Net;
33 using System.Net.Sockets;
34 using System.Runtime.InteropServices;
36 namespace Mono.Unix {
38 public class UnixClient : MarshalByRefObject, IDisposable {
39 NetworkStream stream;
40 Socket client;
41 bool disposed;
43 public UnixClient ()
45 if (client != null) {
46 client.Close ();
47 client = null;
50 client = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
53 public UnixClient (string path) : this ()
55 if (path == null)
56 throw new ArgumentNullException ("ep");
58 Connect (path);
61 public UnixClient (UnixEndPoint ep) : this ()
63 if (ep == null)
64 throw new ArgumentNullException ("ep");
66 Connect (ep);
69 // UnixListener uses this when accepting a connection.
70 internal UnixClient (Socket sock)
72 Client = sock;
75 protected Socket Client {
76 get { return client; }
77 set {
78 client = value;
79 stream = null;
83 public PeerCred PeerCredential {
84 get {
85 CheckDisposed ();
86 return new PeerCred (client);
90 public LingerOption LingerState {
91 get {
92 CheckDisposed ();
93 return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
94 SocketOptionName.Linger);
97 set {
98 CheckDisposed ();
99 client.SetSocketOption (SocketOptionLevel.Socket,
100 SocketOptionName.Linger, value);
104 public int ReceiveBufferSize {
105 get {
106 CheckDisposed ();
107 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
108 SocketOptionName.ReceiveBuffer);
111 set {
112 CheckDisposed ();
113 client.SetSocketOption (SocketOptionLevel.Socket,
114 SocketOptionName.ReceiveBuffer, value);
118 public int ReceiveTimeout {
119 get {
120 CheckDisposed ();
121 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
122 SocketOptionName.ReceiveTimeout);
125 set {
126 CheckDisposed ();
127 client.SetSocketOption (SocketOptionLevel.Socket,
128 SocketOptionName.ReceiveTimeout, value);
132 public int SendBufferSize {
133 get {
134 CheckDisposed ();
135 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
136 SocketOptionName.SendBuffer);
139 set {
140 CheckDisposed ();
141 client.SetSocketOption (SocketOptionLevel.Socket,
142 SocketOptionName.SendBuffer, value);
146 public int SendTimeout {
147 get {
148 CheckDisposed ();
149 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
150 SocketOptionName.SendTimeout);
153 set {
154 CheckDisposed ();
155 client.SetSocketOption (SocketOptionLevel.Socket,
156 SocketOptionName.SendTimeout, value);
160 public void Close ()
162 CheckDisposed ();
163 Dispose ();
166 public void Connect (UnixEndPoint remoteEndPoint)
168 CheckDisposed ();
169 client.Connect (remoteEndPoint);
170 stream = new NetworkStream (client, true);
173 public void Connect (string path)
175 CheckDisposed ();
176 Connect (new UnixEndPoint (path));
179 public void Dispose ()
181 Dispose (true);
182 GC.SuppressFinalize (this);
185 protected virtual void Dispose (bool disposing)
187 if (disposed)
188 return;
190 if (disposing) {
191 // release managed resources
192 NetworkStream s = stream;
193 stream = null;
194 if (s != null) {
195 // This closes the socket as well, as the NetworkStream
196 // owns the socket.
197 s.Close();
198 s = null;
199 } else if (client != null){
200 client.Close ();
202 client = null;
205 disposed = true;
208 public NetworkStream GetStream ()
210 CheckDisposed ();
211 if (stream == null)
212 stream = new NetworkStream (client, true);
214 return stream;
217 void CheckDisposed ()
219 if (disposed)
220 throw new ObjectDisposedException (GetType().FullName);
223 ~UnixClient ()
225 Dispose (false);