| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- //
- // TcpDuplexSessionChannel.cs
- //
- // Author:
- // Marcos Cobena ([email protected])
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
- //
- // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.ServiceModel.Channels;
- using System.Text;
- using System.Threading;
- using System.Xml;
- namespace System.ServiceModel.Channels
- {
- internal class TcpDuplexSessionChannel : DuplexChannelBase, IDuplexSessionChannel
- {
- class TcpDuplexSession : DuplexSessionBase
- {
- TcpDuplexSessionChannel owner;
- internal TcpDuplexSession (TcpDuplexSessionChannel owner)
- {
- this.owner = owner;
- }
- public override TimeSpan DefaultCloseTimeout {
- get { return owner.DefaultCloseTimeout; }
- }
- public override void Close (TimeSpan timeout)
- {
- owner.DiscardSession ();
- }
- }
- TcpChannelInfo info;
- TcpClient client;
- bool is_service_side;
- EndpointAddress local_address;
- TimeSpan timeout;
- TcpBinaryFrameManager frame;
- TcpDuplexSession session; // do not use this directly. Use Session instead.
-
- public TcpDuplexSessionChannel (ChannelFactoryBase factory, TcpChannelInfo info, EndpointAddress address, Uri via)
- : base (factory, address, via)
- {
- is_service_side = false;
- this.info = info;
- }
-
- public TcpDuplexSessionChannel (ChannelListenerBase listener, TcpChannelInfo info, TcpClient client, TimeSpan timeout)
- : base (listener)
- {
- is_service_side = true;
- this.client = client;
- this.info = info;
- this.timeout = timeout;
- }
-
- public MessageEncoder Encoder {
- get { return info.MessageEncoder; }
- }
- public override EndpointAddress LocalAddress {
- get { return local_address; }
- }
-
- public IDuplexSession Session {
- get {
- if (session == null)
- session = new TcpDuplexSession (this);
- return session;
- }
- }
- void DiscardSession ()
- {
- frame.ProcessEndRecordInitiator ();
- session = null;
- }
- public override void Send (Message message)
- {
- Send (message, DefaultSendTimeout);
- }
-
- public override void Send (Message message, TimeSpan timeout)
- {
- if (timeout <= TimeSpan.Zero)
- throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
- if (!is_service_side) {
- if (message.Headers.To == null)
- message.Headers.To = RemoteAddress.Uri;
- if (message.Headers.ReplyTo == null)
- message.Headers.ReplyTo = new EndpointAddress (Constants.WsaAnonymousUri);
- } else {
- if (message.Headers.RelatesTo == null)
- message.Headers.RelatesTo = OperationContext.Current.IncomingMessageHeaders.MessageId;
- }
- client.SendTimeout = (int) timeout.TotalMilliseconds;
- frame.WriteSizedMessage (message);
- // FIXME: should EndRecord be sent here?
- //if (is_service_side && client.Available > 0)
- // frame.ProcessEndRecordRecipient ();
- }
-
- public override Message Receive ()
- {
- return Receive (DefaultReceiveTimeout);
- }
-
- public override Message Receive (TimeSpan timeout)
- {
- if (timeout <= TimeSpan.Zero)
- throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
- client.ReceiveTimeout = (int) timeout.TotalMilliseconds;
- return frame.ReadSizedMessage ();
- }
-
- public override bool TryReceive (TimeSpan timeout, out Message message)
- {
- try {
- DateTime start = DateTime.Now;
- message = Receive (timeout);
- if (message != null)
- return true;
- // received EndRecord, so close the session and return false instead.
- // (Closing channel here might not be a good idea, but right now I have no better way.)
- Close (timeout - (DateTime.Now - start));
- return false;
- } catch (TimeoutException) {
- message = null;
- return false;
- }
- }
-
- public override bool WaitForMessage (TimeSpan timeout)
- {
- if (client.Available > 0)
- return true;
- DateTime start = DateTime.Now;
- do {
- Thread.Sleep (50);
- if (client.Available > 0)
- return true;
- } while (DateTime.Now - start < timeout);
- return false;
- }
-
- // CommunicationObject
-
- [MonoTODO]
- protected override void OnAbort ()
- {
- if (client != null)
- client.Close ();
- }
- [MonoTODO]
- protected override IAsyncResult OnBeginClose (TimeSpan timeout,
- AsyncCallback callback, object state)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
- AsyncCallback callback, object state)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected override void OnClose (TimeSpan timeout)
- {
- if (!is_service_side)
- if (session != null)
- session.Close (timeout);
- if (client != null)
- client.Close ();
- }
-
- [MonoTODO]
- protected override void OnEndClose (IAsyncResult result)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected override void OnEndOpen (IAsyncResult result)
- {
- throw new NotImplementedException ();
- }
-
- [MonoTODO]
- protected override void OnOpen (TimeSpan timeout)
- {
- if (! is_service_side) {
- int explicitPort = RemoteAddress.Uri.Port;
- client = new TcpClient (RemoteAddress.Uri.Host, explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
- //RemoteAddress.Uri.Port);
-
- NetworkStream ns = client.GetStream ();
- frame = new TcpBinaryFrameManager (TcpBinaryFrameManager.DuplexMode, ns, is_service_side) {
- Encoder = this.Encoder,
- Via = RemoteAddress.Uri };
- frame.ProcessPreambleInitiator ();
- frame.ProcessPreambleAckInitiator ();
- } else {
- // server side
- Stream s = client.GetStream ();
- frame = new TcpBinaryFrameManager (TcpBinaryFrameManager.DuplexMode, s, is_service_side) { Encoder = this.Encoder };
- // FIXME: use retrieved record properties in the request processing.
- frame.ProcessPreambleRecipient ();
- frame.ProcessPreambleAckRecipient ();
- }
- }
-
- class MyBinaryWriter : BinaryWriter
- {
- public MyBinaryWriter (Stream s)
- : base (s)
- {
- }
-
- public void WriteBytes (byte [] bytes)
- {
- Write7BitEncodedInt (bytes.Length);
- Write (bytes);
- }
- }
- }
- // seealso: [MC-NMF] Windows Protocol document.
- class TcpBinaryFrameManager
- {
- class MyBinaryReader : BinaryReader
- {
- public MyBinaryReader (Stream s)
- : base (s)
- {
- }
- public int ReadVariableInt ()
- {
- return Read7BitEncodedInt ();
- }
- }
- class MyBinaryWriter : BinaryWriter
- {
- public MyBinaryWriter (Stream s)
- : base (s)
- {
- }
- public void WriteVariableInt (int value)
- {
- Write7BitEncodedInt (value);
- }
- public int GetSizeOfLength (int value)
- {
- int x = 0;
- do {
- value /= 0x100;
- x++;
- } while (value != 0);
- return x;
- }
- }
- class MyXmlBinaryWriterSession : XmlBinaryWriterSession
- {
- public override bool TryAdd (XmlDictionaryString value, out int key)
- {
- if (!base.TryAdd (value, out key))
- return false;
- List.Add (value);
- return true;
- }
- public List<XmlDictionaryString> List = new List<XmlDictionaryString> ();
- }
- public const byte VersionRecord = 0;
- public const byte ModeRecord = 1;
- public const byte ViaRecord = 2;
- public const byte KnownEncodingRecord = 3;
- public const byte ExtendingEncodingRecord = 4;
- public const byte UnsizedEnvelopeRecord = 5;
- public const byte SizedEnvelopeRecord = 6;
- public const byte EndRecord = 7;
- public const byte FaultRecord = 8;
- public const byte UpgradeRequestRecord = 9;
- public const byte UpgradeResponseRecord = 0xA;
- public const byte PreambleAckRecord = 0xB;
- public const byte PreambleEndRecord = 0xC;
- public const byte SingletonUnsizedMode = 1;
- public const byte DuplexMode = 2;
- public const byte SimplexMode = 3;
- public const byte SingletonSizedMode = 4;
- MyBinaryReader reader;
- MyBinaryWriter writer;
- public TcpBinaryFrameManager (int mode, Stream s, bool isServiceSide)
- {
- this.mode = mode;
- this.s = s;
- this.is_service_side = isServiceSide;
- reader = new MyBinaryReader (s);
- ResetWriteBuffer ();
- EncodingRecord = 8; // FIXME: it should depend on mode.
- }
- Stream s;
- MemoryStream buffer;
- bool is_service_side;
- int mode;
- public byte EncodingRecord { get; set; }
- public Uri Via { get; set; }
- public MessageEncoder Encoder { get; set; }
- void ResetWriteBuffer ()
- {
- this.buffer = new MemoryStream ();
- writer = new MyBinaryWriter (buffer);
- }
- public byte [] ReadSizedChunk ()
- {
- int length = reader.ReadVariableInt ();
-
- if (length > 65536)
- throw new InvalidOperationException ("The message is too large.");
- byte [] buffer = new byte [length];
- for (int readSize = 0; readSize < length; )
- readSize += reader.Read (buffer, readSize, length - readSize);
- return buffer;
- }
- public void WriteSizedChunk (byte [] data)
- {
- writer.WriteVariableInt (data.Length);
- writer.Write (data, 0, data.Length);
- }
- public void ProcessPreambleInitiator ()
- {
- ResetWriteBuffer ();
- buffer.WriteByte (VersionRecord);
- buffer.WriteByte (1);
- buffer.WriteByte (0);
- buffer.WriteByte (ModeRecord);
- buffer.WriteByte ((byte) mode);
- buffer.WriteByte (ViaRecord);
- writer.Write (Via.ToString ());
- buffer.WriteByte (KnownEncodingRecord); // FIXME
- buffer.WriteByte ((byte) EncodingRecord);
- buffer.WriteByte (PreambleEndRecord);
- buffer.Flush ();
- s.Write (buffer.GetBuffer (), 0, (int) buffer.Position);
- s.Flush ();
- }
- public void ProcessPreambleAckInitiator ()
- {
- int b = s.ReadByte ();
- switch (b) {
- case PreambleAckRecord:
- return; // success
- case FaultRecord:
- throw new FaultException (reader.ReadString ());
- default:
- throw new ProtocolException (String.Format ("Preamble Ack Record is expected, got {0:X}", b));
- }
- }
- public void ProcessPreambleAckRecipient ()
- {
- s.WriteByte (PreambleAckRecord);
- }
- public void ProcessPreambleRecipient ()
- {
- bool preambleEnd = false;
- while (!preambleEnd) {
- int b = s.ReadByte ();
- switch (b) {
- case VersionRecord:
- if (s.ReadByte () != 1)
- throw new ProtocolException ("Major version must be 1");
- if (s.ReadByte () != 0)
- throw new ProtocolException ("Minor version must be 0");
- break;
- case ModeRecord:
- if (s.ReadByte () != mode)
- throw new ProtocolException (String.Format ("Duplex mode is expected to be {0:X}", mode));
- break;
- case ViaRecord:
- Via = new Uri (reader.ReadString ());
- break;
- case KnownEncodingRecord:
- EncodingRecord = (byte) s.ReadByte ();
- break;
- case ExtendingEncodingRecord:
- throw new NotImplementedException ("ExtendingEncodingRecord");
- case UpgradeRequestRecord:
- throw new NotImplementedException ("UpgradeRequetRecord");
- case UpgradeResponseRecord:
- throw new NotImplementedException ("UpgradeResponseRecord");
- case PreambleEndRecord:
- preambleEnd = true;
- break;
- default:
- throw new ProtocolException (String.Format ("Unexpected record type {0:X2}", b));
- }
- }
- }
- XmlBinaryReaderSession reader_session;
- int reader_session_items;
- public Message ReadSizedMessage ()
- {
- // FIXME: implement full [MC-NMF].
- var packetType = s.ReadByte ();
- if (packetType == EndRecord)
- return null;
- if (packetType != SizedEnvelopeRecord)
- throw new NotImplementedException (String.Format ("Packet type {0:X} is not implemented", packetType));
- byte [] buffer = ReadSizedChunk ();
- var ms = new MemoryStream (buffer, 0, buffer.Length);
- // FIXME: turned out that it could be either in-band dictionary ([MC-NBFSE]), or a mere xml body ([MC-NBFS]).
- if (EncodingRecord != 8)
- throw new NotImplementedException (String.Format ("Message encoding {0:X} is not implemented yet", EncodingRecord));
- // Encoding type 8:
- // the returned buffer consists of a serialized reader
- // session and the binary xml body.
- var session = reader_session ?? new XmlBinaryReaderSession ();
- reader_session = session;
- byte [] rsbuf = new TcpBinaryFrameManager (0, ms, is_service_side).ReadSizedChunk ();
- using (var rms = new MemoryStream (rsbuf, 0, rsbuf.Length)) {
- var rbr = new BinaryReader (rms, Encoding.UTF8);
- while (rms.Position < rms.Length)
- session.Add (reader_session_items++, rbr.ReadString ());
- }
- var benc = Encoder as BinaryMessageEncoder;
- if (benc != null)
- benc.CurrentReaderSession = session;
- // FIXME: supply maxSizeOfHeaders.
- Message msg = Encoder.ReadMessage (ms, 0x10000);
- if (benc != null)
- benc.CurrentReaderSession = null;
- // if (!is_service_side)
- // if (s.Read (eof_buffer, 0, 1) == 1)
- // if (eof_buffer [0] != EndRecord)
- // throw new ProtocolException (String.Format ("Expected EndRecord message, got {0:X02}", eof_buffer [0]));
- //
- return msg;
- }
- byte [] eof_buffer = new byte [1];
- MyXmlBinaryWriterSession writer_session;
- public void WriteSizedMessage (Message message)
- {
- ResetWriteBuffer ();
- // FIXME: implement full [MC-NMF] protocol.
- if (EncodingRecord != 8)
- throw new NotImplementedException (String.Format ("Message encoding {0:X} is not implemented yet", EncodingRecord));
- buffer.WriteByte (SizedEnvelopeRecord);
- MemoryStream ms = new MemoryStream ();
- var session = writer_session ?? new MyXmlBinaryWriterSession ();
- writer_session = session;
- int writer_session_count = session.List.Count;
- var benc = Encoder as BinaryMessageEncoder;
- try {
- if (benc != null)
- benc.CurrentWriterSession = session;
- Encoder.WriteMessage (message, ms);
- } finally {
- benc.CurrentWriterSession = null;
- }
- // dictionary
- MemoryStream msd = new MemoryStream ();
- BinaryWriter dw = new BinaryWriter (msd);
- for (int i = writer_session_count; i < session.List.Count; i++)
- dw.Write (session.List [i].Value);
- dw.Flush ();
- int length = (int) (msd.Position + ms.Position);
- var msda = msd.ToArray ();
- int sizeOfLength = writer.GetSizeOfLength (msda.Length);
- writer.WriteVariableInt (length + sizeOfLength); // dictionary array also involves the size of itself.
- WriteSizedChunk (msda);
- // message body
- var arr = ms.GetBuffer ();
- writer.Write (arr, 0, (int) ms.Position);
- writer.Flush ();
- s.Write (buffer.GetBuffer (), 0, (int) buffer.Position);
- s.Flush ();
- }
- public void ProcessEndRecordInitiator ()
- {
- s.WriteByte (EndRecord); // it is required
- s.Flush ();
- }
- public void ProcessEndRecordRecipient ()
- {
- int b;
- if ((b = s.ReadByte ()) != EndRecord)
- throw new ProtocolException (String.Format ("EndRecord message was expected, got {0:X}", b));
- }
- }
- }
|