New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / UnixClient.cs
bloba01c771b13a6a232852d75028b96e8cc1381bff7
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 using Mono.Posix;
38 namespace Beagle.Util {
40 public class UnixClient : IDisposable {
41 NetworkStream stream;
42 Socket client;
43 bool disposed;
45 public UnixClient ()
47 if (client != null) {
48 client.Close ();
49 client = null;
52 client = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
55 public UnixClient (string path) : this ()
57 if (path == null)
58 throw new ArgumentNullException ("ep");
60 Connect (path);
63 public UnixClient (UnixEndPoint ep) : this ()
65 if (ep == null)
66 throw new ArgumentNullException ("ep");
68 Connect (ep);
71 // UnixListener uses this when accepting a connection.
72 internal UnixClient (Socket sock)
74 Client = sock;
77 protected Socket Client {
78 get { return client; }
79 set {
80 client = value;
81 stream = null;
85 public PeerCred PeerCredential {
86 get {
87 CheckDisposed ();
88 return new PeerCred (client);
92 public LingerOption LingerState {
93 get {
94 CheckDisposed ();
95 return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
96 SocketOptionName.Linger);
99 set {
100 CheckDisposed ();
101 client.SetSocketOption (SocketOptionLevel.Socket,
102 SocketOptionName.Linger, value);
106 public int ReceiveBufferSize {
107 get {
108 CheckDisposed ();
109 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
110 SocketOptionName.ReceiveBuffer);
113 set {
114 CheckDisposed ();
115 client.SetSocketOption (SocketOptionLevel.Socket,
116 SocketOptionName.ReceiveBuffer, value);
120 public int ReceiveTimeout {
121 get {
122 CheckDisposed ();
123 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
124 SocketOptionName.ReceiveTimeout);
127 set {
128 CheckDisposed ();
129 client.SetSocketOption (SocketOptionLevel.Socket,
130 SocketOptionName.ReceiveTimeout, value);
134 public int SendBufferSize {
135 get {
136 CheckDisposed ();
137 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
138 SocketOptionName.SendBuffer);
141 set {
142 CheckDisposed ();
143 client.SetSocketOption (SocketOptionLevel.Socket,
144 SocketOptionName.SendBuffer, value);
148 public int SendTimeout {
149 get {
150 CheckDisposed ();
151 return (int) client.GetSocketOption (SocketOptionLevel.Socket,
152 SocketOptionName.SendTimeout);
155 set {
156 CheckDisposed ();
157 client.SetSocketOption (SocketOptionLevel.Socket,
158 SocketOptionName.SendTimeout, value);
162 public void Close ()
164 CheckDisposed ();
165 Dispose ();
168 public void Connect (UnixEndPoint remoteEndPoint)
170 CheckDisposed ();
171 client.Connect (remoteEndPoint);
172 stream = new NetworkStream (client, true);
175 public void Connect (string path)
177 CheckDisposed ();
178 Connect (new UnixEndPoint (path));
181 public void Dispose ()
183 Dispose (true);
184 GC.SuppressFinalize (this);
187 protected virtual void Dispose (bool disposing)
189 if (disposed)
190 return;
192 if (disposing) {
193 // release managed resources
194 NetworkStream s = stream;
195 stream = null;
196 if (s != null) {
197 // This closes the socket as well, as the NetworkStream
198 // owns the socket.
199 s.Close();
200 s = null;
201 } else if (client != null){
202 client.Close ();
204 client = null;
207 disposed = true;
210 public NetworkStream GetStream ()
212 CheckDisposed ();
213 if (stream == null)
214 stream = new NetworkStream (client, true);
216 return stream;
219 void CheckDisposed ()
221 if (disposed)
222 throw new ObjectDisposedException (GetType().FullName);
225 ~UnixClient ()
227 Dispose (false);