1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
//
// Client.cs
//
// Copyright (C) 2005 Novell, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Xml.Serialization;
using Mono.Unix;
using GLib;
using Beagle.Util;
namespace Beagle {
internal class Client {
private class EventThrowingClosure {
private Client client;
private ResponseMessage response;
public EventThrowingClosure (Client client, ResponseMessage response)
{
this.client = client;
this.response = response;
}
public bool ThrowEvent ()
{
if (this.client.AsyncResponseEvent != null)
this.client.AsyncResponseEvent (this.response);
return false;
}
}
private string socket_name;
private UnixClient client;
private byte[] network_data = new byte [4096];
private MemoryStream buffer_stream = new MemoryStream ();
private bool closed = false;
public delegate void AsyncResponse (ResponseMessage response);
public event AsyncResponse AsyncResponseEvent;
public delegate void Closed ();
public event Closed ClosedEvent;
public Client (string client_name)
{
// use the default socket name when passed null
if (client_name == null)
client_name = "socket";
string storage_dir = PathFinder.GetRemoteStorageDir (false);
if (storage_dir == null)
throw new System.Net.Sockets.SocketException ();
this.socket_name = Path.Combine (storage_dir, client_name);
}
public Client () : this (null)
{
}
~Client ()
{
Close ();
}
public void Close ()
{
bool previously_closed = this.closed;
// Important to set this before we close the
// UnixClient, since that will trigger the
// ReadCallback() method, reading 0 bytes off the
// wire, and we check this.closed in there.
this.closed = true;
if (this.client != null)
this.client.Close ();
if (!previously_closed && this.ClosedEvent != null)
this.ClosedEvent ();
}
static XmlSerializer req_serializer = new XmlSerializer (typeof (RequestWrapper), RequestMessage.Types);
private void SendRequest (RequestMessage request)
{
this.client = new UnixClient (this.socket_name);
NetworkStream stream = this.client.GetStream ();
// The socket may be shut down at some point here. It
// is the caller's responsibility to handle the error
// correctly.
#if ENABLE_XML_DUMP
MemoryStream mem_stream = new MemoryStream ();
XmlFu.SerializeUtf8 (req_serializer, mem_stream, new RequestWrapper (request));
mem_stream.Seek (0, SeekOrigin.Begin);
StreamReader r = new StreamReader (mem_stream);
Logger.Log.Debug ("Sending request:\n{0}\n", r.ReadToEnd ());
mem_stream.Seek (0, SeekOrigin.Begin);
mem_stream.WriteTo (stream);
mem_stream.Close ();
#else
XmlFu.SerializeUtf8 (req_serializer, stream, new RequestWrapper (request));
#endif
// Send end of message marker
stream.WriteByte (0xff);
stream.Flush ();
}
static XmlSerializer resp_serializer = new XmlSerializer (typeof (ResponseWrapper), ResponseMessage.Types);
// This function will be called from its own thread
private void ReadCallback (IAsyncResult ar)
{
if (this.closed)
return;
try {
NetworkStream stream = this.client.GetStream ();
int bytes_read = 0;
try {
bytes_read = stream.EndRead (ar);
} catch (SocketException) {
Logger.Log.Debug ("Caught SocketException in ReadCallback");
} catch (IOException) {
Logger.Log.Debug ("Caught IOException in ReadCallback");
}
// Connection hung up, we're through
if (bytes_read == 0) {
this.Close ();
return;
}
int end_index = -1;
int prev_index = 0;
do {
// 0xff signifies end of message
end_index = ArrayFu.IndexOfByte (this.network_data, (byte) 0xff, prev_index);
this.buffer_stream.Write (this.network_data, prev_index, (end_index == -1 ? bytes_read : end_index) - prev_index);
if (end_index != -1) {
MemoryStream deserialize_stream = this.buffer_stream;
this.buffer_stream = new MemoryStream ();
deserialize_stream.Seek (0, SeekOrigin.Begin);
#if ENABLE_XML_DUMP
StreamReader r = new StreamReader (deserialize_stream);
Logger.Log.Debug ("Received response:\n{0}\n", r.ReadToEnd ());
deserialize_stream.Seek (0, SeekOrigin.Begin);
#endif
ResponseWrapper wrapper;
wrapper = (ResponseWrapper) resp_serializer.Deserialize (deserialize_stream);
ResponseMessage resp = wrapper.Message;
deserialize_stream.Close ();
// Run the handler in an idle handler
// so that events are thrown in the
// main thread instead of this inferior
// helper thread.
EventThrowingClosure closure = new EventThrowingClosure (this, resp);
GLib.Idle.Add (new IdleHandler (closure.ThrowEvent));
// Move past the end-of-message marker
prev_index = end_index + 1;
}
} while (end_index != -1);
// Check to see if we're still connected, and keep
// looking for new data if so.
if (!this.closed)
BeginRead ();
} catch (Exception e) {
Logger.Log.Error (e, "Got an exception while trying to read data:");
}
}
private void BeginRead ()
{
NetworkStream stream = this.client.GetStream ();
Array.Clear (this.network_data, 0, this.network_data.Length);
stream.BeginRead (this.network_data, 0, this.network_data.Length,
new AsyncCallback (ReadCallback), null);
}
public void SendAsync (RequestMessage request)
{
Exception ex = null;
try {
SendRequest (request);
} catch (IOException e) {
ex = e;
} catch (SocketException e) {
ex = e;
}
if (ex != null) {
ResponseMessage resp = new ErrorResponse (ex);
if (this.AsyncResponseEvent != null)
this.AsyncResponseEvent (resp);
} else
BeginRead ();
}
public void SendAsyncBlocking (RequestMessage request)
{
Exception ex = null;
try {
SendRequest (request);
} catch (IOException e) {
ex = e;
} catch (SocketException e) {
ex = e;
}
if (ex != null) {
ResponseMessage resp = new ErrorResponse (ex);
if (this.AsyncResponseEvent != null)
this.AsyncResponseEvent (resp);
return;
}
NetworkStream stream = this.client.GetStream ();
MemoryStream deserialize_stream = new MemoryStream ();
// This buffer is annoyingly small on purpose, to avoid
// having to deal with the case of multiple messages
// in a single block.
byte [] buffer = new byte [32];
while (! this.closed) {
Array.Clear (buffer, 0, buffer.Length);
int bytes_read;
bytes_read = stream.Read (buffer, 0, buffer.Length);
if (bytes_read == 0)
break;
int end_index;
end_index = ArrayFu.IndexOfByte (buffer, (byte) 0xff);
if (end_index == -1) {
deserialize_stream.Write (buffer, 0, bytes_read);
} else {
deserialize_stream.Write (buffer, 0, end_index);
deserialize_stream.Seek (0, SeekOrigin.Begin);
ResponseMessage resp;
try {
ResponseWrapper wrapper;
wrapper = (ResponseWrapper) resp_serializer.Deserialize (deserialize_stream);
resp = wrapper.Message;
} catch (Exception e) {
resp = new ErrorResponse (e);
}
if (this.AsyncResponseEvent != null)
this.AsyncResponseEvent (resp);
deserialize_stream.Close ();
deserialize_stream = new MemoryStream ();
if (bytes_read - end_index - 1 > 0)
deserialize_stream.Write (buffer, end_index+1, bytes_read-end_index-1);
}
}
}
public ResponseMessage Send (RequestMessage request)
{
if (request.Keepalive)
throw new Exception ("A blocking connection on a keepalive request is not allowed");
Exception throw_me = null;
try {
SendRequest (request);
} catch (IOException e) {
throw_me = e;
} catch (SocketException e) {
throw_me = e;
}
if (throw_me != null)
throw new ResponseMessageException (throw_me);
NetworkStream stream = this.client.GetStream ();
int bytes_read, end_index = -1;
do {
bytes_read = stream.Read (this.network_data, 0, 4096);
//Logger.Log.Debug ("Read {0} bytes", bytes_read);
if (bytes_read > 0) {
// 0xff signifies end of message
end_index = ArrayFu.IndexOfByte (this.network_data, (byte) 0xff);
this.buffer_stream.Write (this.network_data, 0,
end_index == -1 ? bytes_read : end_index);
}
} while (bytes_read > 0 && end_index == -1);
// It's possible that the server side shut down the
// connection before we had a chance to read any data.
// If this is the case, throw a rather descriptive
// exception.
if (this.buffer_stream.Length == 0) {
this.buffer_stream.Close ();
throw new ResponseMessageException ("Socket was closed before any data could be read");
}
this.buffer_stream.Seek (0, SeekOrigin.Begin);
ResponseMessage resp = null;
try {
ResponseWrapper wrapper;
wrapper = (ResponseWrapper) resp_serializer.Deserialize (this.buffer_stream);
resp = wrapper.Message;
} catch (Exception e) {
this.buffer_stream.Seek (0, SeekOrigin.Begin);
StreamReader r = new StreamReader (this.buffer_stream);
throw_me = new ResponseMessageException (e, "Exception while deserializing response", String.Format ("Message contents: '{0}'", r.ReadToEnd ()));
this.buffer_stream.Seek (0, SeekOrigin.Begin);
}
this.buffer_stream.Close ();
if (throw_me != null)
throw throw_me;
return resp;
}
}
}
|