Пример #1
0
        public static void Disconnect()
        {
            connectState = ConnectState.Disconnected;
            ClientScene.HandleClientDisconnect(connection);

            // local or remote connection?
            if (isLocalClient)
            {
                if (isConnected)
                {
                    localClientPacketQueue.Enqueue(new BufferHolder(MessagePacker.PackWriter(new DisconnectMessage())));
                }
                NetworkServer.RemoveLocalConnection();
            }
            else
            {
                if (connection != null)
                {
                    connection.Disconnect();
                    connection.Dispose();
                    connection = null;
                    RemoveTransportHandlers();
                }
            }
        }
Пример #2
0
        public static bool SendToReady <T>(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Server.SendToReady msgType:" + typeof(T));
            }

            if (identity != null && identity.observers != null)
            {
                // pack message into byte[] once
                NetworkWriter bytes = MessagePacker.PackWriter(msg);
                bytes.recycleCount = 1;

                bool result = true;
                foreach (KeyValuePair <int, NetworkConnection> kvp in identity.observers)
                {
                    if (kvp.Value.isReady)
                    {
                        bytes.recycleCount++;
                        result &= kvp.Value.SendWriter(bytes, channelId);
                    }
                }
                NetworkWriterPool.Recycle(bytes);
                return(result);
            }
            return(false);
        }
        public bool InvokeHandler <T>(T msg) where T : IMessageBase
        {
            NetworkWriter writer = MessagePacker.PackWriter(msg);
            NetworkReader reader = NetworkReaderPool.GetPooledReader(writer.ToArraySegment());
            bool          result = InvokeHandler(MessagePacker.GetId <T>(), reader);

            NetworkReaderPool.Recycle(reader);
            NetworkWriterPool.Recycle(writer);
            return(result);
        }
Пример #4
0
        public static bool SendToAll <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Server.SendToAll id:" + typeof(T));
            }

            // pack message into byte[] once
            NetworkWriter bytes = MessagePacker.PackWriter(msg);

            bytes.recycleCount = (ushort)connections.Count;

            bool result = true;

            foreach (KeyValuePair <int, NetworkConnection> kvp in connections)
            {
                result &= kvp.Value.SendWriter(bytes, channelId);
            }
            return(result);
        }
Пример #5
0
        // connect host mode
        internal static void ConnectLocalServer()
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Client Connect Local Server");
            }

            RegisterSystemHandlers(true);

            connectState = ConnectState.Connected;

            // create local connection to server
            connection = new ULocalConnectionToServer();
            connection.SetHandlers(handlers);

            // create server connection to local client
            ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient();

            NetworkServer.SetLocalConnection(connectionToClient);

            localClientPacketQueue.Enqueue(new BufferHolder(MessagePacker.PackWriter(new ConnectMessage())));
        }
Пример #6
0
        // this is like SendToReady - but it doesn't check the ready flag on the connection.
        // this is used for ObjectDestroy messages.
        static bool SendToObservers <T>(NetworkIdentity identity, T msg) where T : IMessageBase
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Server.SendToObservers id:" + typeof(T));
            }

            if (identity != null && identity.observers != null)
            {
                // pack message into byte[] once
                NetworkWriter bytes = MessagePacker.PackWriter(msg);
                bytes.recycleCount = (ushort)identity.observers.Count;

                bool result = true;
                foreach (KeyValuePair <int, NetworkConnection> kvp in identity.observers)
                {
                    result &= kvp.Value.SendWriter(bytes);
                }
                return(result);
            }
            return(false);
        }
 public virtual bool Send <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
 {
     // pack message and send
     return(SendWriter(MessagePacker.PackWriter(msg), channelId));
 }