* UnixPath.cs: Add ReadLink() and TryReadLink() methods.
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixClient.cs
1 //
2 // UnixListener.cs
3 //
4 // Authors:
5 //      Joe Shaw (joeshaw@novell.com)
6 //
7 // Copyright (C) 2004-2005 Novell, Inc.
8 //
9
10 //
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:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
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.
28 //
29
30 using System;
31 using System.IO;
32 using System.Net;
33 using System.Net.Sockets;
34 using System.Runtime.InteropServices;
35
36 namespace Mono.Unix {
37
38         public class UnixClient : MarshalByRefObject, IDisposable {
39                 NetworkStream stream;
40                 Socket client;
41                 bool disposed;
42
43                 public UnixClient ()
44                 {
45                         if (client != null) {
46                                 client.Close ();
47                                 client = null;
48                         }
49
50                         client = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
51                 }
52
53                 public UnixClient (string path) : this ()
54                 {
55                         if (path == null)
56                                 throw new ArgumentNullException ("ep");
57
58                         Connect (path);
59                 }
60
61                 public UnixClient (UnixEndPoint ep) : this ()
62                 {
63                         if (ep == null)
64                                 throw new ArgumentNullException ("ep");
65
66                         Connect (ep);
67                 }
68
69                 // UnixListener uses this when accepting a connection.
70                 internal UnixClient (Socket sock)
71                 {
72                         Client = sock;
73                 }
74
75                 protected Socket Client {
76                         get { return client; }
77                         set {
78                                 client = value;
79                                 stream = null;
80                         }
81                 }
82
83                 public PeerCred PeerCredential {
84                         get {
85                                 CheckDisposed ();
86                                 return new PeerCred (client);
87                         }
88                 }
89         
90                 public LingerOption LingerState {
91                         get {
92                                 CheckDisposed ();
93                                 return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
94                                                                               SocketOptionName.Linger);
95                         }
96
97                         set {
98                                 CheckDisposed ();
99                                 client.SetSocketOption (SocketOptionLevel.Socket,
100                                                         SocketOptionName.Linger, value);
101                         }
102                 }
103
104                 public int ReceiveBufferSize {
105                         get {
106                                 CheckDisposed ();
107                                 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
108                                                                      SocketOptionName.ReceiveBuffer);
109                         }
110
111                         set {
112                                 CheckDisposed ();
113                                 client.SetSocketOption (SocketOptionLevel.Socket,
114                                                         SocketOptionName.ReceiveBuffer, value);
115                         }
116                 }
117             
118                 public int ReceiveTimeout {
119                         get {
120                                 CheckDisposed ();
121                                 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
122                                                                      SocketOptionName.ReceiveTimeout);
123                         }
124
125                         set {
126                                 CheckDisposed ();
127                                 client.SetSocketOption (SocketOptionLevel.Socket,
128                                                         SocketOptionName.ReceiveTimeout, value);
129                         }
130                 }
131         
132                 public int SendBufferSize {
133                         get {
134                                 CheckDisposed ();
135                                 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
136                                                                      SocketOptionName.SendBuffer);
137                         }
138
139                         set {
140                                 CheckDisposed ();
141                                 client.SetSocketOption (SocketOptionLevel.Socket,
142                                                         SocketOptionName.SendBuffer, value);
143                         }
144                 }
145         
146                 public int SendTimeout {
147                         get {
148                                 CheckDisposed ();
149                                 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
150                                                                      SocketOptionName.SendTimeout);
151                         }
152
153                         set {
154                                 CheckDisposed ();
155                                 client.SetSocketOption (SocketOptionLevel.Socket,
156                                                         SocketOptionName.SendTimeout, value);
157                         }
158                 }
159         
160                 public void Close ()
161                 {
162                         CheckDisposed ();
163                         Dispose ();
164                 }
165         
166                 public void Connect (UnixEndPoint remoteEndPoint)
167                 {
168                         CheckDisposed ();
169                         client.Connect (remoteEndPoint);
170                         stream = new NetworkStream (client, true);
171                 }
172         
173                 public void Connect (string path)
174                 {
175                         CheckDisposed ();
176                         Connect (new UnixEndPoint (path));
177                 }
178         
179                 public void Dispose ()
180                 {
181                         Dispose (true);
182                         GC.SuppressFinalize (this);
183                 }
184
185                 protected virtual void Dispose (bool disposing)
186                 {
187                         if (disposed)
188                                 return;
189
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 ();
201                                 }
202                                 client = null;
203                         }
204
205                         disposed = true;
206                 }
207
208                 public NetworkStream GetStream ()
209                 {
210                         CheckDisposed ();
211                         if (stream == null)
212                                 stream = new NetworkStream (client, true);
213
214                         return stream;
215                 }
216         
217                 void CheckDisposed ()
218                 {
219                         if (disposed)
220                                 throw new ObjectDisposedException (GetType().FullName);
221                 }        
222
223                 ~UnixClient ()
224                 {
225                         Dispose (false);
226                 }
227         }
228 }
229