HandlerTransportBindingElement.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //
  2. // HandlerTransportBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. using System.Net.Security;
  32. using System.Threading;
  33. using System.Xml;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. namespace MonoTests.System.ServiceModel.Channels
  38. {
  39. public delegate Message RequestSender (Message input);
  40. public delegate Message RequestReceiver ();
  41. public delegate void ReplyHandler (Message input);
  42. public class HandlerTransportBindingElement : TransportBindingElement
  43. {
  44. RequestSender sender;
  45. ReplyHandler reply_handler;
  46. RequestReceiver receiver;
  47. public HandlerTransportBindingElement (RequestSender sender)
  48. {
  49. this.sender = sender;
  50. }
  51. public HandlerTransportBindingElement (ReplyHandler handler, RequestReceiver receiver)
  52. {
  53. this.reply_handler = handler;
  54. this.receiver = receiver;
  55. }
  56. public RequestSender RequestSender {
  57. get { return sender; }
  58. }
  59. public ReplyHandler ReplyHandler {
  60. get { return reply_handler; }
  61. }
  62. public RequestReceiver RequestReceiver {
  63. get { return receiver; }
  64. }
  65. public override string Scheme {
  66. get { return "stream"; }
  67. }
  68. public override BindingElement Clone ()
  69. {
  70. if (sender != null)
  71. return new HandlerTransportBindingElement (sender);
  72. else
  73. return new HandlerTransportBindingElement (reply_handler, receiver);
  74. }
  75. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  76. {
  77. return typeof (TChannel) == typeof (IRequestChannel) ||
  78. typeof (TChannel) == typeof (IRequestChannel);
  79. }
  80. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  81. {
  82. return typeof (TChannel) == typeof (IReplyChannel) ||
  83. typeof (TChannel) == typeof (IInputChannel);
  84. }
  85. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
  86. {
  87. return new HandlerTransportChannelFactory<TChannel> (this);
  88. }
  89. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (BindingContext context)
  90. {
  91. // FIXME: pass uri
  92. return new HandlerTransportChannelListener<TChannel> (this, new Uri ("stream:dummy"));
  93. }
  94. }
  95. public class HandlerTransportChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  96. {
  97. HandlerTransportBindingElement source;
  98. public HandlerTransportChannelFactory (HandlerTransportBindingElement source)
  99. {
  100. this.source = source;
  101. }
  102. public HandlerTransportBindingElement Source {
  103. get { return source; }
  104. }
  105. protected override TChannel OnCreateChannel (EndpointAddress address, Uri via)
  106. {
  107. if (typeof (TChannel) == typeof (IRequestChannel))
  108. return (TChannel) (object) new HandlerTransportRequestChannel ((HandlerTransportChannelFactory<IRequestChannel>) (object) this, address, via);
  109. if (typeof (TChannel) == typeof (IOutputChannel))
  110. return (TChannel) (object) new HandlerTransportOutputChannel ((HandlerTransportChannelFactory<IOutputChannel>) (object) this, address, via);
  111. throw new NotSupportedException (String.Format ("Channel '{0}' is not supported.", typeof (TChannel)));
  112. }
  113. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  114. {
  115. throw new NotSupportedException ();
  116. }
  117. protected override void OnEndOpen (IAsyncResult result)
  118. {
  119. throw new NotSupportedException ();
  120. }
  121. protected override void OnOpen (TimeSpan timeout)
  122. {
  123. // do nothing
  124. }
  125. }
  126. public class HandlerTransportOutputChannel : OutputChannelBase
  127. {
  128. HandlerTransportChannelFactory<IOutputChannel> source;
  129. EndpointAddress address;
  130. Uri via;
  131. public HandlerTransportOutputChannel (HandlerTransportChannelFactory<IOutputChannel> source, EndpointAddress address, Uri via)
  132. : base (source)
  133. {
  134. this.source = source;
  135. this.address = address;
  136. this.via = via;
  137. }
  138. public override EndpointAddress RemoteAddress {
  139. get { return address; }
  140. }
  141. public override Uri Via {
  142. get { return via; }
  143. }
  144. public override void Send (Message input, TimeSpan timeout)
  145. {
  146. source.Source.RequestSender (input);
  147. }
  148. class OutputAsyncResult : IAsyncResult
  149. {
  150. Message message;
  151. object state;
  152. bool completed = true;
  153. public OutputAsyncResult (Message message, object state)
  154. {
  155. this.message = message;
  156. this.state = state;
  157. }
  158. public Message Message {
  159. get { return message; }
  160. }
  161. public object AsyncState {
  162. get { return state; }
  163. }
  164. public WaitHandle AsyncWaitHandle {
  165. get { return null; }
  166. }
  167. public bool CompletedSynchronously {
  168. get { return true; }
  169. }
  170. public bool IsCompleted {
  171. get { return completed; }
  172. internal set { completed = value; }
  173. }
  174. }
  175. public override IAsyncResult BeginSend (Message input, TimeSpan timeout, AsyncCallback callback, object state)
  176. {
  177. // FIXME: timeout is not considered here.
  178. return new OutputAsyncResult (input, state);
  179. }
  180. public override void EndSend (IAsyncResult result)
  181. {
  182. source.Source.RequestSender (((OutputAsyncResult) result).Message);
  183. }
  184. protected override void OnAbort ()
  185. {
  186. }
  187. protected override void OnOpen (TimeSpan timeout)
  188. {
  189. // throw new NotImplementedException ("OnOpen");
  190. }
  191. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  192. {
  193. throw new NotImplementedException ("OnBeginOpen");
  194. }
  195. protected override void OnEndOpen (IAsyncResult result)
  196. {
  197. throw new NotImplementedException ("OnEndOpen");
  198. }
  199. protected override void OnClose (TimeSpan timeout)
  200. {
  201. // throw new NotImplementedException ("OnClose");
  202. }
  203. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  204. {
  205. throw new NotImplementedException ("OnBeginClose");
  206. }
  207. protected override void OnEndClose (IAsyncResult result)
  208. {
  209. throw new NotImplementedException ("OnEndClose");
  210. }
  211. }
  212. public class HandlerTransportRequestChannel : RequestChannelBase
  213. {
  214. HandlerTransportChannelFactory<IRequestChannel> source;
  215. EndpointAddress address;
  216. Uri via;
  217. public HandlerTransportRequestChannel (HandlerTransportChannelFactory<IRequestChannel> source, EndpointAddress address, Uri via)
  218. : base (source)
  219. {
  220. this.source = source;
  221. this.address = address;
  222. this.via = via;
  223. }
  224. public override EndpointAddress RemoteAddress {
  225. get { return address; }
  226. }
  227. public override Uri Via {
  228. get { return via; }
  229. }
  230. public override Message Request (Message input, TimeSpan timeout)
  231. {
  232. return source.Source.RequestSender (input);
  233. }
  234. class RequestAsyncResult : IAsyncResult
  235. {
  236. Message message;
  237. object state;
  238. bool completed = true;
  239. public RequestAsyncResult (Message message, object state)
  240. {
  241. this.message = message;
  242. this.state = state;
  243. }
  244. public Message Message {
  245. get { return message; }
  246. }
  247. public object AsyncState {
  248. get { return state; }
  249. }
  250. public WaitHandle AsyncWaitHandle {
  251. get { return null; }
  252. }
  253. public bool CompletedSynchronously {
  254. get { return true; }
  255. }
  256. public bool IsCompleted {
  257. get { return completed; }
  258. internal set { completed = value; }
  259. }
  260. }
  261. public override IAsyncResult BeginRequest (Message input, TimeSpan timeout, AsyncCallback callback, object state)
  262. {
  263. // FIXME: timeout is not considered here.
  264. return new RequestAsyncResult (input, state);
  265. }
  266. public override Message EndRequest (IAsyncResult result)
  267. {
  268. return source.Source.RequestSender (((RequestAsyncResult) result).Message);
  269. }
  270. protected override void OnAbort ()
  271. {
  272. }
  273. protected override void OnOpen (TimeSpan timeout)
  274. {
  275. // throw new NotImplementedException ("OnOpen");
  276. }
  277. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  278. {
  279. throw new NotImplementedException ("OnBeginOpen");
  280. }
  281. protected override void OnEndOpen (IAsyncResult result)
  282. {
  283. throw new NotImplementedException ("OnEndOpen");
  284. }
  285. protected override void OnClose (TimeSpan timeout)
  286. {
  287. // throw new NotImplementedException ("OnClose");
  288. }
  289. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  290. {
  291. throw new NotImplementedException ("OnBeginClose");
  292. }
  293. protected override void OnEndClose (IAsyncResult result)
  294. {
  295. throw new NotImplementedException ("OnEndClose");
  296. }
  297. }
  298. public class HandlerTransportChannelListener<TChannel>
  299. : ChannelListenerBase<TChannel>
  300. where TChannel : class, IChannel
  301. {
  302. HandlerTransportBindingElement source;
  303. Uri uri;
  304. public HandlerTransportChannelListener (HandlerTransportBindingElement source, Uri uri)
  305. {
  306. this.source = source;
  307. this.uri = uri;
  308. }
  309. public HandlerTransportBindingElement Source {
  310. get { return source; }
  311. }
  312. public override Uri Uri {
  313. get { return uri; }
  314. }
  315. protected override TChannel OnAcceptChannel (TimeSpan timeout)
  316. {
  317. if (typeof (TChannel) == typeof (IReplyChannel))
  318. return (TChannel) (object) new HandlerTransportReplyChannel ((HandlerTransportChannelListener<IReplyChannel>) (object) this);
  319. throw new NotSupportedException ();
  320. }
  321. protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
  322. {
  323. throw new NotImplementedException ("OnBeginAcceptChannel");
  324. }
  325. protected override TChannel OnEndAcceptChannel (IAsyncResult result)
  326. {
  327. throw new NotImplementedException ("EndAcceptChannel");
  328. }
  329. protected override bool OnWaitForChannel (TimeSpan timeout)
  330. {
  331. return true;
  332. }
  333. protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  334. {
  335. throw new NotImplementedException ("OnBeginWaitForChannel");
  336. }
  337. protected override bool OnEndWaitForChannel (IAsyncResult result)
  338. {
  339. throw new NotImplementedException ("EndWaitForChannel");
  340. }
  341. protected override void OnAbort ()
  342. {
  343. }
  344. protected override void OnOpen (TimeSpan timeout)
  345. {
  346. // throw new NotImplementedException ("OnOpen");
  347. }
  348. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  349. {
  350. throw new NotImplementedException ("OnBeginOpen");
  351. }
  352. protected override void OnEndOpen (IAsyncResult result)
  353. {
  354. throw new NotImplementedException ("EndOpen");
  355. }
  356. protected override void OnClose (TimeSpan timeout)
  357. {
  358. //throw new NotImplementedException ("Close");
  359. }
  360. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  361. {
  362. throw new NotImplementedException ("OnBeginClose");
  363. }
  364. protected override void OnEndClose (IAsyncResult result)
  365. {
  366. throw new NotImplementedException ("OnEndClose");
  367. }
  368. }
  369. public class HandlerTransportReplyChannel : ReplyChannelBase
  370. {
  371. EndpointAddress address;
  372. HandlerTransportChannelListener<IReplyChannel> source;
  373. public HandlerTransportReplyChannel (HandlerTransportChannelListener<IReplyChannel> source)
  374. : base (source)
  375. {
  376. this.source = source;
  377. address = new EndpointAddress (source.Uri);
  378. }
  379. public HandlerTransportChannelListener<IReplyChannel> Source {
  380. get { return source; }
  381. }
  382. public override EndpointAddress LocalAddress {
  383. get { return address; }
  384. }
  385. class ReceiveRequestAsyncResult : IAsyncResult
  386. {
  387. object state;
  388. bool completed = true;
  389. public ReceiveRequestAsyncResult (object state)
  390. {
  391. this.state = state;
  392. }
  393. public object AsyncState {
  394. get { return state; }
  395. }
  396. public WaitHandle AsyncWaitHandle {
  397. get { return null; }
  398. }
  399. public bool CompletedSynchronously {
  400. get { return true; }
  401. }
  402. public bool IsCompleted {
  403. get { return completed; }
  404. internal set { completed = value; }
  405. }
  406. }
  407. public override RequestContext ReceiveRequest (TimeSpan timeout)
  408. {
  409. RequestContext ret;
  410. if (!TryReceiveRequest (timeout, out ret))
  411. throw new Exception ();
  412. return ret;
  413. }
  414. public override IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  415. {
  416. return new ReceiveRequestAsyncResult (state);
  417. }
  418. public override RequestContext EndReceiveRequest (IAsyncResult result)
  419. {
  420. //ReceiveRequestAsyncResult r =
  421. // (ReceiveRequestAsyncResult) result;
  422. return new HandlerRequestContext (this);
  423. }
  424. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext ret)
  425. {
  426. ret = new HandlerRequestContext (this);
  427. return true;
  428. }
  429. public override IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  430. {
  431. // hack, hack
  432. return new ReceiveRequestAsyncResult (state);
  433. }
  434. public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext ret)
  435. {
  436. // hack, hack
  437. //ReceiveRequestAsyncResult r =
  438. // (ReceiveRequestAsyncResult) result;
  439. return TryReceiveRequest (TimeSpan.FromSeconds (5), out ret);
  440. }
  441. public override bool WaitForRequest (TimeSpan timeout)
  442. {
  443. throw new NotImplementedException ();
  444. }
  445. public override IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
  446. {
  447. throw new NotImplementedException ("BeginWaitForRequest");
  448. }
  449. public override bool EndWaitForRequest (IAsyncResult result)
  450. {
  451. throw new NotImplementedException ("EndWaitForRequest");
  452. }
  453. protected override void OnAbort ()
  454. {
  455. }
  456. protected override void OnOpen (TimeSpan timeout)
  457. {
  458. // throw new NotImplementedException ("OnOpen");
  459. }
  460. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  461. {
  462. throw new NotImplementedException ("OnBeginOpen");
  463. }
  464. protected override void OnEndOpen (IAsyncResult result)
  465. {
  466. throw new NotImplementedException ("EndOpen");
  467. }
  468. protected override void OnClose (TimeSpan timeout)
  469. {
  470. //throw new NotImplementedException ("Close");
  471. }
  472. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  473. {
  474. throw new NotImplementedException ("OnBeginClose");
  475. }
  476. protected override void OnEndClose (IAsyncResult result)
  477. {
  478. throw new NotImplementedException ("OnEndClose");
  479. }
  480. }
  481. public class HandlerRequestContext : RequestContext
  482. {
  483. HandlerTransportReplyChannel source;
  484. Message request_message;
  485. public HandlerRequestContext (HandlerTransportReplyChannel source)
  486. {
  487. this.source = source;
  488. if (source.Source.Source.RequestReceiver != null)
  489. request_message = source.Source.Source.RequestReceiver ();
  490. }
  491. public override void Abort ()
  492. {
  493. }
  494. public override void Close ()
  495. {
  496. }
  497. public override void Close (TimeSpan timeout)
  498. {
  499. }
  500. public override Message RequestMessage {
  501. get { return request_message; }
  502. }
  503. public override void Reply (Message msg)
  504. {
  505. source.Source.Source.ReplyHandler (msg);
  506. }
  507. public override void Reply (Message msg, TimeSpan timeout)
  508. {
  509. source.Source.Source.ReplyHandler (msg);
  510. }
  511. public override IAsyncResult BeginReply (Message msg, AsyncCallback callback, object state)
  512. {
  513. throw new NotImplementedException ("BeginReply");
  514. }
  515. public override IAsyncResult BeginReply (Message msg, TimeSpan timeout, AsyncCallback callback, object state)
  516. {
  517. throw new NotImplementedException ("BeginReply");
  518. }
  519. public override void EndReply (IAsyncResult result)
  520. {
  521. throw new NotImplementedException ("EndReply");
  522. }
  523. }
  524. }