| 1 |
// Copyright 2006 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.IO; |
| 7 |
using System.Net; |
| 8 |
using System.Net.Sockets; |
| 9 |
|
| 10 |
namespace NDesk.DBus.Transports |
| 11 |
{ |
| 12 |
class SocketTransport : Transport |
| 13 |
{ |
| 14 |
protected Socket socket; |
| 15 |
|
| 16 |
public override void Open (AddressEntry entry) |
| 17 |
{ |
| 18 |
string host, portStr; |
| 19 |
int port; |
| 20 |
|
| 21 |
if (!entry.Properties.TryGetValue ("host", out host)) |
| 22 |
throw new Exception ("No host specified"); |
| 23 |
|
| 24 |
if (!entry.Properties.TryGetValue ("port", out portStr)) |
| 25 |
throw new Exception ("No port specified"); |
| 26 |
|
| 27 |
if (!Int32.TryParse (portStr, out port)) |
| 28 |
throw new Exception ("Invalid port: \"" + port + "\""); |
| 29 |
|
| 30 |
Open (host, port); |
| 31 |
} |
| 32 |
|
| 33 |
public void Open (string host, int port) |
| 34 |
{ |
| 35 |
//TODO: use Socket directly |
| 36 |
TcpClient client = new TcpClient (host, port); |
| 37 |
Stream = client.GetStream (); |
| 38 |
} |
| 39 |
|
| 40 |
public void Open (Socket socket) |
| 41 |
{ |
| 42 |
this.socket = socket; |
| 43 |
|
| 44 |
socket.Blocking = true; |
| 45 |
SocketHandle = (long)socket.Handle; |
| 46 |
//Stream = new UnixStream ((int)socket.Handle); |
| 47 |
Stream = new NetworkStream (socket); |
| 48 |
} |
| 49 |
|
| 50 |
public override void WriteCred () |
| 51 |
{ |
| 52 |
Stream.WriteByte (0); |
| 53 |
} |
| 54 |
|
| 55 |
public override string AuthString () |
| 56 |
{ |
| 57 |
return String.Empty; |
| 58 |
} |
| 59 |
} |
| 60 |
} |