Exemplo n.º 1
0
        /// <summary>
        /// Create a new udp connection to handle communication to a single endpoint from the specified socket.
        /// </summary>
        public UdpConnection(UdpServer server, Socket socket, IPEndPoint remoteEndpoint)
        {
            // persist the server
            Server         = server;
            RemoteEndPoint = remoteEndpoint;

            _timeoutTimer = new Timer(_timeoutMilliseconds, ActionSet.New(ProcessError, new TimeoutException("UdpConnection timeout.")));

            _header             = new UdpHeader();
            _header.Compression = _defaultCompression = DecompressionMethods.None;

            // create the encoder
            _encoder = Encoding.UTF8.GetEncoder();
            _lock    = new Lock();
            _message = new UdpMessage(this);

            _messages  = new ArrayRig <UdpMessage>();
            _onMessage = new ActionPop <UdpMessage>();

            _errors  = new ArrayRig <Exception>();
            _onError = new ActionPop <Exception>();

            // create an async socket to manage the socket
            AsyncSocket = new AsyncUdpSocket(socket, (IPEndPoint)socket.LocalEndPoint, OnReceived, ProcessError);
            AsyncSocket.TargetEndPoint = RemoteEndPoint;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a new incoming udp connection to handle communication to a single endpoint.
        /// </summary>
        public UdpConnection(UdpServer server, IPEndPoint remoteEndpoint, IPEndPoint localEndpoint, byte[] buffer, int count)
        {
            // persist the server
            Server         = server;
            RemoteEndPoint = remoteEndpoint;

            _timeoutTimer = new Timer(_timeoutMilliseconds, ActionSet.New(ProcessError, new TimeoutException("UdpConnection timeout.")));

            _header             = new UdpHeader();
            _header.Compression = _defaultCompression = DecompressionMethods.None;

            // create the encoder
            _encoder = Encoding.UTF8.GetEncoder();
            _lock    = new Lock();
            _message = new UdpMessage(this);

            _messages  = new ArrayRig <UdpMessage>();
            _onMessage = new ActionPop <UdpMessage>();

            _errors  = new ArrayRig <Exception>();
            _onError = new ActionPop <Exception>();

            // call on received for the first buffer
            OnReceived(RemoteEndPoint, buffer, count);

            // create a socket instance for the udp connection
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // create an async socket to manage the socket
            AsyncSocket = new AsyncUdpSocket(socket, localEndpoint, OnReceived, ProcessError);
            AsyncSocket.TargetEndPoint = RemoteEndPoint;
        }
Exemplo n.º 3
0
        //----------------------------------//

        /// <summary>
        /// Start a web site and server with the specified path as the root directory.
        /// </summary>
        public HttpSite(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                Log.Warning("No path specified for site. A path should be set.");
            }

            // persist and clean the path
            Path = Fs.Combine(path);

            // get the configuration for the web site
            ManagerResources.LoadConfig(Fs.Combine(Path, "site.config"), new Act <Configuration>(OnConfiguration));

            // create the authentication dictionary
            Authentication = new Dictionary <string, HttpRequirement>();
            // create the redirects collection
            Redirects = new Dictionary <string, HttpRedirect>();

            // create the cache
            _cache = new Cache <string, WebResource>(Global.Megabyte * 100, r => r.Size);

            _onAccessDenied    = new ActionPop <HttpRequest>();
            _onInvalidResource = new ActionPop <HttpRequest>();

            _defaultSendOptions = new HttpSendOptions {
                ContentType = "text/html"
            };
        }
Exemplo n.º 4
0
        /// <summary>
        /// Construct and start a new web server.
        /// </summary>
        public HttpServer(Configuration config, IAction <HttpConnection> onConnection)
        {
            // persist the configuration reference
            _config = config;

            // get the server name
            _name = _config.GetString("Name", "Efz");

            if (onConnection != null)
            {
                _onConnection = new ActionPop <HttpConnection>(onConnection);
            }

            if (config["Port"].Int32 > 0)
            {
                Port = config["Port"].Int32;
            }

            // create the clients collection
            Connections = new Capsule <HttpConnection>();
            Clients     = new Capsule <HttpClient>();

            _name = "Efz";

            ListenAddress = IPAddress.Parse(config["Address"].String) ?? IPAddress.Any;
            _onClient     = new ActionPop <TcpClient>(OnClient);
            _stopped      = true;
        }
Exemplo n.º 5
0
 /// <summary>
 /// Initialize a new path resolver.
 /// </summary>
 public HttpRequestResolver(Action <string, HttpRequest> defaultCallback) : this()
 {
     if (defaultCallback == null)
     {
         throw new ArgumentNullException("defaultCallback");
     }
     DefaultCallback = new ActionPop <string, HttpRequest>(defaultCallback);
 }
Exemplo n.º 6
0
        //----------------------------------//

        /// <summary>
        /// Create a new server listening to the specified endpoint.
        /// </summary>
        public UdpServer(IPEndPoint localEndpoint, string name = "Efz")
        {
            _connections  = new Shared <Dictionary <IPEndPoint, UdpConnection> >(new Dictionary <IPEndPoint, UdpConnection>());
            _onConnection = new ActionPop <UdpConnection>();

            _name    = name;
            _stopped = true;
            _flags   = SocketFlags.None;

            LocalEndpoint = localEndpoint;

            // create the server socket
            _socket     = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            AsyncSocket = new AsyncUdpSocket(_socket, LocalEndpoint, OnReceive, OnError);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a new tcp server.
        /// </summary>
        public HttpServer(int listenPort, IPAddress listenAddress)
        {
            if (listenPort > 0)
            {
                Port = listenPort;
            }

            // create the clients collection
            Connections = new Capsule <HttpConnection>();
            Clients     = new Capsule <HttpClient>();

            _name = "Efz";

            ListenAddress = listenAddress ?? IPAddress.Any;
            _onClient     = new ActionPop <TcpClient>(OnClient);
            _stopped      = true;
        }
Exemplo n.º 8
0
        //----------------------------------//

        /// <summary>
        /// Construct a new web context for the specified alchemy user context.
        /// </summary>
        public HttpClient(HttpServer server)
        {
            // persist the server
            Server = server;

            _timeout = new Timer(_timeoutMilliseconds, Dispose);

            OnRequest   = new ActionPop <HttpRequest>();
            OnErrorRoll = new ActionRoll <Exception>();

            // create the encoder and decoder
            _encoder = Encoding.UTF8.GetEncoder();

            _requests = new ArrayRig <HttpRequest>();

            Connections = new Capsule <HttpConnection>();

            _lock = new Lock();

            Log.Info("New connection '" + this + "'.");
        }
Exemplo n.º 9
0
 ActionBase IActionVisitor <XElement, ActionBase> .Visit(ActionPop action, XElement xAction)
 {
     return(action);
 }
Exemplo n.º 10
0
 public Route(HttpMethod methods, IAction <string, HttpRequest> callback)
 {
     Methods   = methods;
     OnResolve = new ActionPop <string, HttpRequest>(callback);
 }
Exemplo n.º 11
0
 XElement IActionVisitor <XElement, XElement> .Visit(ActionPop action, XElement arg)
 {
     return(arg);
 }