示例#1
0
        public void AsyncProcessRequest(IClientChannelSinkStack stack,
                                        IMessage msg,
                                        ITransportHeaders headers,
                                        Stream stream)
        {
            DBG.Info(null, "Async: Send the message across the pipe");

            PipeConnection _pipe = SendWithRetry(msg, headers, stream);

            IMethodCallMessage mcm        = (IMethodCallMessage)msg;
            MethodBase         methodBase = mcm.MethodBase;
            bool oneway = RemotingServices.IsOneWay(methodBase);

            if (oneway)
            {
                if (_pipeConnectionPool != null)
                {
                    _pipeConnectionPool.ReturnToPool(_pipe);
                }
                _pipe = null;
            }
            else
            {
                PipeConnectionCookie cookie = new PipeConnectionCookie();

                cookie.pipe      = _pipe;
                cookie.sinkStack = stack;

                //TODO Switch to use Completion port
                ThreadPool.QueueUserWorkItem(callback, cookie);
            }
        }
示例#2
0
        public void ProcessMessage(IMessage msg,
                                   ITransportHeaders reqHead,
                                   Stream reqStm,
                                   out ITransportHeaders respHead,
                                   out Stream respStm)
        {
            DBG.Info(null, "Being asked to process the serialized message!");
            //Console.WriteLine("ProcessMessage TID {0}", Thread.CurrentThread.GetHashCode());

            // Send the message across the pipe.
            PipeConnection _pipe = SendWithRetry(msg, reqHead, reqStm);

            respHead = null;
            respStm  = null;
            // Read response
            if (_pipe != null)
            {
                _pipe.BeginReadMessage();
                respHead = _pipe.ReadHeaders();
                respHead["__CustomErrorsEnabled"] = false;
                respStm = _pipe.ReadStream();
                _pipe.EndReadMessage();

                if (_pipeConnectionPool != null)
                {
                    _pipeConnectionPool.ReturnToPool(_pipe);
                }

                _pipe = null;
            }
        }
        public void StartListening(Object data)
        {
            DBG.Info(null, "Starting to listen...");

            _listener = new Thread(new ThreadStart(this.ListenerMain));
            _listener.IsBackground = true;
            _listener.Start();
        }
 public void StopListening(Object data)
 {
     if (_listener != null)
     {
         DBG.Info(null, "Stop the listening thread...");
         _listener.Abort();
         _listener = null;
     }
 }
示例#5
0
        // IChannelSender
        public IMessageSink CreateMessageSink(String url, Object data, out String objuri)
        {
            DBG.Info(null, "CreateMessageSink: url = " + url);
            // Set the out parameters
            objuri = null;
            String chanuri = null;

            if (url != null) // Is this a well known object?
            {
                /*
                 * String urlCompare = String.ToLower(url);
                 *
                 * // Starts with pipe:// ?
                 * if (urlCompare.StartsWith(ChannelScheme) == false)
                 * {
                 *  return null;
                 * }
                 */

                // Parse returns null if this is not one of the pipe channel url's
                chanuri = Parse(url, out objuri);
            }
            else if (data != null)
            {
                IChannelDataStore cds = data as IChannelDataStore;
                if (cds != null)
                {
                    DBG.Info(null, "ChannelUris[0] = " + cds.ChannelUris[0]);
                    //Console.WriteLine("Channel Uri {0}", cds.ChannelUris[0]);

                    chanuri = Parse(cds.ChannelUris[0], out objuri);
                    DBG.Info(null, "CreateMessageSink: chanuri = " + chanuri + ", objuri = " + objuri);
                    if (chanuri != null)
                    {
                        url = cds.ChannelUris[0];
                    }
                }
            }

            if (null != chanuri)
            {
                if (url == null)
                {
                    url = chanuri;
                }

                DBG.Info(null, "CreateMessageSink: delegating w/ url = " + url);
                //Console.WriteLine("CreateMessageSink: delegating w/ url =  {0}", url);

                return((IMessageSink)_clientSinkProvider.CreateSink(this, url, data));
            }

            DBG.Info(null, "CreateMessageSink: ignoring request...");
            return(null);
        } // CreateMessageSink
        public void Write(String str)
        {
            DBG.Info(null, _handle + "> Write string " + str);

            if (str == null)
            {
                str = "";
            }

            _writer.Write(str); // Length prefixed string
        }
        public void Write(byte[] buffer, int length)
        {
            DBG.Info(null, _handle + "> Write buffer " + length);

            _writer.Write(length);
            _writer.Write(buffer);

            _written += length;

            DBG.Info(null, _handle + ">\t\twritten = " + _written);
            DBG.Info(null, _handle + "> Write finished.");
        }
        public void Dispose()
        {
            if (_handle != 0)
            {
                PipeNative.FlushFileBuffers(_handle);
                PipeNative.DisconnectNamedPipe(_handle);
                PipeNative.CloseHandle(_handle);

                _handle = 0;

                DBG.Info("PipeChannel.Dispose", _pipeName);
            }
        }
示例#9
0
        internal PipeClientTransportSink(String url)
        {
            String objuri  = null;
            String chanuri = PipeConnection.Parse(url, out objuri);

            DBG.Info(null, "PipeClientTransportSink: creating pipe on uri: " + chanuri);
            //Console.WriteLine("PipeClientTransportSink {0}", chanuri);

            _pipeName           = chanuri;
            _pipeConnectionPool = PipeConnectionPoolManager.LookupPool(_pipeName);

            callback = new WaitCallback(this.ReceiveCallback);
        }
        // IChannelReceiver
        public String[] GetUrlsForUri(String objuri)
        {
            DBG.Info(null, "GetUrlsForUri: Looking up URL for uri = " + objuri);
            String[] arr = new String[1];

            if (!objuri.StartsWith("/"))
            {
                objuri = "/" + objuri;
            }

            arr[0] = ChannelScheme + "://" + m_pipeName + objuri;

            return(arr);
        }
        public Stream ReadStream()
        {
            DBG.Info(null, _handle + "> Read stream...");

            int length = _reader.ReadInt32();

            DBG.Info(null, _handle + "> Read stream len " + length);

            byte[] buffer = _reader.ReadBytes(length);

            MemoryStream ms = new MemoryStream(buffer, false);

            return(ms);
        }
        internal static String Parse(String url, out String objuri)
        {
            String pipename = null;

            DBG.Info(null, "Parse: IN: url = " + url);

            // Format is pipe://pipename/objname

            //TODO Look for pipe: scheme prefix
            String urlCompare = url.ToLower();

            // Starts with pipe:// ?
            if (urlCompare.StartsWith(ChannelScheme) == false)
            {
                objuri = null;
                return(null);
            }

            // Parse out the ://
            int start = url.IndexOf("://");

            start += 3;

            int end = url.IndexOf("/", start);

            if (end != -1)
            {
                pipename = url.Substring(start, end - start);
                objuri   = url.Substring(end + 1);
            }
            else
            {
                pipename = url.Substring(start);
                objuri   = null;
            }

            DBG.Info(null, "Parse: OUT: pipename = " + pipename + ", objuri = " + objuri);



            return(objuri);
        }
        private static void PipeConnectionPoolManagerCallback(Object state)
        {
            DBG.Info(null, "PipeConnectionPoolManagerCallback");
            lock (_poolInstances)
            {
                foreach (DictionaryEntry entry in _poolInstances)
                {
                    PipeConnectionPool pool = (PipeConnectionPool)entry.Value;

                    lock (pool)
                    {
                        pool.CloseStaleConnections();
                    }
                }
            }

            // Requeue timer

            timer = new Timer(timerDelegate, null, timerDueTime, 0);
        }
        public void Write(Stream str)
        {
            DBG.Info(null, _handle + "> Write stream " + str.Length);

            //int  chunk = 4096;
            int  chunk = 128;
            long len   = chunk;

            byte[] buffer = new byte[chunk];

            _writer.Write((int)(str.Length));

            while (len != 0)
            {
                len = str.Read(buffer, 0, chunk);
                if (len > 0)
                {
                    _writer.Write(buffer);
                }
            }
        }
        ////////////////////////////////////////////////////////////

        public void CloseStaleConnections()
        {
            ArrayList _activeConnections = new ArrayList();

            foreach (Object obj in _list)
            {
                PipeConnection _pipe = (PipeConnection)obj;

                if (_pipe.IsConnectionStale() == true)
                {
                    DBG.Info(null, "Disposing of stale connection");
                    _pipe.Dispose();
                    _pipe = null;
                }
                else
                {
                    _activeConnections.Add(_pipe);
                }
            }

            _list = _activeConnections;
        }
示例#16
0
        public PipeConnection SendWithRetry(IMessage msg,
                                            ITransportHeaders reqHead,
                                            Stream reqStm)
        {
            IMethodCallMessage mcm = (IMethodCallMessage)msg;

            String         uri   = mcm.Uri;
            PipeConnection _pipe = null;

            int  tryCount       = _defaultRetryCount;
            long reqStmPosition = -1;

            if (reqStm.CanSeek == false)
            {
                tryCount = 1;
            }
            else
            {
                reqStmPosition = reqStm.Position;
            }

            while (tryCount > 0)
            {
                try
                {
                    if (_pipeConnectionPool != null)
                    {
                        DBG.Info(null, "Look in pipe connection in pool");
                        _pipe = (PipeConnection)_pipeConnectionPool.Obtain();
                    }

                    // otherwise create a new connection
                    if (_pipe == null)
                    {
                        _pipe = new PipeConnection(_pipeName, false, IntPtr.Zero);
                    }

                    //Send with Retry
                    _pipe.BeginWriteMessage();
                    _pipe.WriteHeaders(uri, reqHead);
                    _pipe.Write(reqStm);
                    _pipe.EndWriteMessage();

                    tryCount = 0;
                }
                catch (PipeIOException pe)
                {
                    pe = pe;
                    if (_pipe != null)
                    {
                        _pipe.Dispose();
                        _pipe = null;
                    }


                    tryCount--;
                    reqStm.Position = reqStmPosition;
                }
            }

            return(_pipe);
        }
 public void Write(int val)
 {
     DBG.Info(null, _handle + "> Write int " + val);
     _writer.Write(val);
 }
        private void ServerMain()
        {
            PipeConnection pipe = _pipe;

            _pipe = null;

            // Signal the guy to start waiting again... (w/ new event and endpoint)
            _event.Set();

            try
            {
                //TODO close the connection on a timeout
                //TODO if no activity for Nnnn milliseconds
                while (true)
                {
                    pipe.BeginReadMessage();
                    ITransportHeaders headers = pipe.ReadHeaders();
                    headers["__CustomErrorsEnabled"] = false;

                    Stream request = pipe.ReadStream();
                    pipe.EndReadMessage();

                    ServerChannelSinkStack stack = new ServerChannelSinkStack();
                    stack.Push(_transportSink, null);

                    IMessage          responseMsg;
                    ITransportHeaders responseHeaders;
                    Stream            responseStream;

                    ServerProcessing processing = _transportSink.NextChannelSink.ProcessMessage(stack,
                                                                                                null,
                                                                                                headers,
                                                                                                request,
                                                                                                out responseMsg,
                                                                                                out responseHeaders,
                                                                                                out responseStream);

                    // handle response
                    switch (processing)
                    {
                    case ServerProcessing.Complete:
                        // Send the response. Call completed synchronously.
                        stack.Pop(_transportSink);
                        WriteClientResponse(pipe, responseHeaders, responseStream);
                        break;

                    case ServerProcessing.OneWay:
                        break;

                    case ServerProcessing.Async:
                        stack.StoreAndDispatch(_transportSink, null);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                DBG.Info(null, "Terminating client connection: " + e.Message);
                WriteExceptionResponse(pipe, e.ToString());
            }
            finally
            {
                if (pipe != null)
                {
                    pipe.Dispose();
                }
            }
        }
        public byte[] ReadBytes(int cb)
        {
            DBG.Info(null, _handle + "> Reading bytes len " + cb);

            return(_reader.ReadBytes(cb));
        }
 public ushort ReadUShort()
 {
     DBG.Info(null, _handle + "> Reading ushort...");
     return(_reader.ReadUInt16());
 }
 public void Write(ushort val)
 {
     DBG.Info(null, _handle + "> Write ushort " + val);
     _writer.Write(val);
 }
 public String ReadString()
 {
     DBG.Info(null, _handle + "> Reading string...");
     return(_reader.ReadString());
 }
 public int ReadInt()
 {
     DBG.Info(null, _handle + "> Reading int...");
     return(_reader.ReadInt32());
 }