2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Unix / UnixListener.cs
blobbaab8a7ff363a8aaff766529399cf220c6064ead
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.
31 using System;
32 using System.Net;
33 using System.Net.Sockets;
34 using System.IO;
36 namespace Mono.Unix {
38 public class UnixListener : MarshalByRefObject, IDisposable {
39 bool disposed;
40 bool listening;
41 Socket server;
42 EndPoint savedEP;
44 void Init (UnixEndPoint ep)
46 listening = false;
47 string filename = ep.Filename;
48 if (File.Exists (filename)) {
49 Socket conn = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
50 try {
51 conn.Connect (ep);
52 conn.Close ();
53 throw new InvalidOperationException ("There's already a server listening on " + filename);
54 } catch (SocketException) {
56 File.Delete (filename);
59 server = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
60 server.Bind (ep);
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");
77 Init (localEndPoint);
80 public EndPoint LocalEndpoint {
81 get { return savedEP; }
84 protected Socket Server {
85 get { return server; }
88 public Socket AcceptSocket ()
90 CheckDisposed ();
91 if (!listening)
92 throw new InvalidOperationException ("Socket is not listening");
94 return server.Accept ();
97 public UnixClient AcceptUnixClient ()
99 CheckDisposed ();
100 if (!listening)
101 throw new InvalidOperationException ("Socket is not listening");
103 return new UnixClient (AcceptSocket ());
106 ~UnixListener ()
108 Dispose (false);
111 public bool Pending ()
113 CheckDisposed ();
114 if (!listening)
115 throw new InvalidOperationException ("Socket is not listening");
117 return server.Poll (1000, SelectMode.SelectRead);
120 public void Start ()
122 Start (5);
125 public void Start (int backlog)
127 CheckDisposed ();
128 if (listening)
129 return;
131 server.Listen (backlog);
132 listening = true;
135 public void Stop ()
137 CheckDisposed ();
138 Dispose (true);
141 public void Dispose ()
143 Dispose (true);
144 GC.SuppressFinalize (this);
147 protected void Dispose (bool disposing)
149 if (disposed)
150 return;
152 if (disposing) {
153 if (server != null)
154 server.Close ();
156 server = null;
159 disposed = true;
162 void CheckDisposed ()
164 if (disposed)
165 throw new ObjectDisposedException (GetType().FullName);