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 : MarshalByRefObject, IDisposable {
44 void Init (UnixEndPoint ep)
47 string filename = ep.Filename;
48 if (File.Exists (filename)) {
49 Socket conn = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
53 throw new InvalidOperationException ("There's already a server listening on " + filename);
54 } catch (SocketException se) {
56 File.Delete (filename);
59 server = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
61 savedEP = server.LocalEndPoint;
64 public UnixListener (string path)
66 if (!Directory.Exists (Path.GetDirectoryName (path)))
67 Directory.CreateDirectory (Path.GetDirectoryName (path));
69 Init (new UnixEndPoint (path));
72 public UnixListener (UnixEndPoint localEndPoint)
74 if (localEndPoint == null)
75 throw new ArgumentNullException ("localendPoint");
80 public EndPoint LocalEndpoint {
81 get { return savedEP; }
84 protected Socket Server {
85 get { return server; }
88 public Socket AcceptSocket ()
92 throw new InvalidOperationException ("Socket is not listening");
94 return server.Accept ();
97 public UnixClient AcceptUnixClient ()
101 throw new InvalidOperationException ("Socket is not listening");
103 return new UnixClient (AcceptSocket ());
111 public bool Pending ()
115 throw new InvalidOperationException ("Socket is not listening");
117 return server.Poll (1000, SelectMode.SelectRead);
125 public void Start (int backlog)
131 server.Listen (backlog);
141 public void Dispose ()
144 GC.SuppressFinalize (this);
147 protected void Dispose (bool disposing)
162 void CheckDisposed ()
165 throw new ObjectDisposedException (GetType().FullName);