示例#1
0
        /// <summary>
        /// Release resources.
        /// </summary>
        /// <param name = "disposing">
        /// If disposing equals true, managed and unmanaged resources are disposed. if false, Only unmanaged resources
        /// can be disposed.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    // Free managed resources & other reference types:
                    if (this.thread != null)
                    {
                        this.thread.Dispose();
                        this.thread = null;
                    }
                    if (this.transport != null)
                    {
                        // the netbios transport may throw exception, donot arise exception.
                        Utility.SafelyDisconnectNetbiosConnection(this.transport, this.remoteEndPoint);
                        this.transport = null;
                    }
                    if (this.buffer != null)
                    {
                        this.buffer.Dispose();
                        this.buffer = null;
                    }
                }

                // Call the appropriate methods to clean up unmanaged resources.
                // If disposing is false, only the following code is executed:

                this.disposed = true;
            }
        }
        /// <summary>
        /// Release resources.
        /// </summary>
        /// <param name = "disposing">
        /// If disposing equals true, managed and unmanaged resources are disposed. if false, Only unmanaged resources
        /// can be disposed.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    // Free managed resources & other reference types:
                    if (this.thread != null)
                    {
                        this.thread.Dispose();
                        this.thread = null;
                    }
                    if (this.transport != null)
                    {
                        this.transport.Dispose();
                        this.transport = null;
                    }
                }

                // Call the appropriate methods to clean up unmanaged resources.
                // If disposing is false, only the following code is executed:

                this.disposed = true;
            }
        }
示例#3
0
        /// <summary>
        /// connect to remote endpoint.<para/>
        /// the underlayer transport must be NetbiosTransport, UdpClient or NetbiosClient.
        /// </summary>
        /// <returns>
        /// the remote endpoint of the connection.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// thrown when netbios client is connected to server.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the received thread does not cleanup.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        public object Connect()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosClientTransport");
            }

            if (this.transport != null)
            {
                throw new InvalidOperationException("netbios client is connected to server.");
            }

            if (this.thread != null)
            {
                throw new InvalidOperationException("the received thread does not cleanup.");
            }

            this.buffer = new BytesBuffer();

            this.eventQueue.Clear();
            this.packetCache.Clear();

            this.transport = new NetbiosTransport(
                this.netbiosConfig.LocalNetbiosName, this.netbiosConfig.AdapterIndex, (ushort)this.netbiosConfig.BufferSize,
                (byte)this.netbiosConfig.MaxSessions, (byte)this.netbiosConfig.MaxNames);

            this.remoteEndPoint = this.transport.Connect(this.netbiosConfig.RemoteNetbiosName);
            this.localEndPoint  = this.transport.NcbNum;

            this.thread = new ThreadManager(NetbiosClientReceiveLoop, UnblockReceiveThread);
            this.thread.Start();

            return(this.remoteEndPoint);
        }
示例#4
0
        /// <summary>
        /// disconnect from remote host.<para/>
        /// the underlayer transport must be NetbiosTransport, NetbiosClient, NetbiosServer or NetbiosServer.<para/>
        /// client side will disconnect the connection to server.<para/>
        /// server side will disconnect all client connection.
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when netbios client is not connected to server, must invoke Connect() first.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the received thread does not initialize.
        /// </exception>
        public void Disconnect()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosClientTransport");
            }

            if (this.transport == null)
            {
                throw new InvalidOperationException(
                          "netbios client is not connected to server, must invoke Connect() first.");
            }

            if (this.thread == null)
            {
                throw new InvalidOperationException("the received thread does not initialize.");
            }

            this.transport.Disconnect(this.remoteEndPoint);

            this.thread.Dispose();
            this.thread = null;

            this.transport.Dispose();
            this.transport = null;
        }
        /// <summary>
        /// remove the transport instance from transports.<para/>
        /// when dispose the last instance, reset the hasInitialized.
        /// </summary>
        /// <param name="transport">
        /// a NetbiosTransport object that is used to remove from pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when transport is null.
        /// </exception>
        public static void Remove(NetbiosTransport transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            lock (netbiosEnvLocker)
            {
                byte networkAdapterId = transport.NetworkAdapterId;

                if (hasInitialized.ContainsKey(networkAdapterId))
                {
                    hasInitialized[networkAdapterId]--;

                    // the next instance will reset the adapter.
                    if (hasInitialized[networkAdapterId] == 0)
                    {
                        hasInitialized.Remove(networkAdapterId);
                    }
                }

                // unregister the name.
                transport.UnRegisterName();
            }
        }
        /// <summary>
        /// start the listener and then start a thread to accept connection from client.
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the listener on the endpoint has been started.
        /// </exception>
        public void Start()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosServerListener");
            }

            if (this.thread != null)
            {
                throw new InvalidOperationException("the listener on the endpoint has been started.");
            }

            if (this.transport == null)
            {
                this.transport = new NetbiosTransport(
                    this.netbiosName, this.server.NetbiosConfig.AdapterIndex,
                    (ushort)this.server.NetbiosConfig.BufferSize, (byte)this.server.NetbiosConfig.MaxSessions,
                    (byte)this.server.NetbiosConfig.MaxNames);
            }

            this.localEndPoint = this.transport.NcbNum;

            this.thread = new ThreadManager(AcceptLoop, Unblock);
            this.thread.Start();
        }
示例#7
0
        public static void SafelyDisconnectNetbiosConnection(NetbiosTransport netbiosTransport, int sessionId)
        {
            if (netbiosTransport == null)
            {
                return;
            }

            try
            {
                // when disconnect netbios transport multiple times,
                // it may throw exception.
                netbiosTransport.Disconnect(sessionId);
            }
            catch (Exception)
            {
            }
        }
示例#8
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="localEndPoint">
        /// an int value that specifies the session id of connection.
        /// </param>
        /// <param name="remoteEndPoint">
        /// an int value that specifies the session id of connection.
        /// </param>
        /// <param name="server">
        /// NetbiosServerTransport object that specifies the netbios server transport.
        /// </param>
        /// <param name="transport">
        /// a NetbiosTransport object that specifies the owner of client.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when the server is null!
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// thrown when the transport is null!
        /// </exception>
        public NetbiosServerConnection(
            int localEndPoint, int remoteEndPoint, NetbiosServerTransport server, NetbiosTransport transport)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            this.localEndPoint  = localEndPoint;
            this.remoteEndPoint = remoteEndPoint;
            this.server         = server;
            this.transport      = transport;

            this.thread = new ThreadManager(NetbiosServerConnectionReceiveLoop, Unblock);
            this.buffer = new BytesBuffer();
        }
        public static void Visit(
            IVisitorNetbiosReceiveLoop host, ITransport server,
            NetbiosTransport transport, int localEP, int remoteEP, ThreadManager thread)
        {
            byte[] data = null;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    data = transport.Receive(remoteEP);

                    // if the server close the socket, return.
                    if (data == null)
                    {
                        server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray<byte>(data, 0));
                }
                // the connection is disconnected.
                catch (InvalidOperationException ex)
                {
                    server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, ex));

                    break;
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, remoteEP, localEP, ex));

                    break;
                }
            }
        }
        public static void Visit(
            IVisitorNetbiosReceiveLoop host, ITransport server,
            NetbiosTransport transport, int localEP, int remoteEP, ThreadManager thread)
        {
            byte[] data = null;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    data = transport.Receive(remoteEP);

                    // if the server close the socket, return.
                    if (data == null)
                    {
                        server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray <byte>(data, 0));
                }
                // the connection is disconnected.
                catch (InvalidOperationException ex)
                {
                    server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, ex));

                    break;
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, remoteEP, localEP, ex));

                    break;
                }
            }
        }
        /// <summary>
        /// initialize the netbios transport pool.
        /// </summary>
        /// <param name="transport">
        /// a NetbiosTransport object that is used to initialize the pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when transport is null.
        /// </exception>
        public static void Initialize(NetbiosTransport transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            lock (netbiosEnvLocker)
            {
                byte networkAdapterId = transport.NetworkAdapterId;

                if (!hasInitialized.ContainsKey(networkAdapterId))
                {
                    transport.ResetAdapter();
                    hasInitialized[networkAdapterId] = 0;
                }

                hasInitialized[networkAdapterId]++;

                // try to register name, if failed, do not add this instance to pool.
                transport.RegisterName();
            }
        }
        /// <summary>
        /// initialize the netbios transport pool.
        /// </summary>
        /// <param name="transport">
        /// a NetbiosTransport object that is used to initialize the pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when transport is null.
        /// </exception>
        public static void Initialize(NetbiosTransport transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            lock (netbiosEnvLocker)
            {
                byte networkAdapterId = transport.NetworkAdapterId;

                if (!hasInitialized.ContainsKey(networkAdapterId))
                {
                    transport.ResetAdapter();
                    hasInitialized[networkAdapterId] = 0;
                }

                hasInitialized[networkAdapterId]++;

                // try to register name, if failed, do not add this instance to pool.
                transport.RegisterName();
            }
        }
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="localEndPoint">
        /// an int value that specifies the session id of connection.
        /// </param>
        /// <param name="remoteEndPoint">
        /// an int value that specifies the session id of connection.
        /// </param>
        /// <param name="server">
        /// NetbiosServerTransport object that specifies the netbios server transport.
        /// </param>
        /// <param name="transport">
        /// a NetbiosTransport object that specifies the owner of client.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when the server is null!
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// thrown when the transport is null!
        /// </exception>
        public NetbiosServerConnection(
            int localEndPoint, int remoteEndPoint, NetbiosServerTransport server, NetbiosTransport transport)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            this.localEndPoint = localEndPoint;
            this.remoteEndPoint = remoteEndPoint;
            this.server = server;
            this.transport = transport;

            this.thread = new ThreadManager(NetbiosServerConnectionReceiveLoop, Unblock);
            this.buffer = new BytesBuffer();
        }
        /// <summary>
        /// connect to remote endpoint.<para/>
        /// the underlayer transport must be NetbiosTransport, UdpClient or NetbiosClient.
        /// </summary>
        /// <returns>
        /// the remote endpoint of the connection.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// thrown when netbios client is connected to server.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the received thread does not cleanup.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        public object Connect()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosClientTransport");
            }

            if (this.transport != null)
            {
                throw new InvalidOperationException("netbios client is connected to server.");
            }

            if (this.thread != null)
            {
                throw new InvalidOperationException("the received thread does not cleanup.");
            }

            this.buffer = new BytesBuffer();

            this.eventQueue.Clear();
            this.packetCache.Clear();

            this.transport = new NetbiosTransport(
                this.netbiosConfig.LocalNetbiosName, this.netbiosConfig.AdapterIndex, (ushort)this.netbiosConfig.BufferSize,
                (byte)this.netbiosConfig.MaxSessions, (byte)this.netbiosConfig.MaxNames);

            this.remoteEndPoint = this.transport.Connect(this.netbiosConfig.RemoteNetbiosName);
            this.localEndPoint = this.transport.NcbNum;

            this.thread = new ThreadManager(NetbiosClientReceiveLoop, UnblockReceiveThread);
            this.thread.Start();

            return this.remoteEndPoint;
        }
        /// <summary>
        /// disconnect from remote host.<para/>
        /// the underlayer transport must be NetbiosTransport, NetbiosClient, NetbiosServer or NetbiosServer.<para/>
        /// client side will disconnect the connection to server.<para/>
        /// server side will disconnect all client connection.
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when netbios client is not connected to server, must invoke Connect() first.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the received thread does not initialize.
        /// </exception>
        public void Disconnect()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosClientTransport");
            }

            if (this.transport == null)
            {
                throw new InvalidOperationException(
                    "netbios client is not connected to server, must invoke Connect() first.");
            }

            if (this.thread == null)
            {
                throw new InvalidOperationException("the received thread does not initialize.");
            }

            this.transport.Disconnect(this.remoteEndPoint);

            this.thread.Dispose();
            this.thread = null;

            this.transport.Dispose();
            this.transport = null;
        }
        /// <summary>
        /// Release resources. 
        /// </summary>
        /// <param name = "disposing">
        /// If disposing equals true, managed and unmanaged resources are disposed. if false, Only unmanaged resources 
        /// can be disposed. 
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    // Free managed resources & other reference types:
                    if (this.thread != null)
                    {
                        this.thread.Dispose();
                        this.thread = null;
                    }
                    if (this.transport != null)
                    {
                        this.transport.Dispose();
                        this.transport = null;
                    }
                }

                // Call the appropriate methods to clean up unmanaged resources.
                // If disposing is false, only the following code is executed:

                this.disposed = true;
            }
        }
        /// <summary>
        /// start the listener and then start a thread to accept connection from client.
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the listener on the endpoint has been started.
        /// </exception>
        public void Start()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosServerListener");
            }

            if (this.thread != null)
            {
                throw new InvalidOperationException("the listener on the endpoint has been started.");
            }

            if (this.transport == null)
            {
                this.transport = new NetbiosTransport(
                    this.netbiosName, this.server.NetbiosConfig.AdapterIndex,
                    (ushort)this.server.NetbiosConfig.BufferSize, (byte)this.server.NetbiosConfig.MaxSessions,
                    (byte)this.server.NetbiosConfig.MaxNames);
            }

            this.localEndPoint = this.transport.NcbNum;

            this.thread = new ThreadManager(AcceptLoop, Unblock);
            this.thread.Start();
        }
        /// <summary>
        /// remove the transport instance from transports.<para/>
        /// when dispose the last instance, reset the hasInitialized.
        /// </summary>
        /// <param name="transport">
        /// a NetbiosTransport object that is used to remove from pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// thrown when transport is null.
        /// </exception>
        public static void Remove(NetbiosTransport transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            lock (netbiosEnvLocker)
            {
                byte networkAdapterId = transport.NetworkAdapterId;

                if (hasInitialized.ContainsKey(networkAdapterId))
                {
                    hasInitialized[networkAdapterId]--;

                    // the next instance will reset the adapter.
                    if (hasInitialized[networkAdapterId] == 0)
                    {
                        hasInitialized.Remove(networkAdapterId);
                    }
                }

                // unregister the name.
                transport.UnRegisterName();
            }
        }
        /// <summary>
        /// Release resources. 
        /// </summary>
        /// <param name = "disposing">
        /// If disposing equals true, Managed and unmanaged resources are disposed. if false, Only unmanaged resources 
        /// can be disposed. 
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    // Free managed resources & other reference types:
                    if (this.thread != null)
                    {
                        this.thread.Dispose();
                        this.thread = null;
                    }
                    if (this.transport != null)
                    {
                        // the netbios transport may throw exception, donot arise exception.
                        Utility.SafelyDisconnectNetbiosConnection(this.transport, this.remoteEndPoint);
                        this.transport.Dispose();
                        this.transport = null;
                    }
                    if (this.eventQueue != null)
                    {
                        // the SyncFilterQueue may throw exception, donot arise exception.
                        this.eventQueue.Dispose();
                        this.eventQueue = null;
                    }
                    if (this.packetCache != null)
                    {
                        this.packetCache.Dispose();
                        this.packetCache = null;
                    }
                    if (this.buffer != null)
                    {
                        this.buffer.Dispose();
                        this.buffer = null;
                    }
                }

                // Call the appropriate methods to clean up unmanaged resources.
                // If disposing is false, only the following code is executed:

                this.disposed = true;
            }
        }
        /// <summary>
        /// accept the connected NetbiosClient.
        /// </summary>
        /// <param name="localEP">
        /// an int value that specifies the local listening netbios session id.
        /// </param>
        /// <param name="remoteEP">
        /// an int value that specifies the connected netbios client session id.
        /// </param>
        /// <param name="transport">
        /// a NetbiosTransport object that specifies the owner of client.
        /// </param>
        /// <returns>
        /// a NetbiosServerConnection that specifies the connection.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// thrown when invalid connected netbios client.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when invalid endpoint of connected netbios client
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// thrown when the client specified by endpoint exists
        /// </exception>
        internal NetbiosServerConnection AcceptClient(int localEP, int remoteEP, NetbiosTransport transport)
        {
            if (this.connections.ContainsKey(remoteEP))
            {
                throw new InvalidOperationException("the client specified by endpoint exists");
            }

            NetbiosServerConnection connection = new NetbiosServerConnection(localEP, remoteEP, this, transport);

            this.connections.Add(remoteEP, connection);

            return connection;
        }