view Pml/Channels/TCPPmlChannel.cs @ 0:3ab940a0c7a0

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 11 Sep 2012 16:28:53 +0200
parents
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();
			}
		}*/
	}
}