view Pml/Channels/TCPPmlChannel.cs @ 54:ba4e2cb031e0

Added general purpose tar archive reader class
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 02 Oct 2013 21:17:30 +0200
parents 3ab940a0c7a0
children 8fe322656807
line wrap: on
line source

???using System;
using System.IO;
using System.Net.Sockets;
using UCIS.Net;

namespace UCIS.Pml {
	public class TCPPmlChannel : PassivePmlChannel {
		private TCPStream _socket;
		private IPmlRW _rw;
		private bool _open = false;

		public TCPPmlChannel(Socket socket) : this(new TCPStream(socket)) { }
		public TCPPmlChannel(TCPStream socket) {
			if (socket == null) throw new ArgumentNullException("socket");
			_socket = socket;
			_rw = new PmlBinaryRW(_socket);
			_open = true;
			//ThreadPool.RunTask(worker, null);
		}

		public override void WriteMessage(PmlElement message) {
			if (!_open) throw new InvalidOperationException("The channel is not open");
			lock (_rw) _rw.WriteMessage(message);
		}

		public override void Close() {
			if (!_open) return;
			_open = false;
			if (_socket != null) try { _socket.Close(); } catch { }
			base.Close();
		}

		public override PmlElement ReadMessage() {
			return _rw.ReadMessage();
		}

		/*private void worker(Object state) {
			try {
				while (_open) {
					base.PushReceivedMessage(_rw.ReadMessage());
				}
			} catch (Exception ex) {
				Console.WriteLine(ex.ToString());
			} finally {
				Close();
			}
		}*/
	}
}