Exemplo n.º 1
0
        /// <summary>
        /// Handles the given package.
        /// </summary>
        /// <param name="package">The Package.</param>
        private void HandlePackage(IBasePackage package)
        {
            var binaryPackage = package as BinaryPackage;

            if (binaryPackage != null)
            {
                //notify package listeners
                foreach (IPackageListener subscriber in GetPackageSubscriber(binaryPackage.OriginType))
                {
                    subscriber.OnPackageReceived(binaryPackage);
                }

                //send the package to its destination
                if (binaryPackage.Receiver == null)
                {
                    Send(binaryPackage);
                }
                else
                {
                    Send(binaryPackage, binaryPackage.Receiver);
                }

                return;
            }

            //system package pingpackage
            var pingPackage = package as PingPackage;

            if (pingPackage != null)
            {
                SetLatency(pingPackage);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         _udpClient.Client.SendTo(mStream.ToArray(), new IPEndPoint(_ip, 2563));
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Handles a connection.
        /// </summary>
        /// <param name="objConnection">The Connection.</param>
        private void HandleClient(object objConnection)
        {
            var           localConnection = (LocalConnection)objConnection;
            NetworkStream networkStream   = localConnection.Client.GetStream();

            while (localConnection.Connected)
            {
                if (localConnection.Client.Available > 0)
                {
                    //Reset idle
                    _idleTimeout = 0;
                    _currentIdle = 0;
                    IBasePackage package       = PackageSerializer.Deserialize(networkStream);
                    var          binaryPackage = package as BinaryPackage;
                    if (binaryPackage != null)
                    {
                        //notify package listeners
                        foreach (IPackageListener subscriber in GetPackageSubscriber(binaryPackage.OriginType))
                        {
                            subscriber.OnPackageReceived(binaryPackage);
                        }

                        //The package is not a system package, send it to it's destination
                        if (binaryPackage.Receiver == null)
                        {
                            //Send to all Clients
                            Send(binaryPackage);
                        }
                        else
                        {
                            //Special destination
                            Send(binaryPackage, binaryPackage.Receiver);
                        }
                        return;
                    }

                    //system package with type of pingpackage
                    var pingPackage = package as PingPackage;
                    if (pingPackage != null)
                    {
                        SetLatency(pingPackage);
                        return;
                    }
                }
                else
                {
                    Idle();
                }
            }

            //Client exited.
            SendNotificationPackage(NotificationMode.ClientExited,
                                    new IConnection[] { SerializableConnection.FromIConnection(localConnection) });
            _connections.Remove(localConnection);

            IConnection[] connectionList = SerializableConnection.FromIConnection(_connections.ToArray());
            SendNotificationPackage(NotificationMode.ClientList, connectionList);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     package.Receiver = receiver;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         _udpClient.Client.SendTo(mStream.ToArray(), new IPEndPoint(_ip, 2563));
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Sends a package to a specified client.
        /// </summary>
        /// <param name="package">The Package.</param>
        /// <param name="connection">The Connection</param>
        private void SendTo(IBasePackage package, IPAddress connection)
        {
            LocalConnection localConnection = GetConnection(connection);

            if (localConnection != null)
            {
                PackageSerializer.Serialize(package, localConnection.Client.GetStream());
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     byte[] result;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         result = mStream.ToArray();
     }
     _listener.Client.SendTo(result, new IPEndPoint(receiver, 2565));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Sends a package to all clients.
 /// </summary>
 /// <param name="package">The Package.</param>
 private void SendToAllClients(IBasePackage package)
 {
     for (int i = 0; i <= _connections.Count - 1; i++)
     {
         NetworkStream networkStream = _connections[i].Client.GetStream();
         PackageSerializer.Serialize(package, networkStream);
         //May not be neccessary
         networkStream.Flush();
     }
 }
        public ReduxStyleForm(IBasePackage <State> store, string initialUrl)
            : base(initialUrl)
        {
            Store = store;

            Store.Subscribe((subscription, action) =>
            {
                var state  = store.GetState();
                string cmd = string.Format(jsDispatchMethod, JsonConvert.SerializeObject(state));
                ExecuteJavascript(cmd);
            });
        }
Exemplo n.º 9
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     byte[] result;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         result = mStream.ToArray();
     }
     for (int i = 0; i <= _connections.Count - 1; i++)
     {
         _listener.Client.SendTo(result, new IPEndPoint(_connections[i].IPAddress, 2565));
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     if (!_tcpClient.Connected)
     {
         throw new InvalidOperationException("The client is not connected.");
     }
     try
     {
         PackageSerializer.Serialize(package, _nStream);
         _nStream.Flush();
     }
     catch (Exception ex)
     {
         _logger.Error(ex.Message);
     }
 }
Exemplo n.º 11
0
 public static void Register(IBasePackage <AppStore> store)
 {
     store.Middleware(
         curStore => next => action =>
     {
         next(action);
     },
         curStore => next => action =>
     {
         if (!(action is ReSetState))
         {
             curStore.Dispatch(new ReSetState());
         }
         next(action);
     }
         );
 }
Exemplo n.º 12
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     SendToAllClients(package);
 }
Exemplo n.º 13
0
        /// <summary>
        /// Accepts clients if available.
        /// </summary>
        private void BeginAcceptConnections()
        {
            var incommingConnection = new IPEndPoint(IPAddress.Any, 2565);

            while (IsActive)
            {
                try
                {
                    if (_listener.Available > 0)
                    {
                        byte[] receivedData = _listener.Receive(ref incommingConnection);
                        using (var mStream = new MemoryStream(receivedData))
                        {
                            IBasePackage package    = PackageSerializer.Deserialize(mStream);
                            var          udpPackage = package as UdpPackage;
                            if (udpPackage != null)
                            {
                                if (udpPackage.NotifyMode == UdpNotify.Hi)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientJoined, _connections.ToArray());
                                    //add connection
                                    var udpConnection = new UdpConnection(incommingConnection.Address);
                                    _connections.Add(udpConnection);
                                    // publish the new server list, after a new connection.
                                    SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                }
                                else //(UdpNotify.Bye)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientExited, _connections.ToArray());
                                    //remove connection
                                    UdpConnection udpConnection = GetConnection(incommingConnection.Address);
                                    if (udpConnection != null)
                                    {
                                        _connections.Remove(udpConnection);
                                        // publish the new server list, after a lost connection.
                                        SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                    }
                                }
                            }
                            else
                            {
                                //any other package handle in new void
                                HandlePackage(package);
                            }
                        }
                    }
                    else
                    {
                        //no data available

                        Idle();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Handles the given package.
        /// </summary>
        /// <param name="package">The Package.</param>
        private void HandlePackage(IBasePackage package)
        {
            var binaryPackage = package as BinaryPackage;
            if (binaryPackage != null)
            {
                //notify package listeners
                foreach (IPackageListener subscriber in GetPackageSubscriber(binaryPackage.OriginType))
                {
                    subscriber.OnPackageReceived(binaryPackage);
                }

                //send the package to its destination
                if (binaryPackage.Receiver == null)
                {
                    Send(binaryPackage);
                }
                else
                {
                    Send(binaryPackage, binaryPackage.Receiver);
                }

                return;
            }

            //system package pingpackage
            var pingPackage = package as PingPackage;
            if (pingPackage != null)
            {
                SetLatency(pingPackage);
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     byte[] result;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         result = mStream.ToArray();
     }
     _listener.Client.SendTo(result, new IPEndPoint(receiver, 2565));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     byte[] result;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         result = mStream.ToArray();
     }
     for (int i = 0; i <= _connections.Count - 1; i++)
     {
         _listener.Client.SendTo(result, new IPEndPoint(_connections[i].IPAddress, 2565));
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Receives data.
        /// </summary>
        private void InternalBeginReceive()
        {
            while (_connected)
            {
                if (_udpClient.Available > 0)
                {
                    var    serverIpEndPoint = new IPEndPoint(IPAddress.Any, 2565);
                    byte[] receivedData     = _udpClient.Receive(ref serverIpEndPoint);
                    using (var mStream = new MemoryStream(receivedData))
                    {
                        IBasePackage package = PackageSerializer.Deserialize(mStream);

                        var binaryPackage = package as BinaryPackage;
                        if (binaryPackage != null)
                        {
                            //binary package
                            foreach (IPackageListener subscriber in GetPackageSubscriber(binaryPackage.OriginType))
                            {
                                subscriber.OnPackageReceived(binaryPackage);
                            }
                            continue;
                        }

                        //system packages
                        var systemPackage = package as NotificationPackage;
                        if (systemPackage != null)
                        {
                            switch (systemPackage.Mode)
                            {
                            case NotificationMode.ClientJoined:
                                for (int i = 0; i <= _clientListeners.Count - 1; i++)
                                {
                                    _clientListeners[i].OnClientJoined(systemPackage.Connection[0]);
                                }
                                break;

                            case NotificationMode.ClientExited:
                                for (int i = 0; i <= _clientListeners.Count - 1; i++)
                                {
                                    _clientListeners[i].OnClientExited(systemPackage.Connection[0]);
                                }
                                break;

                            case NotificationMode.ClientList:
                                for (int i = 0; i <= _clientListeners.Count - 1; i++)
                                {
                                    _clientListeners[i].OnClientListing(systemPackage.Connection);
                                }
                                break;

                            case NotificationMode.TimeOut:
                                for (int i = 0; i <= _clientListeners.Count - 1; i++)
                                {
                                    _clientListeners[i].OnClientTimedOut();
                                }
                                break;

                            case NotificationMode.ServerShutdown:
                                for (int i = 0; i <= _clientListeners.Count - 1; i++)
                                {
                                    _clientListeners[i].OnServerShutdown();
                                }
                                break;
                            }
                        }

                        var pingPackage = package as PingPackage;
                        if (pingPackage != null)
                        {
                            //send ping package back
                            Send(pingPackage);
                        }
                    }
                }
                else
                {
                    //no data available

                    Idle();
                }
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Serializes the package in the given stream.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="targetStream">The TargetStream</param>
 public static void Serialize(IBasePackage package, Stream targetStream)
 {
     Formatter.Serialize(targetStream, package);
 }
Exemplo n.º 19
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     SendToAllClients(package);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     SendTo(package, receiver);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     package.Receiver = receiver;
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         _udpClient.Client.SendTo(mStream.ToArray(), new IPEndPoint(_ip, 2563));
     }
 }
Exemplo n.º 22
0
 /// <summary>
 /// Sends a package to a specified client.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="connection">The Connection</param>
 private void SendTo(IBasePackage package, IPAddress connection)
 {
     LocalConnection localConnection = GetConnection(connection);
     if (localConnection != null)
     {
         PackageSerializer.Serialize(package, localConnection.Client.GetStream());
     }
 }
Exemplo n.º 23
0
        /// <summary>
        /// Starts receiving data.
        /// </summary>
        private void InternalBeginReceive()
        {
            while (_tcpClient.Connected)
            {
                try
                {
                    IBasePackage package = _tcpClient.Available > 0 ? PackageSerializer.Deserialize(_nStream) : null;
                    if (package == null)
                    {
                        //Idle the client.
                        Idle();
                        continue;
                    }
                    var binaryPackage = package as BinaryPackage;
                    if (binaryPackage != null)
                    {
                        //binary package
                        //Gets the subscriber list with the matching origin type
                        IEnumerable <IPackageListener> subscribers = GetPackageSubscriber(binaryPackage.OriginType);
                        foreach (IPackageListener subscriber in subscribers)
                        {
                            subscriber.OnPackageReceived(binaryPackage);
                        }

                        //Nothing more to do.
                        continue;
                    }
                    //determine, which package is it.
                    var notificationPackage = package as NotificationPackage;
                    if (notificationPackage != null)
                    {
                        //Notificationpackage

                        switch (notificationPackage.Mode)
                        {
                        //client joined
                        case NotificationMode.ClientJoined:
                            for (int i = 0; i <= _clientListeners.Count - 1; i++)
                            {
                                _clientListeners[i].OnClientJoined(notificationPackage.Connection[0]);
                            }
                            break;

                        //client exited
                        case NotificationMode.ClientExited:
                            for (int i = 0; i <= _clientListeners.Count - 1; i++)
                            {
                                _clientListeners[i].OnClientExited(notificationPackage.Connection[0]);
                            }
                            break;

                        //client listing
                        case NotificationMode.ClientList:
                            for (int i = 0; i <= _clientListeners.Count - 1; i++)
                            {
                                _clientListeners[i].OnClientListing(notificationPackage.Connection);
                            }
                            break;

                        //server shutdown
                        case NotificationMode.ServerShutdown:
                            for (int i = 0; i <= _clientListeners.Count - 1; i++)
                            {
                                _clientListeners[i].OnServerShutdown();
                                _tcpClient.Close();
                            }
                            break;

                        //we timed out.
                        case NotificationMode.TimeOut:
                            for (int i = 0; i <= _clientListeners.Count - 1; i++)
                            {
                                _clientListeners[i].OnClientTimedOut();
                            }
                            _tcpClient.Close();
                            break;
                        }

                        //exit sub
                        continue;
                    }
                    var pingPackage = package as PingPackage;
                    if (pingPackage != null)
                    {
                        //Pingpackage
                        //Send the ping package back to the server.
                        Send(pingPackage);
                        //exit sub
                        continue;
                    }

                    _logger.Error("Received unknown package.");
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }
            }
        }
Exemplo n.º 24
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     SendTo(package, receiver);
 }
Exemplo n.º 25
0
 /// <summary>
 /// Serializes the package in the given stream.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="targetStream">The TargetStream</param>
 public static void Serialize(IBasePackage package, Stream targetStream)
 {
     Formatter.Serialize(targetStream, package);
 }
Exemplo n.º 26
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 /// <param name="receiver">The Receiver.</param>
 public void Send(IBasePackage package, IPAddress receiver)
 {
     if (!_tcpClient.Connected) throw new InvalidOperationException("The client is not connected.");
     try
     {
         package.Receiver = receiver;
         PackageSerializer.Serialize(package, _nStream);
         _nStream.Flush();
     }
     catch (Exception ex)
     {
         _logger.Error(ex.Message);
     }
 }
Exemplo n.º 27
0
 /// <summary>
 /// Sends a package to the given receivers.
 /// </summary>
 /// <param name="package">The Package.</param>
 public void Send(IBasePackage package)
 {
     using (var mStream = new MemoryStream())
     {
         PackageSerializer.Serialize(package, mStream);
         _udpClient.Client.SendTo(mStream.ToArray(), new IPEndPoint(_ip, 2563));
     }
 }
Exemplo n.º 28
0
 /// <summary>
 /// Sends a package to all clients.
 /// </summary>
 /// <param name="package">The Package.</param>
 private void SendToAllClients(IBasePackage package)
 {
     for (int i = 0; i <= _connections.Count - 1; i++)
     {
         NetworkStream networkStream = _connections[i].Client.GetStream();
         PackageSerializer.Serialize(package, networkStream);
         //May not be neccessary
         networkStream.Flush();
     }
 }