2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Unix / UnixClient.cs
bloba51a3223eee593706e15380dd0105a39dc5c81fa
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 #if NET_2_0
76 public
77 #else
78 protected
79 #endif
80 Socket Client {
81 get { return client; }
82 set {
83 client = value;
84 stream = null;
88 public PeerCred PeerCredential {
89 get {
90 CheckDisposed ();
91 return new PeerCred (client);
95 public LingerOption LingerState {
96 get {
97 CheckDisposed ();
98 return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
99 SocketOptionName.Linger);
102 set {
103 CheckDisposed ();
104 client.SetSocketOption (SocketOptionLevel.Socket,
105 SocketOptionName.Linger, value);
109 public int ReceiveBufferSize {
110 get {
111 CheckDisposed ();
112 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
113 SocketOptionName.ReceiveBuffer);
116 set {
117 CheckDisposed ();
118 client.SetSocketOption (SocketOptionLevel.Socket,
119 SocketOptionName.ReceiveBuffer, value);
123 public int ReceiveTimeout {
124 get {
125 CheckDisposed ();
126 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
127 SocketOptionName.ReceiveTimeout);
130 set {
131 CheckDisposed ();
132 client.SetSocketOption (SocketOptionLevel.Socket,
133 SocketOptionName.ReceiveTimeout, value);
137 public int SendBufferSize {
138 get {
139 CheckDisposed ();
140 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
141 SocketOptionName.SendBuffer);
144 set {
145 CheckDisposed ();
146 client.SetSocketOption (SocketOptionLevel.Socket,
147 SocketOptionName.SendBuffer, value);
151 public int SendTimeout {
152 get {
153 CheckDisposed ();
154 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
155 SocketOptionName.SendTimeout);
158 set {
159 CheckDisposed ();
160 client.SetSocketOption (SocketOptionLevel.Socket,
161 SocketOptionName.SendTimeout, value);
165 public void Close ()
167 CheckDisposed ();
168 Dispose ();
171 public void Connect (UnixEndPoint remoteEndPoint)
173 CheckDisposed ();
174 client.Connect (remoteEndPoint);
175 stream = new NetworkStream (client, true);
178 public void Connect (string path)
180 CheckDisposed ();
181 Connect (new UnixEndPoint (path));
184 public void Dispose ()
186 Dispose (true);
187 GC.SuppressFinalize (this);
190 protected virtual void Dispose (bool disposing)
192 if (disposed)
193 return;
195 if (disposing) {
196 // release managed resources
197 NetworkStream s = stream;
198 stream = null;
199 if (s != null) {
200 // This closes the socket as well, as the NetworkStream
201 // owns the socket.
202 s.Close();
203 s = null;
204 } else if (client != null){
205 client.Close ();
207 client = null;
210 disposed = true;
213 public NetworkStream GetStream ()
215 CheckDisposed ();
216 if (stream == null)
217 stream = new NetworkStream (client, true);
219 return stream;
222 void CheckDisposed ()
224 if (disposed)
225 throw new ObjectDisposedException (GetType().FullName);
228 ~UnixClient ()
230 Dispose (false);