5 // Joe Shaw (joeshaw@novell.com)
7 // Copyright (C) 2004-2005 Novell, Inc.
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.
33 using System.Net.Sockets;
38 public class UnixListener : IDisposable {
44 void Init (UnixEndPoint ep)
48 string filename = ep.Filename;
49 if (File.Exists (filename)) {
50 Socket conn = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
54 throw new InvalidOperationException ("There's already a server listening on " + filename);
55 } catch (SocketException se) {
57 File.Delete (filename);
60 server = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
62 savedEP = server.LocalEndPoint;
65 public UnixListener (string path)
67 if (!Directory.Exists (Path.GetDirectoryName (path)))
68 Directory.CreateDirectory (Path.GetDirectoryName (path));
70 Init (new UnixEndPoint (path));
73 public UnixListener (UnixEndPoint localEndPoint)
75 if (localEndPoint == null)
76 throw new ArgumentNullException ("localendPoint");
81 public EndPoint LocalEndpoint {
82 get { return savedEP; }
85 protected Socket Server {
86 get { return server; }
89 public Socket AcceptSocket ()
93 throw new InvalidOperationException ("Socket is not listening");
95 return server.Accept ();
98 public UnixClient AcceptUnixClient ()
102 throw new InvalidOperationException ("Socket is not listening");
104 return new UnixClient (AcceptSocket ());
112 public bool Pending ()
116 throw new InvalidOperationException ("Socket is not listening");
118 return server.Poll (1000, SelectMode.SelectRead);
126 public void Start (int backlog)
132 server.Listen (backlog);
142 public void Dispose ()
145 GC.SuppressFinalize (this);
148 protected void Dispose (bool disposing)
163 void CheckDisposed ()
166 throw new ObjectDisposedException (GetType().FullName);
169 static void Cleanup (UnixEndPoint ep)
171 string path = ((UnixEndPoint) ep).Filename;
172 if (File.Exists (path))