comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3ab940a0c7a0
1 using System;
2 using System.IO;
3 using System.Net.Sockets;
4 using UCIS.Net;
5
6 namespace UCIS.Pml {
7 public class TCPPmlChannel : PassivePmlChannel {
8 private TCPStream _socket;
9 private IPmlRW _rw;
10 private bool _open = false;
11
12 public TCPPmlChannel(Socket socket) : this(new TCPStream(socket)) { }
13 public TCPPmlChannel(TCPStream socket) {
14 if (socket == null) throw new ArgumentNullException("socket");
15 _socket = socket;
16 _rw = new PmlBinaryRW(_socket);
17 _open = true;
18 //ThreadPool.RunTask(worker, null);
19 }
20
21 public override void WriteMessage(PmlElement message) {
22 if (!_open) throw new InvalidOperationException("The channel is not open");
23 lock (_rw) _rw.WriteMessage(message);
24 }
25
26 public override void Close() {
27 if (!_open) return;
28 _open = false;
29 if (_socket != null) try { _socket.Close(); } catch { }
30 base.Close();
31 }
32
33 public override PmlElement ReadMessage() {
34 return _rw.ReadMessage();
35 }
36
37 /*private void worker(Object state) {
38 try {
39 while (_open) {
40 base.PushReceivedMessage(_rw.ReadMessage());
41 }
42 } catch (Exception ex) {
43 Console.WriteLine(ex.ToString());
44 } finally {
45 Close();
46 }
47 }*/
48 }
49 }