2 // TcpDuplexSessionChannel.cs
5 // Marcos Cobena (marcoscobena@gmail.com)
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
13 using System.Net.Sockets;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;
16 using System.ServiceModel.Channels;
19 namespace System.ServiceModel.Channels
21 internal class TcpDuplexSessionChannel : DuplexChannelBase, IDuplexSessionChannel
27 EndpointAddress local_address;
28 EndpointAddress remote_address;
29 TcpListener tcp_listener;
33 public TcpDuplexSessionChannel (ChannelFactoryBase factory, TcpChannelInfo info, EndpointAddress address, Uri via)
36 is_service_side = false;
38 remote_address = address;
42 public TcpDuplexSessionChannel (ChannelListenerBase listener, TcpChannelInfo info, TcpClient acceptedRequest, TimeSpan timeout)
45 is_service_side = true;
47 this.client = acceptedRequest;
48 this.timeout = timeout;
50 Stream s = client.GetStream ();
53 // Console.Write ("{0:X02} ", s.ReadByte ());
55 for (int i = 0; i < 6; i++)
58 int size = s.ReadByte ();
60 for (int i = 0; i < size; i++)
69 public MessageEncoder Encoder {
70 get { return info.MessageEncoder; }
73 public override EndpointAddress LocalAddress {
74 get { return local_address; }
77 public override EndpointAddress RemoteAddress {
78 get { return remote_address; }
82 public IDuplexSession Session {
83 get { throw new NotImplementedException (); }
86 public override Uri Via {
91 public override IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
93 throw new NotImplementedException ();
97 public override IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
99 throw new NotImplementedException ();
103 public override void EndSend (IAsyncResult result)
105 throw new NotImplementedException ();
109 public override void Send (Message message)
111 MemoryStream ms = new MemoryStream ();
112 BinaryFormatter bf = new BinaryFormatter ();
116 NetworkStream stream = client.GetStream ();
117 MyBinaryWriter bw = new MyBinaryWriter (stream);
119 Encoder.WriteMessage (message, ms);
120 bw.WriteBytes (ms.ToArray ());
124 stream.ReadByte (); // 7
133 public override void Send (Message message, TimeSpan timeout)
135 throw new NotImplementedException ();
139 public override IAsyncResult BeginReceive (AsyncCallback callback, object state)
141 throw new NotImplementedException ();
145 public override IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
147 throw new NotImplementedException ();
151 public override IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
153 throw new NotImplementedException ();
157 public override IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
159 throw new NotImplementedException ();
163 public override Message EndReceive (IAsyncResult result)
165 throw new NotImplementedException ();
169 public override bool EndTryReceive (IAsyncResult result, out Message message)
171 throw new NotImplementedException ();
175 public override bool EndWaitForMessage (IAsyncResult result)
177 throw new NotImplementedException ();
181 public override Message Receive ()
183 Stream s = client.GetStream ();
185 MyBinaryReader br = new MyBinaryReader (s);
186 // string msg = br.ReadString ();
187 // br.Read7BitEncodedInt ();
188 byte [] buffer = new byte [65536];
189 buffer = br.ReadBytes ();
190 MemoryStream ms = new MemoryStream ();
191 ms.Write (buffer, 0, buffer.Length);
192 ms.Seek (0, SeekOrigin.Begin);
195 // Console.Write ("{0:X02} ", s.ReadByte ());
198 // FIXME: To supply maxSizeOfHeaders.
199 msg = Encoder.ReadMessage (ms, 0x10000);
201 // Console.WriteLine (msg);
209 public override Message Receive (TimeSpan timeout)
211 throw new NotImplementedException ();
215 public override bool TryReceive (TimeSpan timeout, out Message message)
217 throw new NotImplementedException ();
221 public override bool WaitForMessage (TimeSpan timeout)
223 throw new NotImplementedException ();
226 // CommunicationObject
229 protected override void OnAbort ()
231 throw new NotImplementedException ();
235 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
236 AsyncCallback callback, object state)
238 throw new NotImplementedException ();
242 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
243 AsyncCallback callback, object state)
245 throw new NotImplementedException ();
249 protected override void OnClose (TimeSpan timeout)
255 protected override void OnEndClose (IAsyncResult result)
257 throw new NotImplementedException ();
261 protected override void OnEndOpen (IAsyncResult result)
263 throw new NotImplementedException ();
267 protected override void OnOpen (TimeSpan timeout)
269 if (! is_service_side) {
270 int explicitPort = RemoteAddress.Uri.Port;
271 client = new TcpClient (RemoteAddress.Uri.Host, explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
272 //RemoteAddress.Uri.Port);
274 NetworkStream ns = client.GetStream ();
281 byte [] bytes = System.Text.Encoding.UTF8.GetBytes (RemoteAddress.Uri.ToString ());
282 ns.WriteByte ((byte) bytes.Length);
283 ns.Write (bytes, 0, bytes.Length);
287 int hoge = ns.ReadByte ();
289 // Console.Write ("{0:X02} ", ns.ReadByte ());
294 Console.WriteLine ("Server side.");
298 // FIXME: To look for other way to do this.
299 class MyBinaryReader : BinaryReader
301 public MyBinaryReader (Stream s)
306 public byte [] ReadBytes ()
308 byte [] buffer = new byte [65536];
309 int length = Read7BitEncodedInt ();
312 throw new InvalidOperationException ("The message is too large.");
314 Read (buffer, 0, length);
320 class MyBinaryWriter : BinaryWriter
322 public MyBinaryWriter (Stream s)
327 public void WriteBytes (byte [] bytes)
329 Write7BitEncodedInt (bytes.Length);