comparison Pml/Channels/PmlChannel.cs @ 0:3ab940a0c7a0

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 11 Sep 2012 16:28:53 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3ab940a0c7a0
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace UCIS.Pml {
6 public class PmlChannel : ActivePmlChannel {
7 private IPmlRW _rw;
8 private bool _open;
9
10 public PmlChannel(IPmlRW rw) {
11 if (rw == null) throw new ArgumentNullException("rw");
12 _rw = rw;
13 _open = true;
14 ThreadPool.RunTask(worker, null);
15 }
16
17 public IPmlReader Reader { get { return _rw; } }
18 public IPmlWriter Writer { get { return _rw; } }
19
20 public new bool IsOpen { get { return _open; } }
21
22 public override void WriteMessage(PmlElement message) {
23 if (!_open) throw new InvalidOperationException("The channel is not open");
24 lock (_rw) _rw.WriteMessage(message);
25 }
26
27 public override void Close() {
28 if (!_open) return;
29 _open = false;
30 if (_rw != null) _rw = null;
31 base.Close();
32 }
33
34 private void worker(Object state) {
35 try {
36 while (_open) {
37 base.PushReceivedMessage(_rw.ReadMessage());
38 }
39 } catch (System.Net.Sockets.SocketException ex) {
40 Console.WriteLine("SocketException in PmlChannel.worker: " + ex.Message);
41 } catch (System.IO.EndOfStreamException ex) {
42 Console.WriteLine("EndOfStreamException in PmlChannel.worker: " + ex.Message);
43 } catch (Exception ex) {
44 Console.WriteLine(ex.ToString());
45 } finally {
46 Close();
47 }
48 }
49 }
50 }