Ejemplo n.º 1
0
        public override void Connect(string host, int port)
        {
            if (ClientLidgrenNetwork != null)
            {
                ClientLidgrenNetwork.Close();
                ClientLidgrenNetwork = null;
            }

            var config   = new NetworkConfiguration(ClientConfiguration.Instance.Host, ClientConfiguration.Instance.Port);
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream("Intersect.Client.public-intersect.bek"))
            {
                var rsaKey = EncryptionKey.FromStream <RsaKey>(stream);
                Debug.Assert(rsaKey != null, "rsaKey != null");
                ClientLidgrenNetwork = new ClientNetwork(config, rsaKey.Parameters);
            }

            if (ClientLidgrenNetwork == null)
            {
                return;
            }

            ClientLidgrenNetwork.Handler             = AddPacketToQueue;
            ClientLidgrenNetwork.OnConnected        += delegate { OnConnected(); };
            ClientLidgrenNetwork.OnDisconnected     += delegate { OnDisconnected(); };
            ClientLidgrenNetwork.OnConnectionDenied += delegate { OnConnectionFailed(true); };

            if (!ClientLidgrenNetwork.Connect())
            {
                Log.Error("An error occurred while attempting to connect.");
            }
        }
Ejemplo n.º 2
0
        public static void InitNetwork()
        {
            if (EditorLidgrenNetwork == null)
            {
                var config = new NetworkConfiguration(
                    ClientConfiguration.Instance.Host, ClientConfiguration.Instance.Port
                    );

                var assembly = Assembly.GetExecutingAssembly();
                using (var stream = assembly.GetManifestResourceStream("Intersect.Editor.public-intersect.bek"))
                {
                    var rsaKey = EncryptionKey.FromStream <RsaKey>(stream);
                    Debug.Assert(rsaKey != null, "rsaKey != null");
                    EditorLidgrenNetwork = new ClientNetwork(config, rsaKey.Parameters);
                }

                EditorLidgrenNetwork.Handler             = PacketHandler.HandlePacket;
                EditorLidgrenNetwork.OnDisconnected     += HandleDc;
                EditorLidgrenNetwork.OnConnectionDenied += delegate
                {
                    Connecting       = false;
                    ConnectionDenied = true;
                };
            }

            if (!Connected)
            {
                Connecting = true;
                if (!EditorLidgrenNetwork.Connect())
                {
                    Log.Error("An error occurred while attempting to connect.");
                }
            }
        }
        public static void InitNetwork()
        {
            if (EditorLidgrenNetwork == null)
            {
                var logger             = Log.Default;
                var packetTypeRegistry = new PacketTypeRegistry(logger);
                if (!packetTypeRegistry.TryRegisterBuiltIn())
                {
                    throw new Exception("Failed to register built-in packets.");
                }

                var packetHandlerRegistry = new PacketHandlerRegistry(packetTypeRegistry, logger);
                var networkHelper         = new NetworkHelper(packetTypeRegistry, packetHandlerRegistry);
                PackedIntersectPacket.AddKnownTypes(networkHelper.AvailablePacketTypes);
                var virtualEditorContext = new VirtualEditorContext(networkHelper, logger);
                PacketHandler = new PacketHandler(virtualEditorContext, packetHandlerRegistry);

                var config = new NetworkConfiguration(
                    ClientConfiguration.Instance.Host, ClientConfiguration.Instance.Port
                    );

                var assembly = Assembly.GetExecutingAssembly();
                using (var stream = assembly.GetManifestResourceStream("Intersect.Editor.network.handshake.bkey.pub"))
                {
                    var rsaKey = EncryptionKey.FromStream <RsaKey>(stream);
                    Debug.Assert(rsaKey != null, "rsaKey != null");
                    EditorLidgrenNetwork = new ClientNetwork(networkHelper, config, rsaKey.Parameters);
                }

                EditorLidgrenNetwork.Handler             = PacketHandler.HandlePacket;
                EditorLidgrenNetwork.OnDisconnected     += HandleDc;
                EditorLidgrenNetwork.OnConnectionDenied += delegate
                {
                    Connecting       = false;
                    ConnectionDenied = true;
                };
            }

            if (!Connected)
            {
                Connecting = true;
                if (!EditorLidgrenNetwork.Connect())
                {
                    Log.Error("An error occurred while attempting to connect.");
                }
            }
        }