removed unused variables
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Messaging / SoapTcpChannel.cs
1 //
2 // Microsoft.Web.Services.Messaging.SoapTcpChannel.cs
3 //
4 // Author: Todd Berman <tberman@gentoo.org>
5 //
6 // (C) 2003 Todd Berman
7
8 using System;
9 using System.Net;
10 using System.Net.Sockets;
11
12 namespace Microsoft.Web.Services.Messaging
13 {
14
15         public class SoapTcpChannel : SoapChannel
16         {
17
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;
27
28                 public SoapTcpChannel (Socket sock, ISoapFormatter format) : base (format)
29                 {
30                         _socket = sock;
31                         _stream = new NetworkStream (sock, false);
32                         _active = true;
33
34                         IPEndPoint ep = sock.RemoteEndPoint as IPEndPoint;
35
36                         _destination = new Uri ("soap.tcp://" + ep.Address.ToString () + ":" + ep.Port);
37                 }
38
39                 public SoapTcpChannel (Uri uri, ISoapFormatter format) : base (format)
40                 {
41                         if(uri == null) {
42                                 throw new ArgumentNullException ("to");
43                         }
44                         if(uri.Scheme != "soap.tcp") {
45                                 throw new ArgumentException ("Invalid Scheme");
46                         }
47                         _hostname = uri.Host;
48                         _port = uri.Port < 0 ? 8081 : uri.Port;
49                 }
50
51                 public override void Close ()
52                 {
53                         lock(SyncRoot) {
54                                 try {
55                                         _active = false;
56                                         if(_socket != null || !_socket.Connected) {
57                                                 _socket.Close ();
58                                         }
59                                 } catch {
60                                         _socket = null;
61                                 }
62                         }
63                 }
64                 
65                 public void Connect ()
66                 {
67                         if(_disposed) {
68                                 throw new ObjectDisposedException (GetType ().FullName);
69                         }
70                         if(_active) {
71                                 return;
72                         }
73                         lock(SyncRoot) {
74                                 if(_active) {
75                                         return;
76                                 }
77                                 IPHostEntry host = Dns.Resolve (_hostname);
78
79                                 IPAddress[] ip_addrs = host.AddressList;
80
81                                 Exception exception = null;
82
83                                 for (int i = 0; i < ip_addrs.Length; i++) {
84                                         IPAddress addy = ip_addrs[i];
85
86                                         _addrFam = addy.AddressFamily;
87                                         _socket = new Socket (_addrFam, SocketType.Stream, ProtocolType.Tcp);
88                                         _active = false;
89
90                                         try {
91                                                 Connect ( new IPEndPoint (addy, _port) );
92                                                 break;
93                                         } catch (Exception e) {
94                                                 _socket.Close ();
95                                                 _socket = null;
96                                                 exception = e;
97                                         }
98                                 }
99
100                                 if(_active == false) {
101                                         if(exception != null) {
102                                                 throw exception;
103                                         }
104
105                                         throw new Exception ("Not Connected");
106                                 }
107                                 _stream = new NetworkStream (_socket, false);
108                         }
109                 }
110
111                 public void Connect (IPEndPoint endpoint)
112                 {
113                         if(_disposed) {
114                                 throw new ObjectDisposedException (GetType ().FullName);
115                         }
116                         if(endpoint == null) {
117                                 throw new ArgumentNullException ("endpoint");
118                         }
119
120                         _socket.Connect (endpoint);
121                         _active = true;
122                         UpdateLastActivity ();
123                 }
124
125
126                 ~SoapTcpChannel ()
127                 {
128                         if(_active == false) {
129                                 Close ();
130                                 _disposed = true;
131                         }
132                 }
133
134
135                 public override SoapEnvelope Receive ()
136                 {
137                         if(!_active) {
138                                 Connect ();
139                         }
140
141                         SoapEnvelope env = DeserializeMessage (_stream);
142
143                         if(env != null) {
144                                 env.Context.Channel = this;
145                         }
146
147                         UpdateLastActivity ();
148                         
149                         return env;
150                 }
151
152                 public override void Send (SoapEnvelope env)
153                 {
154                         lock(SyncRoot) {
155                                 if(!_active) {
156                                         Connect ();
157                                 }
158                                 SerializeMessage (env, _stream);
159                                 UpdateLastActivity ();
160                         }
161                 }
162
163                 public void UpdateLastActivity ()
164                 {
165                         lock (SyncRoot) {
166                                 _lastActivity = DateTime.Now;
167                         }
168                 }
169
170                 public override bool Active {
171                         get { return _active; }
172                 }
173
174                 public Uri Destination {
175                         get { return _destination; }
176                 }
177
178                 public DateTime LastActivity {
179                         get { return _lastActivity; }
180                 }
181
182                 public override string Scheme {
183                         get { return "soap.tcp"; }
184                 }
185                 
186         }
187
188 }