Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Flexible Plugin System

, 3 Sep 2008 LGPL3
A generic plugin system used to load and manage plugins
fadd-15373.zip
trunk
dlls
xunit.dll
Examples
Plugins
ExampleApplication.Shared
Properties
ExampleApplication
Properties
ExamplePlugin.Shared
Properties
ExamplePlugin
Properties
Fadd.Globalization.Yaml
fadd.snk
Properties
Tests
fadd
Commands
Net
Tests
fadd.snk
Globalization
Tests
Logging
Plugins
Properties
Tests
Validation
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace Fadd.Commands.Net
{
    /// <summary>
    /// Receives commands from the client and invoked them in the server dispatcher.
    /// </summary>
    public class CommandServer
    {
        private TcpListener _listener;
        private readonly List<CommandChannel> _clients = new List<CommandChannel>();
        private readonly CommandManager _cmdMgr;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandServer"/> class.
        /// </summary>
        /// <param name="mgr">Manager that the incoming commands are invoked in.</param>
        public CommandServer(CommandManager mgr)
        {
            Check.Require(mgr, "mgr");
            _cmdMgr = mgr;
        }

        /// <summary>
        /// Starts the server.
        /// </summary>
        /// <param name="address">IP address that we accept incoming connections on.</param>
        /// <param name="port">Port that the server accepts new connections on.</param>
        public void Start(IPAddress address, int port)
        {
            if (_listener != null)
                throw new InvalidOperationException("CommandServer is already started.");

            _listener = new TcpListener(address, port);
            _listener.Start(10);
            _listener.BeginAcceptSocket(OnAcceptSocket, null);
        }

        /// <summary>
        /// Stops the server.
        /// </summary>
        public void Stop()
        {
            lock (_clients)
            {
                foreach (CommandChannel client in _clients)
                    client.Dispose();
                _clients.Clear();                
            }

            _listener.Stop();
        }

        private void OnAcceptSocket(IAsyncResult ar)
        {
            Socket socket;
            try
            {
                socket = _listener.EndAcceptSocket(ar);
                _listener.BeginAcceptSocket(OnAcceptSocket, null);
            }
            catch (SocketException)
            {
                return;
            }
            catch(ObjectDisposedException)
            {
                return;
            }

            CommandChannel tunnel = new CommandChannel(_cmdMgr, socket);
            tunnel.Disconnected += OnClientDisconnected;
            tunnel.AutoReconnect = false;
            lock (_clients)
                _clients.Add(tunnel);
        }

        private void OnClientDisconnected(object source, DisconnectedEventArgs args)
        {
            lock (_clients)
            {
                CommandChannel tunnel = (CommandChannel) source;
                tunnel.Disconnected -= OnClientDisconnected;
                _clients.Remove(tunnel);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid