public void DataEngine(object obj, EventArgs e)
        {
            if (_networkEnabled == false)
            {
                return;
            }

            string data = null;

            try
            {
                data = GetData(_read);
                if (data == null)
                {
                    return;
                }
                else if (data == "") //TODO Не нашел лучшего способа определения диссконекта, когда сервер использует программное отключение
                {
                    ThrowDisconnect("Разрыв соединения с сервером");
                }
                else
                {
                    EventUpdate?.Invoke(this, new EventNetworkUpdate(data));
                }
            }
            catch (IOException)
            {
                ThrowDisconnect("Разрыв соединения с сервером");
            }
        }
    /// <summary>
    /// manually call is not recommended
    /// </summary>
    public void ServerTick()
    {
        if (ClientDataList.Count < 1)
        {
            return;
        }

        ClientDataList.ForEach((c) =>
        {
            if (c.TcpSocket.Available > 0)
            {
                c.TcpSocket.Client.Receive(buffer);
                Debug.Log("Tcp Received : " + encoding.GetString(buffer));
                DecompClientRequest(buffer, c);
                if (OnTcpPacketReceived != null)
                {
                    OnTcpPacketReceived.Invoke(buffer, c);
                }
            }
        });
        if (UdpSocket.Available > 0)
        {
            IPEndPoint endPoint  = null;
            byte[]     Udpbuffer = UdpSocket.Receive(ref endPoint);
            Debug.Log("Udp Received : " + encoding.GetString(Udpbuffer));
            ClientDataContainer client = ClientDataList.Find((c) => c.address == endPoint.Address);
            DecompClientAutonomousData(Udpbuffer, client);
            if (OnUdpPacketReceived != null)
            {
                OnUdpPacketReceived.Invoke(Udpbuffer, client);
            }
        }
        Replicate();
    }
Exemple #3
0
 public void ClientTick()
 {
     if (OwnTcpSocket == null || !OwnTcpSocket.Connected)
     {
         return;
     }
     if (OwnTcpSocket.Available > 0)
     {
         OwnTcpSocket.Client.Receive(databuffer);
         Debug.Log("Tcp Received :" + encoding.GetString(databuffer));
         DecompServerMessage(databuffer);
         if (OnTcpPacketReceived != null)
         {
             OnTcpPacketReceived.Invoke(databuffer);
         }
     }
     if (OwnUdpClient.Available > 0)
     {
         IPEndPoint endPoint      = null;
         byte[]     Udpdatabuffer = OwnUdpClient.Receive(ref endPoint);
         Debug.Log("Udp Received : " + encoding.GetString(Udpdatabuffer));
         DecompReplicationData(Udpdatabuffer);
         if (OnUdpPacketReceived != null)
         {
             OnUdpPacketReceived.Invoke(Udpdatabuffer);
         }
     }
     ReplicateAutonomousObject();
 }
    void ClientRequest(string request, ClientDataContainer client)
    {
        string[] vs = request.Contains(',') ? request.Split(',') : new string[] { request };
        switch (vs[0])
        {
        case "NewAutoObj":
            CreateAutonomousPrefab(vs[1], vs[2], Serializer.StringToVector3(vs[3], vs[4], vs[5]), Serializer.StringToVector3(vs[6], vs[7], vs[8]), vs[9], client);
            break;

        case "DestAutoObj":
            RepObjPairs.TryGetValue(int.Parse(vs[1]), out ReplicatiorBase replicatior);
            if (client.AutonomousObjects.Contains(replicatior))
            {
                DestroyReplicatedObject(int.Parse(vs[1]));
            }
            break;

        case "Init":
            SendInitialMessage(client);
            break;

        case "RPWCOS":     //WideRange RPC On Server
            ProcessRPC_Wide(vs[1], vs[2], vs[3]);
            break;

        case "RPWCOC":     //WideRange RPC On Client
            HandOutRPC_Wide(byte.Parse(vs[1]), vs[2], vs[3], vs[4]);
            break;

        case "RPWCMC":     //MultiCast WideRange RPC
            MultiCastRPC_Wide(vs[1], vs[2], vs[3]);
            break;

        case "RPCOS":     //RPC On Server
            ProcessRPC(int.Parse(vs[1]), vs[2], vs[3]);
            break;

        case "RPCOC":     //RPC On Client
            HandOutRPC(byte.Parse(vs[1]), vs[2], vs[3], vs[4]);
            break;

        case "RPCMC":     //MultiCast RPC
            MultiCastRPC(vs[1], vs[2], vs[3]);
            break;

        case "Disconnect":
            ClientDisconnected(client);
            break;

        default:
            OnTcpMessageReceived?.Invoke(encoding.GetBytes(request), client);
            break;
        }
    }
Exemple #5
0
    void ProcessServerMessage(string mes)
    {
        string[] vs = mes.Contains(',') ? mes.Split(',') : new string[] { mes };
        switch (vs[0])
        {
        case "NewRepObj":
            CreateReplicatedPrefab(vs[1], Serializer.StringToVector3(vs[2], vs[3], vs[4]), Serializer.StringToVector3(vs[5], vs[6], vs[7]), vs[8], int.Parse(vs[9]), byte.Parse(vs[10]));
            break;

        case "AutoObjAdded":
            AddAdmittedAutonomousObject(vs[1], int.Parse(vs[2]));
            break;

        case "Dest":
            DestroyReplicatedObject(int.Parse(vs[1]));
            break;

        case "End":
            Debug.Log("Server Shutdown");
            ShutDownClient();
            break;

        case "Kicked":
            Debug.Log("Kicked From Server");
            ShutDownClient();
            break;

        case "RPCOC":
            ProcessRPC(int.Parse(vs[1]), vs[2], vs[3]);
            break;

        case "RPWCOC":
            ProcessRPC_Wide(vs[1], vs[2], vs[3]);
            break;

        case "Assign":
            NetworkInitialize(byte.Parse(vs[1]));
            break;

        default:
            OnTcpMessageReceived?.Invoke(encoding.GetBytes(mes));
            break;
        }
    }