Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 165 lines (133 sloc) 4.933 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
// -*- mode: csharp; c-basic-offset: 2; indent-tabs-mode: nil -*-
//
// IceClientTransportSink.cs
//
// Written by:
// Vladimir Vukicevic <vladimir@sparklestudios.com>
//
// Copyright (C) 2003 Sparkle Studios, LLC
//
// This file is distributed under the terms of the license
// agreement contained in the LICENSE file in the top level
// of this distribution.
//

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Diagnostics;

namespace Ice {

  public class IceClientTransportSink
    : IClientChannelSink
  {
    string _host;
    int _port;
    string _objectUri;

    Ice.Endpoint _e;
    Ice.ReceiverDispatcher _rd;

    public IceClientTransportSink (string url)
    {
      IceChannelUtils.ParseIceURL (url, out _host, out _port, out _objectUri);

      Ice.TcpEndpoint te = new Ice.TcpEndpoint (_host, _port);
      _e = Ice.Manager.GetManager().GetEndpoint (te);
    }

    public IDictionary Properties {
      get {
        return null;
      }
    }

    public IClientChannelSink NextChannelSink {
      get {
        return null;
      }
    }

    public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
                                     IMessage msg,
                                     ITransportHeaders headers,
                                     Stream stream)
    {
      AsyncSendMessageStreamDelegate asmd = new AsyncSendMessageStreamDelegate (AsyncSendMessageStream);
      asmd.BeginInvoke (sinkStack, msg, headers, stream, null, null);
    }

    public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
                                      object state,
                                      ITransportHeaders headers,
                                      Stream stream)
    {
      Console.WriteLine ("ClientTransport: AsyncProcessResponse");
      throw new NotSupportedException();
    }

    public Stream GetRequestStream (IMessage msg, ITransportHeaders headers)
    {
      return null;
    }

    public void ProcessMessage (IMessage msg,
                                ITransportHeaders requestHeaders,
                                Stream requestStream,
                                out ITransportHeaders responseHeaders,
                                out Stream responseStream)
    {
      IMethodCallMessage mcall = msg as IMethodCallMessage;

      if (_rd == null) {
        _rd = _e.ReceiverDispatcher;
      }

      int requestId = (int) mcall.LogicalCallContext.GetData("__iceRequestId");

      // Register the reply notification /before/ we even send the
      // request
      ManualResetEvent mre = null;

      if (requestId != 0)
         mre = _rd.RegisterReplyNotification (requestId);

      lock (_e.Stream) {
        ((MemoryStream) requestStream).WriteTo (_e.Stream);
      }

      // oneway invocation, nothing to respond
      if (requestId == 0) {
        responseHeaders = null;
        responseStream = null;
        return;
      }

      // wait for the reply to come in
      Trace.WriteLine ("ClientTransportSink Waiting on requestID " + requestId);
      mre.WaitOne();
      Trace.WriteLine ("ClientTransportSink got notification, requestID " + requestId);
      MemoryStream ms = _rd.GetMessageStream (requestId);

      responseStream = ms;
      responseHeaders = new TransportHeaders();
    }

    private delegate void AsyncSendMessageStreamDelegate (IClientChannelSinkStack sinkStack,
                                                          IMessage msg,
                                                          ITransportHeaders requestHeaders,
                                                          Stream requestStream);

    private void AsyncSendMessageStream (IClientChannelSinkStack sinkStack,
                                         IMessage msg,
                                         ITransportHeaders requestHeaders,
                                         Stream requestStream)
    {
      IMethodCallMessage mcall = msg as IMethodCallMessage;

      if (_rd == null) {
        _rd = _e.ReceiverDispatcher;
      }

      int requestId = (int) mcall.LogicalCallContext.GetData("__iceRequestId");
      ManualResetEvent mre = null;

      if (requestId != 0)
        mre = _rd.RegisterReplyNotification (requestId);

      lock (_e.Stream) {
        ((MemoryStream) requestStream).WriteTo (_e.Stream);
      }

      // oneway invocation, no response
      if (requestId == 0) {
        return;
      }

      mre.WaitOne();
      MemoryStream responseStream = _rd.GetMessageStream (requestId);
      TransportHeaders responseHeaders = new TransportHeaders();

      sinkStack.AsyncProcessResponse (responseHeaders, responseStream);
    }

  }

}
Something went wrong with that request. Please try again.