Example #1
0
        // Use this for initialization
        private void Start()
        {
            //try
            //{
            string destPlatform = String.Empty;

#if UNITY_ANDROID
            destPlatform = "Android";
#endif
#if UNITY_IPHONE
            destPlatform = "iOS";
#endif
            string fileNameWithPath = String.Format("TextFiles/{0}.txt", destPlatform);

            // Get data from Resources.
            IResourceManager resourceManager = new ResourceManager();
            TextAsset        asset           = (TextAsset)resourceManager.LoadResourceImmediate(typeof(TextAsset), fileNameWithPath);
            String           text            = asset.text;
            string           host            = resourceManager.GetInformationFromFile(text, "HOST");
            port = Convert.ToInt32(resourceManager.GetInformationFromFile(text, "PORT"));

            // Get data from Streaming Assets
            string         fileRoot      = Application.streamingAssetsPath;
            string         fullPath      = fileRoot + "/GameServer.txt";
            IConfigManager configManager = new ConfigManager();
            host = configManager.GetInformationFromFile(fullPath, host);

            // Invoke IP Manger for IP Protocol
            ILogManager logManager = new LogManager();
            logManager.Initialize(TargetEnvironment.Test);

            IIPNetworkFactory ipNetworkFactory = new IPNetworkFactory();
            IPNetworkManager  ipNetworkManager = new IPNetworkManager(ipNetworkFactory, logManager);

            IPProtocol           ipProtocol           = ipNetworkManager.GetNetworkProtocolsFromDNS(host);
            IPAddress            netIPAddress         = IPProtocol.IPv4 == ipProtocol ? IPAddress.Any : IPAddress.IPv6Any;
            AddressFamily        netAddressFamily     = IPProtocol.IPv4 == ipProtocol ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
            NetGameConfiguration netGameConfiguration = new NetGameConfiguration(netIPAddress, netAddressFamily);

            // Convert IP address
            IPAddress ipAddress = ipNetworkManager.GetHostAddressForType(host, ipProtocol);
            hostip = ipAddress.ToString();


            // Create new instance of configs. Parameter is "application Id". It has to be same on client and server.
            NetPeerConfiguration Config = new NetPeerConfiguration("game", netGameConfiguration);

            // Create new client, with previously created configs
            Client = new NetClient(Config, netGameConfiguration);

            // Create new outgoing message
            NetOutgoingMessage outmsg = Client.CreateMessage();


            //LoginPacket lp = new LoginPacket("Katu");

            // Start client
            Client.Start();

            // Write byte ( first byte informs server about the message type ) ( This way we know, what kind of variables to read )
            outmsg.Write((byte)PacketTypes.LOGIN);

            // Write String "Name" . Not used, but just showing how to do it
            outmsg.Write("MyName");

            // Connect client, to ip previously requested from user
            Client.Connect(hostip, port, outmsg);


            ResultText = "Client Started";

            // Create the list of characters
            GameStateList = new List <Character>();

            // Funtion that waits for connection approval info from server
            WaitForStartingInfo();
            //}
            //catch (Exception ex)
            //{
            //	ErrorText = ex.ToString();
            //}
        }
Example #2
0
        static void Main()
        {
            // Ask for IP
            // Read Ip to string
            string host = ConfigurationManager.AppSettings["host"];

            port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);

            ILogManager logManager = new LogManager();

            logManager.Initialize(TargetEnvironment.Test);

            IIPNetworkFactory ipNetworkFactory = new IPNetworkFactory();
            IPNetworkManager  ipNetworkManager = new IPNetworkManager(ipNetworkFactory, logManager);

            IPProtocol           ipProtocol           = ipNetworkManager.GetNetworkProtocolsFromDNS(host);
            IPAddress            netIPAddress         = IPProtocol.IPv4 == ipProtocol ? IPAddress.Any : IPAddress.IPv6Any;
            AddressFamily        netAddressFamily     = IPProtocol.IPv4 == ipProtocol ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
            NetGameConfiguration netGameConfiguration = new NetGameConfiguration(netIPAddress, netAddressFamily);

            // Convert IP address
            IPAddress ipAddress = ipNetworkManager.GetHostAddressForType(host, ipProtocol);

            hostip = ipAddress.ToString();
            Console.WriteLine("Enter IP To Connect - {0}:{1}", hostip, port);
            Console.Read();

            // Create new instance of configs. Parameter is "application Id". It has to be same on client and server.
            NetPeerConfiguration Config = new NetPeerConfiguration("game", netGameConfiguration);

            // Create new client, with previously created configs
            Client = new NetClient(Config, netGameConfiguration);

            // Create new outgoing message
            NetOutgoingMessage outmsg = Client.CreateMessage();


            //LoginPacket lp = new LoginPacket("Katu");

            // Start client
            Client.Start();

            // Write byte ( first byte informs server about the message type ) ( This way we know, what kind of variables to read )
            outmsg.Write((byte)PacketTypes.LOGIN);

            // Write String "Name" . Not used, but just showing how to do it
            outmsg.Write("MyName");

            // Connect client, to ip previously requested from user
            Client.Connect(hostip, port, outmsg);


            Console.WriteLine("Client Started");

            // Create the list of characters
            GameStateList = new List <Character>();

            // Set timer to tick every 50ms
            update = new System.Timers.Timer(50);

            // When time has elapsed ( 50ms in this case ), call "update_Elapsed" funtion
            update.Elapsed += new System.Timers.ElapsedEventHandler(update_Elapsed);

            // Funtion that waits for connection approval info from server
            WaitForStartingInfo();

            // Start the timer
            update.Start();

            // While..running
            while (IsRunning)
            {
                // Just loop this like madman
                GetInputAndSendItToServer();
            }
        }