示例#1
0
 public void OnEventRaised(LNSClient from, ushort eventCode, LNSReader reader, DeliveryMethod deliveryMethod)
 {
     if (messageReceiver != null)
     {
         messageReceiver(reader.GetString());
     }
 }
示例#2
0
 protected static void PutIntoPool(LNSReader reader)
 {
     lock (theLock)
     {
         pool.Enqueue(reader);
     }
 }
示例#3
0
 public void OnEventRaised(LNSClient from, ushort eventCode, LNSReader reader, DeliveryMethod deliveryMethod)
 {
     if (eventCode == 0)
     {
         Color color = reader.GetColor();
         if (others.ContainsKey(from.id))
         {
             Clone o = others[from.id];
             o.GetComponent <Clone>().SetColor(color);
             o.gameObject.name = from.id + "_" + from.displayName;
         }
         else
         {
             GameObject o = Instantiate(clonePrefab);
             o.GetComponent <Clone>().SetColor(color);
             o.name = from.id + "_" + from.displayName;
             others.Add(from.id, o.GetComponent <Clone>());
         }
     }
     else if (eventCode == 1)
     {
         string     playerid = from.id;
         Vector3    pos      = reader.GetVector3();
         Quaternion rot      = reader.GetQuaternion();
         //long timestamp = reader.GetLong();
         //Debug.Log(timestamp);
         //float delay = (float)(System.DateTime.UtcNow - System.DateTime.FromFileTimeUtc(timestamp)).TotalMilliseconds;
         //if (delay > 400)
         //{
         //    //Discard old packet
         //    return;
         //}
         if (others.ContainsKey(playerid))
         {
             others[playerid].SetTarget(pos, rot);
         }
     }
     else if (eventCode == 2)
     {
         if (others.ContainsKey(from.id))
         {
             Vector3    pos = reader.GetVector3();
             Quaternion rot = reader.GetQuaternion();
             others[from.id].ShootBulletAt(pos, rot);
         }
     }
     else if (eventCode == 3)
     {
         player.transform.position += Vector3.up;
     }
     else if (eventCode == 10)
     {
         if (others.ContainsKey(from.id))
         {
             others[from.id].audioReceiver.Play(reader.GetInt(), reader.GetInt(), reader.GetRemainingBytes());
         }
     }
 }
示例#4
0
 public void OnEventRaised(LNSClient from, ushort eventCode, LNSReader reader, DeliveryMethod deliveryMethod)
 {
     totalDataTransfered += reader.RawDataSize;
     if (Time.time - timeDataCountStarted > 1)
     {
         dataTransferPerSec   = (float)totalDataTransfered / (Time.time - timeDataCountStarted);
         timeDataCountStarted = Time.time;
         totalDataTransfered  = 0;
     }
     if (eventCode == 0)
     {
         if (!remoteClones.ContainsKey(from.id))
         {
             GameObject clone = GameObject.Instantiate(clonePrefab, clonePrefab.transform.parent, true);
             clone.SetActive(true);
             clone.name = "Clone_" + from.id;
             remoteClones.Add(from.id, clone.transform);
         }
         remoteClones[from.id].localPosition = reader.GetVector3();
     }
 }
示例#5
0
    //protected void WritePlayerData(NetDataWriter writer)
    //{
    //    writer.Put(id);
    //    writer.Put(displayName);
    //    writer.Put(this.settings.gameKey);
    //    writer.Put(this.settings.gameVersion);
    //    writer.Put(this.settings.platform);
    //}


    private void Listener_NetworkReceiveEvent(NetPeer peer, NetPacketReader packetReader, DeliveryMethod deliveryMethod)
    {
        byte clientInstruction = packetReader.GetByte();

        if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_LIST)
        {
            if (roomList == null)
            {
                roomList = new LNSRoomList();
            }
            JsonUtility.FromJsonOverwrite(packetReader.GetString(), roomList);
            if (onRoomListReceived != null)
            {
                threadDispatcher.Add(() => onRoomListReceived(roomList));
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_CREATED)
        {
            isInActiveRoom     = true;
            _lastConnectedRoom = packetReader.GetString();


            if (onRoomCreated != null)
            {
                threadDispatcher.Add(() => onRoomCreated());
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_DISCONNECTED)
        {
            if (onDisconnectedFromRoom != null)
            {
                threadDispatcher.Add(() => onDisconnectedFromRoom());
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_FAILED_CREATE)
        {
            byte reason = packetReader.GetByte();
            if (onRoomCreateFailed != null)
            {
                threadDispatcher.Add(() => onRoomCreateFailed((ROOM_FAILURE_CODE)reason));
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_FAILED_JOIN)
        {
            byte reason = packetReader.GetByte();
            if (onRoomJoinFailed != null)
            {
                threadDispatcher.Add(() => onRoomJoinFailed((ROOM_FAILURE_CODE)reason));
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_FAILED_REJOIN)
        {
            byte reason = packetReader.GetByte();
            if (onRoomRejoinFailed != null)
            {
                threadDispatcher.Add(() => onRoomRejoinFailed((ROOM_FAILURE_CODE)reason));
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_JOINED)
        {
            isInActiveRoom     = true;
            _lastConnectedRoom = packetReader.GetString();

            if (onRoomJoined != null)
            {
                threadDispatcher.Add(() => onRoomJoined());
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_REJOINED)
        {
            isInActiveRoom     = true;
            _lastConnectedRoom = packetReader.GetString();
            if (onRoomRejoined != null)
            {
                threadDispatcher.Add(() => onRoomRejoined());
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_FAILED_RANDOM_JOIN)
        {
            isInActiveRoom = false;
            if (onRandomRoomJoinFailed != null)
            {
                threadDispatcher.Add(() => onRandomRoomJoinFailed());
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_MASTERCLIENT_CHANGED)
        {
            _lastConnectedRoomMasterClient = packetReader.GetString();
            localClient.isMasterClient     = isLocalPlayerMasterClient = (_lastConnectedRoomMasterClient == id);
            if (onMasterClientUpdated != null)
            {
                try
                {
                    if (isLocalPlayerMasterClient)
                    {
                        threadDispatcher.Add(() =>
                        {
                            onMasterClientUpdated(localClient);
                        });
                    }
                    else
                    {
                        for (int i = 0; i < clients.Count; i++)
                        {
                            if (clients[i].id == _lastConnectedRoomMasterClient)
                            {
                                clients[i].isMasterClient = true;
                                LNSClient client = clients[i];
                                threadDispatcher.Add(() =>
                                {
                                    onMasterClientUpdated(client);
                                });
                            }
                            else
                            {
                                clients[i].isMasterClient = false;
                            }
                        }
                    }
                }catch (System.Exception ex)
                {
                    Debug.LogError(ex.Message + " " + ex.StackTrace);
                }
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_PLAYER_CONNECTED)
        {
            string          client_id          = packetReader.GetString();
            string          client_displayName = packetReader.GetString();
            CLIENT_PLATFORM client_platform    = (CLIENT_PLATFORM)packetReader.GetByte();
            int             client_netID       = packetReader.GetInt();
            LNSClient       client             = null;
            for (int i = 0; i < clients.Count; i++)
            {
                if (clients[i].id == client_id)
                {
                    client             = clients[i];
                    client.displayName = client_displayName;
                    client.platform    = client_platform;
                    client.networkID   = client_netID;
                    break;
                }
            }

            if (client == null)
            {
                client             = new LNSClient();
                client.id          = client_id;
                client.displayName = client_displayName;
                client.platform    = client_platform;
                client.networkID   = client_netID;

                clients.Add(client);
            }
            client.isConnected = true;

            if (onPlayerConnected != null)
            {
                threadDispatcher.Add(() =>
                {
                    onPlayerConnected(client);
                });
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_PLAYER_DISCONNECTED)
        {
            string client_id = packetReader.GetString();

            for (int i = 0; i < clients.Count; i++)
            {
                if (clients[i].id == client_id)
                {
                    clients[i].isConnected = false;
                    if (onPlayerDisconnected != null)
                    {
                        threadDispatcher.Add(() =>
                        {
                            onPlayerDisconnected(clients[i]);
                        });
                    }
                    return;
                }
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_CACHE_DATA)
        {
            LNSReader reader = LNSReader.GetFromPool();
            string    key    = packetReader.GetString();
            byte[]    data   = packetReader.GetRemainingBytes();
            reader.SetSource(data);

            if (persistentData.ContainsKey(key))
            {
                persistentData[key] = data;
            }
            else
            {
                persistentData.Add(key, data);
            }

            if (dataReceiver != null)
            {
                threadDispatcher.Add(() =>
                {
                    try
                    {
                        dataReceiver.OnCachedDataReceived(key, reader);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogError(ex.Message + " " + ex.StackTrace);
                    }
                    reader.Recycle();
                });
            }
        }
        else if (clientInstruction == LNSConstants.CLIENT_EVT_ROOM_RAW)
        {
            string fromid = packetReader.GetString();

            for (int i = 0; i < clients.Count; i++)
            {
                if (fromid == clients[i].id)
                {
                    if (dataReceiver != null)
                    {
                        ushort    eventCode = packetReader.GetUShort();
                        LNSReader reader    = LNSReader.GetFromPool();


                        LNSClient      fromClient      = clients[i];
                        DeliveryMethod _deliveryMethod = deliveryMethod;

                        reader.SetSource(packetReader.GetRemainingBytes());

                        threadDispatcher.Add(() =>
                        {
                            try
                            {
                                dataReceiver.OnEventRaised(fromClient, eventCode, reader, _deliveryMethod);
                            }
                            catch (System.Exception ex) {
                                Debug.LogError(ex.Message + " " + ex.StackTrace);
                            }
                            reader.Recycle();
                        });
                    }
                    break;
                }
            }
        }
        packetReader.Recycle();
    }
示例#6
0
 public void OnCachedDataReceived(string key, LNSReader data)
 {
     Debug.Log("Cached: " + key + "," + data.GetString());
 }
示例#7
0
 public void OnCachedDataReceived(string key, LNSReader data)
 {
 }