Exemplo n.º 1
0
    private void ReceiverThread(Thread main, int id = 0)
    {
        //Debug.Log("id:"+id+" NetworkManager - ReceiverThread Start");
        try
        {
            bool done = false;

            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
            listener = new UdpClient(port);
            listener.Client.ReceiveTimeout = receiverTimeout;
            while (!done)
            {
                //Thread.Sleep(1000);
                if (!main.IsAlive)
                {
                    throw new Exception("NetworkManager - Aplikacja zamknieta");
                }
                try
                {
                    //Debug.Log("Waiting for broadcast");
                    byte[] bytes = listener.Receive(ref groupEP);

                    //Debug.Log("Odebrano");
                    //Debug.Log(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
                    string    json      = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    QueryPack queryPack = JsonUtility.FromJson <QueryPack>(json);
                    QueuePack queuePack = new QueuePack();
                    queuePack.endpoint      = groupEP;
                    queuePack.endpoint.Port = queryPack.port;
                    queuePack.qp            = queryPack;
                    processQueueMessage(queuePack);
                    //Console.WriteLine("Received broadcast from {0} :\n {1}\n",groupEP.ToString(),Encoding.ASCII.GetString(bytes, 0, bytes.Length));
                }
                catch (Exception e)
                {
                    //Debug.Log("Blad in");
                }
            }
            listener.Close();
        }
        catch (ThreadAbortException e)
        {
            //Debug.Log("id:" + id + " Abort");
        }
        catch (SocketException e)
        {
            Debug.Log("id:" + id + " Port error");
            listenerErrorTrigger = true;
        }
        catch (Exception e)
        {
            Debug.Log("id:" + id + " Blad");
            disableTrigger = true;
        }
        finally
        {
            //Debug.Log("id:" + id + " NetworkManager - ReceiverThread Stop");
        }
    }
    //Pilnuje aby obiekt dotarł do każdego komputera w grze, poza komputerem z którego wysłano obiekt.
    public void SendToAllComputers(object o)
    {
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }
        var json = JsonUtility.ToJson(o);
        var qp   = new QueryPack
        {
            json     = json,
            type     = o.GetType().FullName,
            port     = port,
            sendMode = SendMode.SM_ALL_IN_NETWORK
        };
        var queue = new QueuePack
        {
            qp = qp
        };

        switch (networkState)
        {
        case NetworkState.NET_SERVER:
        {
            foreach (var comp in computers)
            {
                var ip = comp.ip;
                if (Equals(ip, myIp))
                {
                    continue;
                }
                var tmp = new QueuePack
                {
                    endpoint = ip,
                    qp       = queue.qp
                };
                sendQueue.Enqueue(tmp);
            }

            break;
        }

        case NetworkState.NET_CLIENT:
        {
            queue.endpoint = serverIp;
            sendQueue.Enqueue(queue);
            break;
        }

        default:
            sendQueue.Enqueue(queue);
            break;
        }
    }
Exemplo n.º 3
0
 private void sendAllQueriesInQueue()
 {
     if (networkState != NetworkState.NET_DISABLED && sendQueue != null)
     {
         for (; sendQueue.Count > 0;)
         {
             QueuePack queue = sendQueue.Dequeue();
             string    json  = QueryPack.getJson(queue.qp);
             sendObject(json, queue.endpoint);
         }
     }
 }
Exemplo n.º 4
0
    //Wysyła obiekt do komputera o podanym ip
    public void sendToComputer(object o, IPEndPoint ip)
    {
        string    json = JsonUtility.ToJson(o);
        QueryPack qp   = new QueryPack();

        qp.json     = json;
        qp.type     = o.GetType().FullName;
        qp.port     = port;
        qp.sendMode = SendMode.SM_COMPUTER;
        QueuePack queue = new QueuePack();

        queue.qp       = qp;
        queue.endpoint = ip;
        sendQueue.Enqueue(queue);
    }
Exemplo n.º 5
0
    //Pilnuje aby obiekt dotarł do każdego komputera w grze, poza komputerem z którego wysłano obiekt.
    public void sendToAllComputers(object o)
    {
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }
        string    json = JsonUtility.ToJson(o);
        QueryPack qp   = new QueryPack();

        qp.json     = json;
        qp.type     = o.GetType().FullName;
        qp.port     = port;
        qp.sendMode = SendMode.SM_ALL_IN_NETWORK;
        QueuePack queue = new QueuePack();

        queue.qp = qp;
        //throw new NotImplementedException();
        switch (networkState)
        {
        case NetworkState.NET_SERVER:
        {
            foreach (Computer comp in computers)
            {
                IPEndPoint ip = comp.ip;
                if (IPEndPoint.Equals(ip, myIp))
                {
                    continue;
                }
                QueuePack tmp = new QueuePack();
                tmp.endpoint = ip;
                tmp.qp       = queue.qp;
                sendQueue.Enqueue(tmp);
            }
            break;
        }

        case NetworkState.NET_CLIENT:
        {
            queue.endpoint = serverIp;
            sendQueue.Enqueue(queue);
            break;
        }

        default:
            sendQueue.Enqueue(queue);
            break;
        }
    }
Exemplo n.º 6
0
    //Wysyła obiekt do wszystkich urządzeń w domenie rozgłoszeniowej, nawet do samego siebie
    public void sendBroadcast(object o)
    {
        string    json = JsonUtility.ToJson(o);
        QueryPack qp   = new QueryPack();

        qp.json     = json;
        qp.type     = o.GetType().FullName;
        qp.port     = port;
        qp.sendMode = SendMode.SM_BROADCAST;
        QueuePack queue = new QueuePack();

        queue.qp       = qp;
        queue.endpoint = new IPEndPoint(IPAddress.Broadcast, broadcastPort);
        sendQueue.Enqueue(queue);
        //Debug.Log("Broadcast");
    }
    //Wysyła obiekt do komputera o podanym ip
    public void SendToComputer(object o, IPEndPoint ip)
    {
        var json = JsonUtility.ToJson(o);
        var qp   = new QueryPack
        {
            json     = json,
            type     = o.GetType().FullName,
            port     = port,
            sendMode = SendMode.SM_COMPUTER
        };
        var queue = new QueuePack
        {
            qp       = qp,
            endpoint = ip
        };

        sendQueue.Enqueue(queue);
    }
    //Wysyła obiekt do wszystkich urządzeń w domenie rozgłoszeniowej, nawet do samego siebie
    public void SendBroadcast(object o)
    {
        var json = JsonUtility.ToJson(o);
        var qp   = new QueryPack
        {
            json     = json,
            type     = o.GetType().FullName,
            port     = port,
            sendMode = SendMode.SM_BROADCAST
        };
        var queue = new QueuePack
        {
            qp       = qp,
            endpoint = new IPEndPoint(IPAddress.Broadcast, broadcastPort)
        };

        sendQueue.Enqueue(queue);
    }
    //Wysyła obiekt do komputera na którym gra gracz o danym id, nawet jeżeli to komputer z którego wysłano obiekt.
    public void SendToPlayer(object o, int playerId)
    {
        throw new NotImplementedException();
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }

        var json = JsonUtility.ToJson(o);
        var qp   = new QueryPack
        {
            json           = json,
            type           = o.GetType().FullName,
            targetPlayerId = playerId,
            port           = port,
            sendMode       = SendMode.SM_PLAYER
        };

        var queue = new QueuePack
        {
            qp = qp
        };

        switch (networkState)
        {
        case NetworkState.NET_CLIENT:
            queue.endpoint = serverIp;
            sendQueue.Enqueue(queue);
            break;

        case NetworkState.NET_SERVER:
            foreach (var player in players)
            {
                if (player.id == playerId)
                {
                    queue.endpoint = player.ip;
                    sendQueue.Enqueue(queue);
                    break;
                }
            }

            break;
        }
    }
Exemplo n.º 10
0
    //Wysyła obiekt do serwera, jeżeli serwer to wysyła to wyśle sam do siebie.
    public void sendToServer(object o)
    {
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }
        string    json = JsonUtility.ToJson(o);
        QueryPack qp   = new QueryPack();

        qp.json     = json;
        qp.type     = o.GetType().FullName;
        qp.port     = port;
        qp.sendMode = SendMode.SM_TO_SERVER;
        QueuePack queue = new QueuePack();

        queue.qp       = qp;
        queue.endpoint = serverIp;
        sendQueue.Enqueue(queue);
    }
    //Wysyła obiekt do serwera, jeżeli serwer to wysyła to wyśle sam do siebie.
    public void SendToServer(object o)
    {
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }
        var json = JsonUtility.ToJson(o);
        var qp   = new QueryPack
        {
            json     = json,
            type     = o.GetType().FullName,
            port     = port,
            sendMode = SendMode.SM_TO_SERVER
        };
        var queue = new QueuePack
        {
            qp       = qp,
            endpoint = serverIp
        };

        sendQueue.Enqueue(queue);
    }
Exemplo n.º 12
0
    //Wysyła obiekt do komputera na którym gra gracz o danym id, nawet jeżeli to komputer z którego wysłano obiekt.
    public void sendToPlayer(object o, int playerId)
    {
        throw new NotImplementedException();
        if (networkState == NetworkState.NET_DISABLED || networkState == NetworkState.NET_ENABLED)
        {
            return;
        }
        string    json = JsonUtility.ToJson(o);
        QueryPack qp   = new QueryPack();

        qp.json           = json;
        qp.type           = o.GetType().FullName;
        qp.targetPlayerId = playerId;
        qp.port           = port;
        qp.sendMode       = SendMode.SM_PLAYER;
        QueuePack queue = new QueuePack();

        queue.qp = qp;
        switch (networkState)
        {
        case NetworkState.NET_CLIENT:
            queue.endpoint = serverIp;
            sendQueue.Enqueue(queue);
            break;

        case NetworkState.NET_SERVER:
            foreach (PlayerInfo player in players)
            {
                if (player.id == playerId)
                {
                    queue.endpoint = player.ip;
                    sendQueue.Enqueue(queue);
                    break;
                }
            }
            break;
        }
    }
Exemplo n.º 13
0
    private void processQueueMessage(QueuePack queuePack)
    {
        bool wtf = !IPEndPoint.Equals(queuePack.endpoint, serverIp);

        if (networkState == NetworkState.NET_CLIENT && wtf)
        {
            return;
        }
        if (networkState == NetworkState.NET_SERVER && lockMode && !isKnownComputer(queuePack.endpoint))
        {
            return;
        }
        switch (queuePack.qp.sendMode)
        {
        case SendMode.SM_BROADCAST:
            if (!IPEndPoint.Equals(queuePack.endpoint, myIp))
            {
                receiveQueue.Enqueue(queuePack);
            }
            break;

        case SendMode.SM_ALL_IN_NETWORK:
            receiveQueue.Enqueue(queuePack);
            if (this.networkState == NetworkState.NET_SERVER)
            {
                IPEndPoint source = queuePack.endpoint;
                foreach (Computer computer in computers)
                {
                    if (IPEndPoint.Equals(source, computer.ip) || IPEndPoint.Equals(myIp, computer.ip))
                    {
                        continue;
                    }
                    QueuePack tmp2 = new QueuePack();
                    tmp2.endpoint = computer.ip;
                    tmp2.qp       = queuePack.qp;
                    tmp2.qp.port  = serverIp.Port;
                    sendQueue.Enqueue(tmp2);
                }
            }
            break;

        case SendMode.SM_PLAYER:
            if (this.networkState == NetworkState.NET_SERVER)
            {
                foreach (PlayerInfo player in players)
                {
                    if (player.id == queuePack.qp.targetPlayerId)
                    {
                        queuePack.endpoint = player.ip;
                        sendQueue.Enqueue(queuePack);
                        break;
                    }
                }
            }
            else
            {
                receiveQueue.Enqueue(queuePack);
            }
            break;

        case SendMode.SM_TO_SERVER_TO_ALL:
            foreach (Computer comp in computers)
            {
                IPEndPoint ip  = comp.ip;
                QueryPack  tmp = queuePack.qp;
                tmp.sendMode = SendMode.SM_COMPUTER;
                QueuePack queue = new QueuePack();
                queue.qp       = tmp;
                queue.endpoint = ip;
                sendQueue.Enqueue(queue);
            }
            break;

        case SendMode.SM_TO_SERVER:
            receiveQueue.Enqueue(queuePack);
            break;

        default:
            receiveQueue.Enqueue(queuePack);
            break;
        }
    }
Exemplo n.º 14
0
 public static string getJson(QueryPack q)
 {
     return(JsonUtility.ToJson(q));
 }