// Remove serverContext from context collection.
        internal void RemoveServerContext(RpceServerContext serverContext)
        {
            string key = BuildServerContextKey(serverContext.ProtocolSequence, serverContext.Endpoint);

            lock (this.serverContexts)
            {
                if (this.serverContexts.ContainsKey(key))
                {
                    this.serverContexts.Remove(key);
                }
            }
        }
 // Add a server context to context collection.
 internal void AddServerContext(RpceServerContext serverContext)
 {
     string key = BuildServerContextKey(serverContext.ProtocolSequence, serverContext.Endpoint);
     lock (this.serverContexts)
     {
         if (this.serverContexts.ContainsKey(key))
         {
             throw new InvalidOperationException("Server context already exists.");
         }
         this.serverContexts.Add(key, serverContext);
     }
 }
        // Add a server context to context collection.
        internal void AddServerContext(RpceServerContext serverContext)
        {
            string key = BuildServerContextKey(serverContext.ProtocolSequence, serverContext.Endpoint);

            lock (this.serverContexts)
            {
                if (this.serverContexts.ContainsKey(key))
                {
                    throw new InvalidOperationException("Server context already exists.");
                }
                this.serverContexts.Add(key, serverContext);
            }
        }
 /// <summary>
 /// Initialize a RpceTransportEvent.
 /// </summary>
 /// <param name="type">the type of the occurred event.</param>
 /// <param name="remoteEndpoint">the remote endpoint from which the event occurred.</param>
 /// <param name="localEndpoint">the local endpoint from which the event occurred.</param>
 /// <param name="serverContext">The server context.</param>
 /// <param name="sessionContext">The session context.</param>
 /// <param name="pdu">A received PDU.</param>
 internal RpceTransportEvent(
     EventType type,
     object remoteEndpoint,
     object localEndpoint,
     RpceServerContext serverContext,
     RpceServerSessionContext sessionContext,
     RpcePdu pdu) : base(type, remoteEndpoint, localEndpoint, pdu)
 {
     //because sessionContext might be null when accept a connection, we must pass it in.
     this.serverContext  = serverContext;
     this.sessionContext = sessionContext;
     this.pdu            = pdu;
 }
 /// <summary>
 /// Initialize a RpceTransportEvent.
 /// </summary>
 /// <param name="type">the type of the occurred event.</param>
 /// <param name="remoteEndpoint">the remote endpoint from which the event occurred.</param>
 /// <param name="localEndpoint">the local endpoint from which the event occurred.</param>
 /// <param name="serverContext">The server context.</param>
 /// <param name="sessionContext">The session context.</param>
 /// <param name="pdu">A received PDU.</param>
 internal RpceTransportEvent(
     EventType type,
     object remoteEndpoint,
     object localEndpoint,
     RpceServerContext serverContext,
     RpceServerSessionContext sessionContext,
     RpcePdu pdu)
     : base(type, remoteEndpoint, localEndpoint, pdu)
 {
     //because sessionContext might be null when accept a connection, we must pass it in.
     this.serverContext = serverContext;
     this.sessionContext = sessionContext;
     this.pdu = pdu;
 }
예제 #6
0
        /// <summary>
        /// Initialize Rpce server context
        /// </summary>
        /// <param name="serverContext">The rpce server context.</param>
        /// <param name="remoteEndpoint">Remote endpoint.</param>
        internal RpceServerSessionContext(
            RpceServerContext serverContext,
            object remoteEndpoint)
            : base()
        {
            this.serverContext = serverContext;

            this.ProtocolSequence = serverContext.ProtocolSequence;
            this.RpcVersionMajor  = serverContext.RpcVersionMajor;
            this.RpcVersionMinor  = serverContext.RpcVersionMinor;

            // Make handle always different.
            handle = new IntPtr(Environment.TickCount);

            this.remoteEndpoint       = remoteEndpoint;
            this.outstandingCalls     = new List <uint>();
            this.associateGroupIdList = new List <uint>();
        }
        /// <summary>
        /// Initialize Rpce server context
        /// </summary>
        /// <param name="serverContext">The rpce server context.</param>
        /// <param name="remoteEndpoint">Remote endpoint.</param>
        internal RpceServerSessionContext(
            RpceServerContext serverContext,
            object remoteEndpoint)
            : base()
        {
            this.serverContext = serverContext;

            this.ProtocolSequence = serverContext.ProtocolSequence;
            this.RpcVersionMajor = serverContext.RpcVersionMajor;
            this.RpcVersionMinor = serverContext.RpcVersionMinor;

            // Make handle always different.
            handle = new IntPtr(Environment.TickCount);

            this.remoteEndpoint = remoteEndpoint;
            this.outstandingCalls = new List<uint>();
            this.associateGroupIdList = new List<uint>();
        }
        public virtual RpceServerContext StartTcp(ushort port)
        {
            lock (this.tcpThreadLocker)
            {
                RpceServerContext serverContext = this.serverContextManager.LookupServerContext(
                    RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE,
                    port.ToString());

                if (serverContext != null)
                {
                    throw new InvalidOperationException("The server with the port has been started. Please try other port.");
                }

                serverContext = new RpceServerContext(
                        RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE,
                        port.ToString());

                this.serverContextManager.AddServerContext(serverContext);
                if (this.openedTcpPortList == null)
                {
                    this.openedTcpPortList = new List<ushort>();
                }
                this.openedTcpPortList.Add(port);

                bool ipv4Started = false;
                bool ipv6Started = false;
                Exception ex = null;

                try
                {
                    if (this.tcpTransport == null)
                    {
                        SocketTransportConfig config = new SocketTransportConfig();
                        config.Type = StackTransportType.Tcp;
                        config.Role = Role.Server;
                        config.LocalIpAddress = IPAddress.Any;
                        config.MaxConnections = RpceServerContext.DEFAULT_MAX_CONNECTIONS;
                        config.BufferSize = Math.Max(serverContext.MaxReceiveFragmentSize, serverContext.MaxTransmitFragmentSize);

                        this.tcpTransport = new TransportStack(config, RpceDecodePduCallback);
                    }
                }
                catch (Exception e)
                {
                    ex = e;
                    this.tcpTransport = null;
                }

                try
                {
                    //Start IPv4
                    IPEndPoint ipv4Endpoint = new IPEndPoint(IPAddress.Any, port);
                    this.tcpTransport.Start(ipv4Endpoint);
                    ipv4Started = true;
                }
                catch (Exception e)
                {
                    ex = e;
                }

                //Start IPv6
                try
                {
                    IPEndPoint ipv6Endpoint = new IPEndPoint(IPAddress.IPv6Any, port);
                    this.tcpTransport.Start(ipv6Endpoint);
                    ipv6Started = true;
                }
                catch (Exception e)
                {
                    ex = e;
                }

                if (!ipv4Started && !ipv6Started)
                {
                    this.serverContextManager.RemoveServerContext(serverContext);
                    this.openedTcpPortList.Remove(port);
                    throw new InvalidOperationException("TCP server failed to start.", ex);
                }

                if (this.tcpReceiveThread == null)
                {
                    this.tcpReceiveThread = new Thread(TcpReceiveLoop);
                    this.tcpReceiveThread.Start();
                }

                return serverContext;
            }
        }
        /// <summary>
        /// Start to listen on a named pipe.
        /// </summary>
        /// <param name="namedPipe">The name of named pipe to listen on.</param>
        /// <param name="credential">Credential to be used by underlayer SMB/SMB2 transport.</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when the server with the named pipe has been started.
        /// </exception>
        /// <param name="ipAddress">server's ipAddress</param>
        public virtual RpceServerContext StartNamedPipe(string namedPipe, AccountCredential credential, 
            IPAddress ipAddress)
        {
            namedPipe = namedPipe.ToUpper();

            lock (this.smbThreadLocker)
            {
                RpceServerContext serverContext = this.serverContextManager.LookupServerContext(
                    RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE,
                    namedPipe);

                if (serverContext != null)
                {
                    throw new InvalidOperationException("The server with the named pipe has been started. Please try other port.");
                }

                serverContext = new RpceServerContext(
                        RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE,
                        namedPipe,
                        credential);

                this.serverContextManager.AddServerContext(serverContext);
                if (this.openedNamedPipeList == null)
                {
                    this.openedNamedPipeList = new List<string>();
                }
                this.openedNamedPipeList.Add(namedPipe);

                try
                {
                    if (this.smbTransport == null)
                    {
                        this.smbTransport = new Smb.SmbServerTransport();
                        this.smbTransport.Start(RpceUtility.NAMED_PIPE_PORT, credential, ipAddress);
                    }
                }
                catch
                {
                    this.serverContextManager.RemoveServerContext(serverContext);
                    this.openedNamedPipeList.Remove(namedPipe);
                    throw;
                }

                if (this.smbReceiveThread == null)
                {
                    this.smbReceiveThread = new Thread(SmbReceiveLoop);
                    this.smbReceiveThread.Start();
                }

                return serverContext;
            }
        }
 // Remove serverContext from context collection.
 internal void RemoveServerContext(RpceServerContext serverContext)
 {
     string key = BuildServerContextKey(serverContext.ProtocolSequence, serverContext.Endpoint);
     lock (this.serverContexts)
     {
         if (this.serverContexts.ContainsKey(key))
         {
             this.serverContexts.Remove(key);
         }
     }
 }