2 // Microsoft.Web.Services.Messaging.SoapTcpChannel.cs
4 // Author: Todd Berman <tberman@gentoo.org>
6 // (C) 2003 Todd Berman
10 using System.Net.Sockets;
12 namespace Microsoft.Web.Services.Messaging
15 public class SoapTcpChannel : SoapChannel
18 private bool _active = false;
19 private bool _disposed = false;
20 private AddressFamily _addrFam = AddressFamily.InterNetwork;
21 private DateTime _lastActivity = DateTime.Now;
22 private int _port = 0;
23 private Socket _socket = null;
24 private NetworkStream _stream = null;
25 private Uri _destination = null;
26 private string _hostname = null;
28 public SoapTcpChannel (Socket sock, ISoapFormatter format) : base (format)
31 _stream = new NetworkStream (sock, false);
34 IPEndPoint ep = sock.RemoteEndPoint as IPEndPoint;
36 _destination = new Uri ("soap.tcp://" + ep.Address.ToString () + ":" + ep.Port);
39 public SoapTcpChannel (Uri uri, ISoapFormatter format) : base (format)
42 throw new ArgumentNullException ("to");
44 if(uri.Scheme != "soap.tcp") {
45 throw new ArgumentException ("Invalid Scheme");
48 _port = uri.Port < 0 ? 8081 : uri.Port;
51 public override void Close ()
56 if(_socket != null || !_socket.Connected) {
65 public void Connect ()
68 throw new ObjectDisposedException (GetType ().FullName);
77 IPHostEntry host = Dns.Resolve (_hostname);
79 IPAddress[] ip_addrs = host.AddressList;
81 Exception exception = null;
83 for (int i = 0; i < ip_addrs.Length; i++) {
84 IPAddress addy = ip_addrs[i];
86 _addrFam = addy.AddressFamily;
87 _socket = new Socket (_addrFam, SocketType.Stream, ProtocolType.Tcp);
91 Connect ( new IPEndPoint (addy, _port) );
93 } catch (Exception e) {
100 if(_active == false) {
101 if(exception != null) {
105 throw new Exception ("Not Connected");
107 _stream = new NetworkStream (_socket, false);
111 public void Connect (IPEndPoint endpoint)
114 throw new ObjectDisposedException (GetType ().FullName);
116 if(endpoint == null) {
117 throw new ArgumentNullException ("endpoint");
120 _socket.Connect (endpoint);
122 UpdateLastActivity ();
128 if(_active == false) {
135 public override SoapEnvelope Receive ()
141 SoapEnvelope env = DeserializeMessage (_stream);
144 env.Context.Channel = this;
147 UpdateLastActivity ();
152 public override void Send (SoapEnvelope env)
158 SerializeMessage (env, _stream);
159 UpdateLastActivity ();
163 public void UpdateLastActivity ()
166 _lastActivity = DateTime.Now;
170 public override bool Active {
171 get { return _active; }
174 public Uri Destination {
175 get { return _destination; }
178 public DateTime LastActivity {
179 get { return _lastActivity; }
182 public override string Scheme {
183 get { return "soap.tcp"; }