TcpDuplexSessionChannel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // TcpDuplexSessionChannel.cs
  3. //
  4. // Author:
  5. // Marcos Cobena ([email protected])
  6. //
  7. // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.ServiceModel.Channels;
  16. using System.Xml;
  17. namespace System.ServiceModel.Channels
  18. {
  19. internal class TcpDuplexSessionChannel : DuplexChannelBase, IDuplexSessionChannel
  20. {
  21. TcpChannelInfo info;
  22. TcpClient client;
  23. bool is_service_side;
  24. EndpointAddress local_address;
  25. TcpListener tcp_listener;
  26. TimeSpan timeout;
  27. public TcpDuplexSessionChannel (ChannelFactoryBase factory, TcpChannelInfo info, EndpointAddress address, Uri via)
  28. : base (factory, address, via)
  29. {
  30. is_service_side = false;
  31. this.info = info;
  32. }
  33. public TcpDuplexSessionChannel (ChannelListenerBase listener, TcpChannelInfo info, TcpClient acceptedRequest, TimeSpan timeout)
  34. : base (listener)
  35. {
  36. is_service_side = true;
  37. this.info = info;
  38. this.client = acceptedRequest;
  39. this.timeout = timeout;
  40. Stream s = client.GetStream ();
  41. //while (s.CanRead)
  42. // Console.Write ("{0:X02} ", s.ReadByte ());
  43. for (int i = 0; i < 6; i++)
  44. s.ReadByte ();
  45. int size = s.ReadByte ();
  46. for (int i = 0; i < size; i++)
  47. s.ReadByte (); // URI
  48. s.ReadByte ();
  49. s.ReadByte ();
  50. s.ReadByte ();
  51. s.WriteByte (0x0B);
  52. }
  53. public MessageEncoder Encoder {
  54. get { return info.MessageEncoder; }
  55. }
  56. public override EndpointAddress LocalAddress {
  57. get { return local_address; }
  58. }
  59. // FIXME: implement
  60. public IDuplexSession Session {
  61. get { throw new NotImplementedException (); }
  62. }
  63. [MonoTODO]
  64. public override IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
  65. {
  66. throw new NotImplementedException ();
  67. }
  68. [MonoTODO]
  69. public override IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. [MonoTODO]
  74. public override void EndSend (IAsyncResult result)
  75. {
  76. throw new NotImplementedException ();
  77. }
  78. [MonoTODO]
  79. public override void Send (Message message)
  80. {
  81. MemoryStream ms = new MemoryStream ();
  82. BinaryFormatter bf = new BinaryFormatter ();
  83. try
  84. {
  85. NetworkStream stream = client.GetStream ();
  86. MyBinaryWriter bw = new MyBinaryWriter (stream);
  87. bw.Write ((byte) 6);
  88. Encoder.WriteMessage (message, ms);
  89. bw.WriteBytes (ms.ToArray ());
  90. bw.Write ((byte) 7);
  91. bw.Flush ();
  92. stream.ReadByte (); // 7
  93. }
  94. catch (Exception e)
  95. {
  96. throw e;
  97. }
  98. }
  99. [MonoTODO]
  100. public override void Send (Message message, TimeSpan timeout)
  101. {
  102. throw new NotImplementedException ();
  103. }
  104. [MonoTODO]
  105. public override IAsyncResult BeginReceive (AsyncCallback callback, object state)
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. [MonoTODO]
  110. public override IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. [MonoTODO]
  115. public override IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. [MonoTODO]
  120. public override IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
  121. {
  122. throw new NotImplementedException ();
  123. }
  124. [MonoTODO]
  125. public override Message EndReceive (IAsyncResult result)
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. [MonoTODO]
  130. public override bool EndTryReceive (IAsyncResult result, out Message message)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. [MonoTODO]
  135. public override bool EndWaitForMessage (IAsyncResult result)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. [MonoTODO]
  140. public override Message Receive ()
  141. {
  142. Stream s = client.GetStream ();
  143. s.ReadByte (); // 6
  144. MyBinaryReader br = new MyBinaryReader (s);
  145. // string msg = br.ReadString ();
  146. // br.Read7BitEncodedInt ();
  147. byte [] buffer = new byte [65536];
  148. buffer = br.ReadBytes ();
  149. MemoryStream ms = new MemoryStream ();
  150. ms.Write (buffer, 0, buffer.Length);
  151. ms.Seek (0, SeekOrigin.Begin);
  152. // while (s.CanRead)
  153. // Console.Write ("{0:X02} ", s.ReadByte ());
  154. Message msg = null;
  155. // FIXME: To supply maxSizeOfHeaders.
  156. msg = Encoder.ReadMessage (ms, 0x10000);
  157. s.ReadByte (); // 7
  158. // Console.WriteLine (msg);
  159. s.WriteByte (7);
  160. s.Flush ();
  161. return msg;
  162. }
  163. [MonoTODO]
  164. public override Message Receive (TimeSpan timeout)
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MonoTODO]
  169. public override bool TryReceive (TimeSpan timeout, out Message message)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. [MonoTODO]
  174. public override bool WaitForMessage (TimeSpan timeout)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. // CommunicationObject
  179. [MonoTODO]
  180. protected override void OnAbort ()
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. [MonoTODO]
  185. protected override IAsyncResult OnBeginClose (TimeSpan timeout,
  186. AsyncCallback callback, object state)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. [MonoTODO]
  191. protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
  192. AsyncCallback callback, object state)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO]
  197. protected override void OnClose (TimeSpan timeout)
  198. {
  199. client.Close ();
  200. }
  201. [MonoTODO]
  202. protected override void OnEndClose (IAsyncResult result)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. [MonoTODO]
  207. protected override void OnEndOpen (IAsyncResult result)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. [MonoTODO]
  212. protected override void OnOpen (TimeSpan timeout)
  213. {
  214. if (! is_service_side) {
  215. int explicitPort = RemoteAddress.Uri.Port;
  216. client = new TcpClient (RemoteAddress.Uri.Host, explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
  217. //RemoteAddress.Uri.Port);
  218. NetworkStream ns = client.GetStream ();
  219. ns.WriteByte (0);
  220. ns.WriteByte (1);
  221. ns.WriteByte (0);
  222. ns.WriteByte (1);
  223. ns.WriteByte (2);
  224. ns.WriteByte (2);
  225. byte [] bytes = System.Text.Encoding.UTF8.GetBytes (RemoteAddress.Uri.ToString ());
  226. ns.WriteByte ((byte) bytes.Length);
  227. ns.Write (bytes, 0, bytes.Length);
  228. ns.WriteByte (3);
  229. ns.WriteByte (3);
  230. ns.WriteByte (0xC);
  231. int hoge = ns.ReadByte ();
  232. //while (ns.CanRead)
  233. // Console.Write ("{0:X02} ", ns.ReadByte ());
  234. }
  235. // Service side.
  236. /*
  237. else
  238. Console.WriteLine ("Server side.");
  239. */
  240. }
  241. // FIXME: To look for other way to do this.
  242. class MyBinaryReader : BinaryReader
  243. {
  244. public MyBinaryReader (Stream s)
  245. : base (s)
  246. {
  247. }
  248. public byte [] ReadBytes ()
  249. {
  250. byte [] buffer = new byte [65536];
  251. int length = Read7BitEncodedInt ();
  252. if (length > 65536)
  253. throw new InvalidOperationException ("The message is too large.");
  254. Read (buffer, 0, length);
  255. return buffer;
  256. }
  257. }
  258. class MyBinaryWriter : BinaryWriter
  259. {
  260. public MyBinaryWriter (Stream s)
  261. : base (s)
  262. {
  263. }
  264. public void WriteBytes (byte [] bytes)
  265. {
  266. Write7BitEncodedInt (bytes.Length);
  267. Write (bytes);
  268. }
  269. }
  270. }
  271. }