Exemplo n.º 1
0
    public static List <MiniHostData> GetServerList(string serverAddress, int serverPort)
    {
        List <MiniHostData> serverList = new List <MiniHostData>();
        TcpClient           socket     = null;

        try
        {
            socket = new TcpClient(serverAddress, serverPort);

            if (MiniUtility.Write(socket, "list"))
            {
                string s = MiniUtility.Read(socket);
                if (!string.IsNullOrEmpty(s))
                {
                    serverList = (List <MiniHostData>)MiniUtility.Deserialize(s);
                }
            }
        }
        catch (SocketException socketException)
        {
            Debug.LogError("MiniClient::GetServerList: Socket exception: " + socketException);
        }

        if (socket != null && socket.Connected)
        {
            socket.Close();
        }

        return(serverList);
    }
Exemplo n.º 2
0
    public static bool UnRegister(string serverAddress, int serverPort, MiniHostData hostData)
    {
        TcpClient socket  = null;
        bool      success = false;

        try
        {
            socket = new TcpClient(serverAddress, serverPort);

            if (MiniUtility.Write(socket, "unregister"))
            {
                string s = MiniUtility.Serialize(hostData);
                if (!string.IsNullOrEmpty(s))
                {
                    success = MiniUtility.Write(socket, s);
                }
            }
        }
        catch (SocketException socketException)
        {
            Debug.LogError("MiniClient::UnRegister: Socket exception: " + socketException);
        }

        if (socket != null && socket.Connected)
        {
            socket.Close();
        }

        return(success);
    }
Exemplo n.º 3
0
    private void ClientCommand(TcpClient socket)
    {
        //Debug.Log("MiniServer::ClientCommand: new client thread: " + socket.Client.LocalEndPoint.ToString());

        string cmd = MiniUtility.Read(socket);

        if (cmd == "list")
        {
            //Debug.Log("MiniServer::ClientCommand: server list requested");
            mutex.WaitOne();
            string s = MiniUtility.Serialize(serverList);
            mutex.ReleaseMutex();
            if (!MiniUtility.Write(socket, s))
            {
                Debug.LogError("MiniServer::ClientCommand: could not send server list to client: "
                               + socket.Client.LocalEndPoint.ToString());
            }
        }
        else if (cmd == "register")
        {
            //Debug.Log("MiniServer::ClientCommand: requesting client registration");
            string s = MiniUtility.Read(socket);
            if (!string.IsNullOrEmpty(s))
            {
                MiniHostData hostData = (MiniHostData)MiniUtility.Deserialize(s);
                if (hostData != null)
                {
                    mutex.WaitOne();
                    MiniHostData registredHost = serverList.Find(h => h.ip == hostData.ip && h.port == hostData.port);
                    if (registredHost != null)
                    {
                        Debug.Log("MiniServer::ClientCommand: updating host data: " + hostData.ip);
                        serverList[serverList.IndexOf(registredHost)] = hostData;
                    }
                    else
                    {
                        Debug.Log("MiniServer::ClientCommand: registred host: " + hostData.ip);
                        serverList.Add(hostData);
                    }
                    mutex.ReleaseMutex();
                }
                else
                {
                    Debug.LogError("MiniServer::ClientCommand: could not deserialize host data");
                }
            }
        }
        else if (cmd == "unregister")
        {
            //Debug.Log("MiniServer::ClientCommand: requesting host deletion");
            string s = MiniUtility.Read(socket);
            if (!string.IsNullOrEmpty(s))
            {
                MiniHostData hostData = (MiniHostData)MiniUtility.Deserialize(s);
                if (hostData != null)
                {
                    mutex.WaitOne();
                    MiniHostData registredHost = serverList.Find(h => h.ip == hostData.ip && h.port == hostData.port);
                    if (registredHost != null)
                    {
                        Debug.Log("MiniServer::ClientCommand: removing host: " + hostData.ip);
                        serverList.Remove(registredHost);
                    }
                    else
                    {
                        Debug.Log("MiniServer::ClientCommand: could not remove host, host not found: " + hostData.ip);
                    }
                    mutex.ReleaseMutex();
                }
                else
                {
                    Debug.LogError("MiniServer::ClientCommand: could not deserialize host data");
                }
            }
        }

        socket.Close();
    }