Ejemplo n.º 1
0
		public override 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 = Array.IndexOf<byte> (this.network_data, (byte) 0xff);
					
					this.BufferStream.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.BufferStream.Length == 0) {
				this.BufferStream.Close ();
				throw new ResponseMessageException ("Socket was closed before any data could be read");
			}

			this.BufferStream.Seek (0, SeekOrigin.Begin);

#if ENABLE_XML_DUMP
			StreamReader dump_reader = new StreamReader (this.BufferStream);
			Logger.Log.Debug ("Received response:\n{0}\n", dump_reader.ReadToEnd ());
			this.BufferStream.Seek (0, SeekOrigin.Begin);
#endif
			
			ResponseMessage resp = null;

			try {
				ResponseWrapper wrapper = (ResponseWrapper)resp_serializer.Deserialize (this.BufferStream);
				resp = wrapper.Message;
			} catch (Exception e) {
				this.BufferStream.Seek (0, SeekOrigin.Begin);
				StreamReader r = new StreamReader (this.BufferStream);
				throw_me = new ResponseMessageException (e, "Exception while deserializing response", String.Format ("Message contents: '{0}'", r.ReadToEnd ()));
				this.BufferStream.Seek (0, SeekOrigin.Begin);
			}

			this.BufferStream.Close ();

			if (throw_me != null)
				throw throw_me;
			
			return resp;
		}
Ejemplo n.º 2
0
        public override 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 = Array.IndexOf <byte> (this.network_data, (byte)0xff);

                    this.BufferStream.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.BufferStream.Length == 0)
            {
                this.BufferStream.Close();
                throw new ResponseMessageException("Socket was closed before any data could be read");
            }

            this.BufferStream.Seek(0, SeekOrigin.Begin);

#if ENABLE_XML_DUMP
            StreamReader dump_reader = new StreamReader(this.BufferStream);
            Logger.Log.Debug("Received response:\n{0}\n", dump_reader.ReadToEnd());
            this.BufferStream.Seek(0, SeekOrigin.Begin);
#endif

            ResponseMessage resp = null;

            try {
                ResponseWrapper wrapper = (ResponseWrapper)resp_serializer.Deserialize(this.BufferStream);
                resp = wrapper.Message;
            } catch (Exception e) {
                this.BufferStream.Seek(0, SeekOrigin.Begin);
                StreamReader r = new StreamReader(this.BufferStream);
                throw_me = new ResponseMessageException(e, "Exception while deserializing response", String.Format("Message contents: '{0}'", r.ReadToEnd()));
                this.BufferStream.Seek(0, SeekOrigin.Begin);
            }

            this.BufferStream.Close();

            if (throw_me != null)
            {
                throw throw_me;
            }

            return(resp);
        }