void Start()
    {
        isListening    = false;
        newConnections = new Queue <NewConnection>();
        clients        = new Dictionary <int, ORTCPClient>();
        StartListening();

        Observable
        .Interval(TimeSpan.FromSeconds(1))
        .Where(x => newConnections.Count > 0)
        .Subscribe(x =>
        {
            NewConnection newConnection = newConnections.Dequeue();
            ORTCPClient client          = ORTCPClient.CreateClientInstance("MultiserverClient", newConnection.tcpClient, this);

            int clientId = SaveClient(client);
            TCPEventParams eventParams = new TCPEventParams();
            eventParams.eventType      = eTCPEventType.Connected;
            eventParams.client         = client;
            eventParams.clientID       = clientId;
            eventParams.socket         = newConnection.tcpClient;

            if (verbose)
            {
                print("[TCPServer] New Client Connected: " + client.name);
            }
        });
    }
    public void Start(int port = 0)
    {
        _listenning     = false;
        _newConnections = new Queue <NewConnection>();
        _clients        = new Dictionary <int, ORTCPClient>();

        if (port != 0)
        {
            StartListening(port);
        }
        else
        {
            StartListening();
        }

        Observable
        .Interval(TimeSpan.FromSeconds(1))
        .Where(_ => _newConnections.Count > 0)
        .Subscribe(_ =>
        {
            //Debug.Log(Thread.CurrentThread.ManagedThreadId);
            NewConnection newConnection = _newConnections.Dequeue();
            ORTCPClient client          = ORTCPClient.CreateInstance("ORMultiServerClient", newConnection.tcpClient, this, port);

            int clientID = SaveClient(client);
            ORTCPEventParams eventParams = new ORTCPEventParams();
            eventParams.eventType        = ORTCPEventType.Connected;
            eventParams.client           = client;
            eventParams.clientID         = clientID;
            eventParams.socket           = newConnection.tcpClient;
            Console.BackgroundColor      = ConsoleColor.Cyan;
            Console.ForegroundColor      = ConsoleColor.Black;
            Console.WriteLine("[TCPServer] New client connected");
        });
    }
    private void Start()
    {
        _listenning     = false;
        _newConnections = new Queue <NewConnection>();
        _clients        = new Dictionary <int, ORTCPClient>();
        StartListening();

        Observable
        .EveryUpdate()
        .Where(_ => _newConnections.Count > 0)
        .Subscribe(_ =>
        {
            //Debug.Log(Thread.CurrentThread.ManagedThreadId);
            NewConnection newConnection = _newConnections.Dequeue();
            ORTCPClient client          = ORTCPClient.CreateInstance("ORMultiServerClient", newConnection.tcpClient, this);


            int clientID = SaveClient(client);
            ORTCPEventParams eventParams = new ORTCPEventParams();
            eventParams.eventType        = ORTCPEventType.Connected;
            eventParams.client           = client;
            eventParams.clientID         = clientID;
            eventParams.socket           = newConnection.tcpClient;
            if (verbose)
            {
                print("[TCPServer] New client connected");
            }
        });
    }
    protected int SaveClient(ORTCPClient clientToSave)
    {
        int currentClientID = clientID;

        clients.Add(currentClientID, clientToSave);
        clientID++;
        return(currentClientID);
    }
    protected int SaveClient(ORTCPClient client)
    {
        int currentClientID = ClientID;

        _clients.Add(currentClientID, client);
        ClientID++;
        return(currentClientID);
    }
Beispiel #6
0
    public static ORTCPClient CreateInstance(string name, TcpClient tcpClient, ORTCPMultiServer serverDelegate)     // this is only used by the server
    {
        GameObject  go     = new GameObject(name);
        ORTCPClient client = go.AddComponent <ORTCPClient>();

        client.SetTcpClient(tcpClient);
        client.serverDelegate = serverDelegate;
        client.verbose        = false;
        return(client);
    }
    protected ORTCPClient GetClient(int clientIDToGet)
    {
        ORTCPClient client = null;

        if (clients.TryGetValue(clientIDToGet, out client))
        {
            return(client);
        }
        return(null);
    }
    protected TcpClient GetTcpClient(int clientID)
    {
        ORTCPClient client = null;

        if (!_clients.TryGetValue(clientID, out client))
        {
            return(null);
        }
        return(client.tcpClient);
    }
    protected int RemoveClient(ORTCPClient client)
    {
        int clientID = GetClientID(client);

        if (clientID < 0)
        {
            return(-1);
        }
        return(RemoveClient(clientID));
    }
    public ORTCPClient GetClientStatusWithID(int clientID)
    {
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return(null);
        }

        return(client);
    }
    public void DisconnectClientWithID(int clientID)
    {
        Console.WriteLine("[TCPServer] DisconnectClientWithID: " + clientID);
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return;
        }
        client.Disconnect();
    }
 public int GetClientID(ORTCPClient client)
 {
     foreach (KeyValuePair <int, ORTCPClient> entry in _clients)
     {
         if (entry.Value == client)
         {
             return(entry.Key);
         }
     }
     return(-1);
 }
 protected int GetClientID(ORTCPClient clientToGet)
 {
     foreach (KeyValuePair <int, ORTCPClient> client in clients)
     {
         if (client.Value == clientToGet)
         {
             return(client.Key);
         }
     }
     return(-1);
 }
    protected int RemoveClient(ORTCPClient clientToRemove)
    {
        int clientToRemoveID = GetClientID(clientToRemove);

        if (clientToRemoveID < 0)
        {
            Destroy(clientToRemove.gameObject);
            return(-1);
        }
        return(RemoveClient(clientToRemoveID));
    }
Beispiel #15
0
    public static ORTCPClient CreateInstance(string name, TcpClient tcpClient, ORTCPMultiServer serverDelegate, int port)     // this is only used by the server
    {
        ORTCPClient client = new ORTCPClient();

        client.port = port;
        client.SetTcpClient(tcpClient);
        client.serverDelegate = serverDelegate;
        client.verbose        = false;
        client.Start();
        return(client);
    }
    protected int RemoveClient(int clientID)
    {
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return(clientID);
        }
        client.Disconnect();
        _clients.Remove(clientID);
        return(clientID);
    }
    protected int RemoveClient(int clientIDToRemove)
    {
        ORTCPClient client = GetClient(clientIDToRemove);

        if (client == null)
        {
            return(clientIDToRemove);
        }

        client.Disconnect();
        clients.Remove(clientIDToRemove);
        Destroy(client.gameObject);
        return(clientIDToRemove);
    }
    public void SendClientWithIDMessage(int clientID, string message)
    {
        Console.WriteLine("[TCPServer] SendClientWithIDMessage: " + clientID + ". " + message);
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return;
        }



        client.Send(message);
    }
Beispiel #19
0
    public void DisconnectClientWithID(int clientID)
    {
        if (verbose)
        {
            print("[TCPServer] DisconnectClientWithID: " + clientID);
        }
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return;
        }
        client.Disconnect();
    }
Beispiel #20
0
    public void SendClientWithIDMessage(int clientID, string message)
    {
        if (verbose)
        {
            print("[TCPServer] SendClientWithIDMessage: " + clientID + ". " + message);
        }
        ORTCPClient client = GetClient(clientID);

        if (client == null)
        {
            return;
        }

        client.Send(message);
    }
Beispiel #21
0
    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
        }
        //debug binding
        Observable.EveryUpdate()
        .Select(_ => GameManager.TargetObject_External)
        .DistinctUntilChanged()
        .Subscribe(_ => verbose = (GameManager.TargetObject_External & TargetObject.TCP) == TargetObject.TCP)
        .AddTo(gameObject);

        _listenning     = false;
        _newConnections = new Queue <NewConnection>();
        _clients        = new Dictionary <int, ORTCPClient>();
        StartListening();

        Observable
        .EveryUpdate()
        .Where(_ => _newConnections.Count > 0)
        .Subscribe(_ =>
        {
            //Debug.Log(Thread.CurrentThread.ManagedThreadId);
            NewConnection newConnection = _newConnections.Dequeue();
            ORTCPClient client          = ORTCPClient.CreateInstance("ORMultiServerClient", newConnection.tcpClient, this);


            int clientID = SaveClient(client);
            ORTCPEventParams eventParams = new ORTCPEventParams();
            eventParams.eventType        = ORTCPEventType.Connected;
            eventParams.client           = client;
            eventParams.clientID         = clientID;
            eventParams.socket           = newConnection.tcpClient;
            if (verbose)
            {
                print("[TCPServer] New client connected");
            }
        });
    }
        static void DoCommnad(ORTCPEventParams e)
        {
            var cmd = e.message;

            switch (cmd)
            {
            case "100":
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                APP = e.client;
                var str = "[TCPServer] Sending Message to all Clients: TargetApp Connected";
                Console.WriteLine(str);
                ORTCPMultiServer.Instance.SendAllClientsMessage(str);
                APP?.Send("Ready to receive cmds");
                break;

            case "200":
                Console.BackgroundColor = ConsoleColor.DarkGray;
                Console.ForegroundColor = ConsoleColor.White;
                SC = e.client;
                var str1 = "[TCPServer] Sending Message to all Clients: ShowControl Connected";
                Console.WriteLine(str1);
                ORTCPMultiServer.Instance.SendAllClientsMessage(str1);
                APP?.Send("Show Control Connected");
                break;

            case "shutdown-machine":
            {
                Console.ForegroundColor = ConsoleColor.Green;
                var str2 = "[TCPServer] Sending Message to all Clients: Shutting down in 3s";
                Console.WriteLine(str2);
                ORTCPMultiServer.Instance.SendAllClientsMessage(str2);
                APP?.Send("Shutting down in 3s..");
                Observable.Timer(TimeSpan.FromSeconds(3))
                .Take(1)
                .Subscribe(_ =>
                    {
                        ProcessStartInfo proc = new ProcessStartInfo();
                        proc.WindowStyle      = ProcessWindowStyle.Hidden;
                        proc.FileName         = "cmd";
                        proc.Arguments        = "/C shutdown /s /t 0";
                        Process.Start(proc);
                    });
            }
            break;

            case "restart-machine":
            {
                Console.ForegroundColor = ConsoleColor.Green;
                var str3 = "[TCPServer] Sending Message to all Clients: Restarting in 3s";
                Console.WriteLine(str3);
                ORTCPMultiServer.Instance.SendAllClientsMessage(str3);
                APP?.Send("Shutting down in 3s...");
                Observable.Timer(TimeSpan.FromSeconds(3))
                .Take(1)
                .Subscribe(_ =>
                    {
                        ProcessStartInfo proc = new ProcessStartInfo();
                        proc.WindowStyle      = ProcessWindowStyle.Hidden;
                        proc.FileName         = "cmd";
                        proc.Arguments        = "/C shutdown /f /r /t 0";
                        Process.Start(proc);
                    });
            }
            break;

            case "restart-application":
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                APP?.Send("Restarting the app..");

                Observable.Timer(TimeSpan.FromSeconds(2))
                .Take(1)
                .Subscribe(_ => TargetApp.CloseMainWindow(),
                           () =>
                    {
                        Observable.Timer(TimeSpan.FromSeconds(2))
                        .Take(1)
                        .Subscribe(_ => TargetApp.Start());
                    });
            }
            break;

            case "reset-application":
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                var str3 = "reset-application";
                Console.WriteLine("[TCPServer] Sending Message to all Clients: " + str3);
                ORTCPMultiServer.Instance.SendAllClientsMessage(str3);
            }
            break;

            case "shutdown-application":
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                APP?.Send("Shutting down the app...");
                Observable.Timer(TimeSpan.FromSeconds(2))
                .Take(1)
                .Subscribe(_ => TargetApp.CloseMainWindow());
            }
            break;

            case "status-application":
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("[TCPServer] Sending Message to App client: status-application");
                APP?.Send(e.message);
            }
            break;

            default:
            {
                if (SC != null)
                {
                    SC.Send(e.message);
                }
                else
                {
                    Console.Write("Unknown command. Show Control is null.");
                }
            }
            break;
            }
        }