Contains information about events received by the network. The type of the network event decides the content it can contain. Most important are: UnreliableMessageReceived / ReliableMessageReceived: A new message was received. The property MessageData will return a buffer + byte array containing the data received. ServerInitialized: A call to StartServer was successful. The Info property will return the address the server can be accessed by.
Esempio n. 1
0
        public void AddClientSide(NetworkViewID recieverId, NetworkMessageInfo info)
        {
            NetworkViewID senderViewId = Network.AllocateViewID();

            this.glNetworkView.RPC("AddServerSide", info.sender, senderViewId);

            NetworkPlayer            player   = info.sender;
            ConnectionId             playerid = NetworkPlayerToConnectionId(player);
            SingleEndpointConnection endpoint = mKnownPlayers[playerid];

            NetworkView reciever = endpoint.gameObject.AddComponent <NetworkView>();

            reciever.viewID = recieverId;
            reciever.stateSynchronization = NetworkStateSynchronization.Unreliable;

            NetworkView sender = endpoint.gameObject.AddComponent <NetworkView>();

            sender.viewID = senderViewId;
            sender.stateSynchronization = NetworkStateSynchronization.Unreliable;

            endpoint.Receiver = reciever;
            endpoint.Sender   = sender;
            endpoint.Init();


            Debug.Log("Connection to " + playerid + " initialized");
            NetworkEvent ev = new NetworkEvent(NetEventType.NewConnection, playerid, null);

            mEvents.Enqueue(ev);
        }
Esempio n. 2
0
        private void OnServerInitialized()
        {
            mConnectionInfo = "" + Network.player.guid;
            Debug.Log("OnServerInitialized GUID: " + mConnectionInfo);
            NetworkEvent ev = new NetworkEvent(NetEventType.ServerInitialized, ConnectionId.INVALID, mConnectionInfo);

            mEvents.Enqueue(ev);
        }
Esempio n. 3
0
        public void DeliverMessage(byte[] data, NetworkMessageInfo info)
        {
            ConnectionId    sender = NetworkPlayerToConnectionId(info.sender);
            ByteArrayBuffer bab    = new ByteArrayBuffer(data);
            NetworkEvent    ev     = new NetworkEvent(NetEventType.ReliableMessageReceived, sender, bab);

            mEvents.Enqueue(ev);
        }
Esempio n. 4
0
        private void OnFailedToConnect(NetworkConnectionError error)
        {
            mDebugConnecting = false;
            string       mErrorMessage = "Could not connect to server: " + error;
            NetworkEvent ev            = new NetworkEvent(NetEventType.ConnectionFailed, ConnectionId.INVALID, mErrorMessage);

            mEvents.Enqueue(ev);
            Debug.Log(mErrorMessage);
        }
Esempio n. 5
0
        public void StartServer(int port)
        {
            mEvents.Clear();
            NetworkConnectionError err = Network.InitializeServer(8, port, true);

            Debug.Log("InitializeServer" + err);
            if (err != NetworkConnectionError.NoError)
            {
                NetworkEvent ev = new NetworkEvent(NetEventType.ServerInitFailed, ConnectionId.INVALID, "Error " + err);
                mEvents.Enqueue(ev);
            }
        }
Esempio n. 6
0
        private void OnPlayerDisconnected(NetworkPlayer player)
        {
            NetworkEvent ev = new NetworkEvent(NetEventType.Disconnected, NetworkPlayerToConnectionId(player), null);

            mEvents.Enqueue(ev);

            if (mKnownPlayers.ContainsKey(ev.ConnectionId))
            {
                mPlayerToConnectionId.Remove(player);
                Destroy(mKnownPlayers[ev.ConnectionId].gameObject);
                mKnownPlayers.Remove(ev.ConnectionId);
            }
        }
Esempio n. 7
0
        private void OnDisconnectedFromServer(NetworkDisconnection info)
        {
            string errormessage;

            if (Network.isServer)
            {
                errormessage = "Server connection disconnected";
                NetworkEvent tev = new NetworkEvent(NetEventType.ServerClosed, ConnectionId.INVALID, errormessage);
                mEvents.Enqueue(tev);
            }
            else
            {
                NetworkEvent tev = new NetworkEvent(NetEventType.Disconnected, ConnectionId.INVALID, null);
                mEvents.Enqueue(tev);
            }
        }
Esempio n. 8
0
        public void ConnectToGuid(string guid)
        {
            mEvents.Clear();
            NetworkConnectionError err = Network.Connect(guid);

            Debug.Log("ConnectTo" + err);
            if (err != NetworkConnectionError.NoError)
            {
                NetworkEvent ev = new NetworkEvent(NetEventType.ConnectionFailed, ConnectionId.INVALID, "Error " + err);
                mEvents.Enqueue(ev);
            }
            else
            {
                mDebugConnectingStart = Time.time;
                mDebugConnecting      = true;
            }
        }
Esempio n. 9
0
        public bool Dequeue(out NetworkEvent evt)
        {
            //this is a try to solve an error in unity network which fails to connect but doesn't
            //send any message after a certain time after connectin we just give up.
            if (mDebugConnecting)
            {
                if (Time.time > (mDebugTimeout + mDebugConnectingStart))
                {
                    Network.Disconnect();
                    OnFailedToConnect(NetworkConnectionError.ConnectionFailed);
                }
            }

            if (mEvents.Count == 0)
            {
                evt = new NetworkEvent();
                return(false);
            }
            evt = mEvents.Dequeue();
            return(true);
        }
Esempio n. 10
0
        public void AddServerSide(NetworkViewID id, NetworkMessageInfo info)
        {
            ConnectionId             playerid = NetworkPlayerToConnectionId(info.sender);
            SingleEndpointConnection endpoint = mKnownPlayers[playerid];

            NetworkView reciever = endpoint.gameObject.AddComponent <NetworkView>();

            reciever.viewID = id;
            //reciever.observed = this;
            reciever.stateSynchronization = NetworkStateSynchronization.Unreliable;

            endpoint.Receiver = reciever;
            endpoint.Init();



            Debug.Log("Connection to " + playerid + " initialized");

            mPlayerToConnectionId[info.sender] = playerid;
            NetworkEvent ev = new NetworkEvent(NetEventType.NewConnection, playerid, null);

            mEvents.Enqueue(ev);
        }
Esempio n. 11
0
 public void DeliverMessage(byte[] data, NetworkMessageInfo info)
 {
     ConnectionId sender = NetworkPlayerToConnectionId(info.sender);
     ByteArrayBuffer bab = new ByteArrayBuffer(data);
     NetworkEvent ev = new NetworkEvent(NetEventType.ReliableMessageReceived, sender, bab);
     mEvents.Enqueue(ev);
 }
Esempio n. 12
0
        public void AddClientSide(NetworkViewID recieverId, NetworkMessageInfo info)
        {
            NetworkViewID senderViewId = Network.AllocateViewID();
            this.glNetworkView.RPC("AddServerSide", info.sender, senderViewId);

            NetworkPlayer player = info.sender;
            ConnectionId playerid = NetworkPlayerToConnectionId(player);
            SingleEndpointConnection endpoint = mKnownPlayers[playerid];

            NetworkView reciever = endpoint.gameObject.AddComponent<NetworkView>();
            reciever.viewID = recieverId;
            reciever.stateSynchronization = NetworkStateSynchronization.Unreliable;

            NetworkView sender = endpoint.gameObject.AddComponent<NetworkView>();
            sender.viewID = senderViewId;
            sender.stateSynchronization = NetworkStateSynchronization.Unreliable;

            endpoint.Receiver = reciever;
            endpoint.Sender = sender;
            endpoint.Init();


            Debug.Log("Connection to " + playerid + " initialized");
            NetworkEvent ev = new NetworkEvent(NetEventType.NewConnection, playerid, null);
            mEvents.Enqueue(ev);
        }
Esempio n. 13
0
 private void OnFailedToConnect(NetworkConnectionError error)
 {
     mDebugConnecting = false;
     string mErrorMessage = "Could not connect to server: " + error;
     NetworkEvent ev = new NetworkEvent(NetEventType.ConnectionFailed, ConnectionId.INVALID, mErrorMessage);
     mEvents.Enqueue(ev);
     Debug.Log(mErrorMessage);
 }
Esempio n. 14
0
        /// <summary>
        /// Dequeues a new event
        /// </summary>
        /// <param name="evt"></param>
        /// <returns></returns>
        public bool Dequeue(out NetworkEvent evt)
        {
            evt = new NetworkEvent();
            if (mEvents.Count == 0)
                return false;

            evt = mEvents.Dequeue();
            return true;
        }
Esempio n. 15
0
        /// <summary>
        /// Retrieves an event from the js library, handles it internallay and then adds it to a queue for delivery to the user.
        /// </summary>
        /// <param name="evt"> The new network event or an empty struct if none is found.</param>
        /// <returns>True if event found, false if no events queued.</returns>
        private bool DequeueInternal(out NetworkEvent evt)
        {
            int length = UnityWebRtcNetworkPeekEventDataLength(mReference);
            if(length == -1) //-1 == no event available
            {
                evt = new NetworkEvent();
                return false;
            }
            else
            {
                ByteArrayBuffer buf = ByteArrayBuffer.Get(length);
                bool eventFound = UnityWebRtcNetworkDequeue(mReference, mTypeidBuffer, mConidBuffer, buf.array, 0, buf.array.Length, mDataWrittenLenBuffer);
                //set the write correctly
                buf.positionWrite = mDataWrittenLenBuffer[0];

                NetEventType type = (NetEventType)mTypeidBuffer[0];
                ConnectionId id;
                id.id = (short)mConidBuffer[0];
                object data = null;

                if (buf.positionWrite == 0 || buf.positionWrite == -1) //no data
                {
                    data = null;
                    //was an empty buffer -> release it and
                    buf.Dispose();
                }
                else if (type == NetEventType.ReliableMessageReceived || type == NetEventType.UnreliableMessageReceived)
                {
                    //store the data for the user to use
                    data = buf;
                }
                else
                {
                    //non data message with data attached -> can only be a string
                    string stringData = Encoding.ASCII.GetString(buf.array, 0, buf.positionWrite);
                    data = stringData;
                    buf.Dispose();

                }

                evt = new NetworkEvent(type, id, data);
                UnityEngine.Debug.Log("event" + type + " received");
                HandleEventInternally(ref evt);
                return eventFound;
            }
        }
Esempio n. 16
0
        public bool Dequeue(out NetworkEvent evt)
        {
            //this is a try to solve an error in unity network which fails to connect but doesn't
            //send any message after a certain time after connectin we just give up.
            if (mDebugConnecting)
            {
                if(Time.time > (mDebugTimeout + mDebugConnectingStart))
                {
                    Network.Disconnect();
                    OnFailedToConnect(NetworkConnectionError.ConnectionFailed);
                }
            }

            if (mEvents.Count == 0)
            {
                evt = new NetworkEvent();
                return false;
            }
            evt = mEvents.Dequeue();
            return true;
        }
Esempio n. 17
0
        public void SingleEndpoint_OnSerialize(BitStream stream, NetworkPlayer senderOrReceiver)
        {
            //doesnt work. server sends messages automatically to all others


            if (stream.isReading)
            {
                char countin = (char)0;
                stream.Serialize(ref countin);
                int count = (int)countin;
                //Debug.Log(count + " packages sent");
                for (int k = 0; k < count; k++)
                {
                    int length = 0;
                    short lengthin = 0;
                    stream.Serialize(ref lengthin);
                    length = ((int)(ushort)lengthin); //just making sure it converts the - values back into high positive values
                    ByteArrayBuffer msg = ByteArrayBuffer.Get(length);



                    for (int i = 0; i < length; i++)
                    {
                        char c = 'v';
                        stream.Serialize(ref c);
                        msg.array[i] = (byte)c;
                        msg.positionWrite++;
                    }
                    NetworkEvent ev = new NetworkEvent(NetEventType.UnreliableMessageReceived, NetworkPlayerToConnectionId(senderOrReceiver), msg);
                    mEvents.Enqueue(ev);
                }
            }
            else
            {
                Queue<ByteArrayBuffer> userOut;
                ConnectionId userid;
                userid = NetworkPlayerToConnectionId(senderOrReceiver);
                
                // NetworkPlayerToInt(senderOrReceiver);

                bool found = mOutgoingUnreliableMessages.TryGetValue(userid, out userOut);

                if (found && userOut.Count > 0)
                {
                    char count = (char)0;
                    if (userOut.Count > 255)
                    {
                        Debug.LogWarning("Too many messages at once");
                        count = (char)255;
                    }else
                    {
                        count = (char)userOut.Count;
                    }

                    stream.Serialize(ref count);

                    for(int i = 0; userOut.Count > 0 && i < 256; i++)
                    {
                        ByteArrayBuffer msg = userOut.Dequeue();

                        short length = (short)msg.positionWrite;

                        stream.Serialize(ref length);
                        for (int k = 0; k < length; k++)
                        {
                            char c = (char)msg.array[k];
                            stream.Serialize(ref c);
                        }
                        msg.Dispose();
                    }
                    //Debug.Log(count + " messages sent to " + userid);

                }

            }
        }
Esempio n. 18
0
        private void OnPlayerDisconnected(NetworkPlayer player)
        {
            NetworkEvent ev = new NetworkEvent(NetEventType.Disconnected, NetworkPlayerToConnectionId(player), null);
            mEvents.Enqueue(ev);

            if(mKnownPlayers.ContainsKey(ev.ConnectionId))
            {
                mPlayerToConnectionId.Remove(player);
                Destroy(mKnownPlayers[ev.ConnectionId].gameObject);
                mKnownPlayers.Remove(ev.ConnectionId);
            }


        }
Esempio n. 19
0
        private void OnDisconnectedFromServer(NetworkDisconnection info)
        {
            string errormessage;
            if (Network.isServer)
            {
                errormessage = "Server connection disconnected";
                NetworkEvent tev = new NetworkEvent(NetEventType.ServerClosed, ConnectionId.INVALID, errormessage);
                mEvents.Enqueue(tev);
            }
            else
            {
                NetworkEvent tev = new NetworkEvent(NetEventType.Disconnected, ConnectionId.INVALID, null);
                mEvents.Enqueue(tev);
            }

        }
Esempio n. 20
0
        public void SingleEndpoint_OnSerialize(BitStream stream, NetworkPlayer senderOrReceiver)
        {
            //doesnt work. server sends messages automatically to all others


            if (stream.isReading)
            {
                char countin = (char)0;
                stream.Serialize(ref countin);
                int count = (int)countin;
                //Debug.Log(count + " packages sent");
                for (int k = 0; k < count; k++)
                {
                    int   length   = 0;
                    short lengthin = 0;
                    stream.Serialize(ref lengthin);
                    length = ((int)(ushort)lengthin); //just making sure it converts the - values back into high positive values
                    ByteArrayBuffer msg = ByteArrayBuffer.Get(length);



                    for (int i = 0; i < length; i++)
                    {
                        char c = 'v';
                        stream.Serialize(ref c);
                        msg.array[i] = (byte)c;
                        msg.positionWrite++;
                    }
                    NetworkEvent ev = new NetworkEvent(NetEventType.UnreliableMessageReceived, NetworkPlayerToConnectionId(senderOrReceiver), msg);
                    mEvents.Enqueue(ev);
                }
            }
            else
            {
                Queue <ByteArrayBuffer> userOut;
                ConnectionId            userid;
                userid = NetworkPlayerToConnectionId(senderOrReceiver);

                // NetworkPlayerToInt(senderOrReceiver);

                bool found = mOutgoingUnreliableMessages.TryGetValue(userid, out userOut);

                if (found && userOut.Count > 0)
                {
                    char count = (char)0;
                    if (userOut.Count > 255)
                    {
                        Debug.LogWarning("Too many messages at once");
                        count = (char)255;
                    }
                    else
                    {
                        count = (char)userOut.Count;
                    }

                    stream.Serialize(ref count);

                    for (int i = 0; userOut.Count > 0 && i < 256; i++)
                    {
                        ByteArrayBuffer msg = userOut.Dequeue();

                        short length = (short)msg.positionWrite;

                        stream.Serialize(ref length);
                        for (int k = 0; k < length; k++)
                        {
                            char c = (char)msg.array[k];
                            stream.Serialize(ref c);
                        }
                        msg.Dispose();
                    }
                    //Debug.Log(count + " messages sent to " + userid);
                }
            }
        }
Esempio n. 21
0
 public void StartServer(int port)
 {
     mEvents.Clear();
     NetworkConnectionError err = Network.InitializeServer(8, port, true);
     Debug.Log("InitializeServer" + err);
     if (err != NetworkConnectionError.NoError)
     {
         NetworkEvent ev = new NetworkEvent(NetEventType.ServerInitFailed, ConnectionId.INVALID, "Error " + err);
         mEvents.Enqueue(ev);
     }
 }
Esempio n. 22
0
        public void AddServerSide(NetworkViewID id, NetworkMessageInfo info)
        {
            ConnectionId playerid = NetworkPlayerToConnectionId(info.sender);
            SingleEndpointConnection endpoint = mKnownPlayers[playerid];

            NetworkView reciever = endpoint.gameObject.AddComponent<NetworkView>();
            reciever.viewID = id;
            //reciever.observed = this;
            reciever.stateSynchronization = NetworkStateSynchronization.Unreliable;

            endpoint.Receiver = reciever;
            endpoint.Init();



            Debug.Log("Connection to " + playerid + " initialized");

            mPlayerToConnectionId[info.sender] = playerid;
            NetworkEvent ev = new NetworkEvent(NetEventType.NewConnection, playerid, null);
            mEvents.Enqueue(ev);
        }
Esempio n. 23
0
        /// <summary>
        /// Needs to be called to read data from the unterlaying network and update this class.
        /// 
        /// Use Dequeue to get the events it read.
        /// </summary>
        public void Update()
        {
            NetworkEvent ev = new NetworkEvent();

            //DequeueInternal will read the message from js, change the state of this object
            //e.g. if a server is successfully opened it will set mIsServer to true
            while(DequeueInternal(out ev))
            {
                //add it for delivery to the user
                mEvents.Enqueue(ev);
            }
        }
Esempio n. 24
0
 private void OnServerInitialized()
 {
     mConnectionInfo = "" + Network.player.guid;
     Debug.Log("OnServerInitialized GUID: " + mConnectionInfo);
     NetworkEvent ev = new NetworkEvent(NetEventType.ServerInitialized, ConnectionId.INVALID, mConnectionInfo);
     mEvents.Enqueue(ev);
 }
Esempio n. 25
0
 /// <summary>
 /// Handles events internally. Needed to change the internal states: Server flag and connection id list.
 /// 
 /// Would be better to remove that in the future from the main library and treat it separately. 
 /// </summary>
 /// <param name="evt"> event to handle </param>
 private void HandleEventInternally(ref NetworkEvent evt)
 {
     if(evt.Type == NetEventType.NewConnection)
     {
         mConnections.Add(evt.ConnectionId);
     }else if(evt.Type == NetEventType.Disconnected)
     {
         mConnections.Remove(evt.ConnectionId);
     }else if(evt.Type == NetEventType.ServerInitialized)
     {
         mIsServer = true;
     }
     else if (evt.Type == NetEventType.ServerClosed || evt.Type == NetEventType.ServerInitFailed)
     {
         mIsServer = false;
     }
 }
Esempio n. 26
0
 public void ConnectToGuid(string guid)
 {
     mEvents.Clear();
     NetworkConnectionError err = Network.Connect(guid);
     Debug.Log("ConnectTo" + err);
     if (err != NetworkConnectionError.NoError)
     {
         NetworkEvent ev = new NetworkEvent(NetEventType.ConnectionFailed, ConnectionId.INVALID, "Error " + err);
         mEvents.Enqueue(ev);
     }
     else
     {
         mDebugConnectingStart = Time.time;
         mDebugConnecting = true;
     }
 }