/// <summary> /// NetPeerConfiguration constructor /// </summary> public NetPeerConfiguration(string appIdentifier, NetGameConfiguration netGameConfiguration) { m_NetGameConfiguration = netGameConfiguration; if (string.IsNullOrEmpty(appIdentifier)) { throw new NetException("App identifier must be at least one character long"); } m_appIdentifier = appIdentifier; // // default values // m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess; m_networkThreadName = "Lidgren network thread"; m_localAddress = m_NetGameConfiguration.NetIPAddress; m_broadcastAddress = IPAddress.Broadcast; var ip = NetUtility.GetBroadcastAddress(); if (ip != null) { m_broadcastAddress = ip; } m_port = 0; m_receiveBufferSize = 131071; m_sendBufferSize = 131071; m_acceptIncomingConnections = false; m_maximumConnections = 32; m_defaultOutgoingMessageCapacity = 16; m_pingInterval = 4.0f; m_connectionTimeout = 25.0f; m_useMessageRecycling = true; m_resendHandshakeInterval = 3.0f; m_maximumHandshakeAttempts = 5; m_autoFlushSendQueue = true; m_maximumTransmissionUnit = kDefaultMTU; m_autoExpandMTU = false; m_expandMTUFrequency = 2.0f; m_expandMTUFailAttempts = 5; m_unreliableSizeBehaviour = NetUnreliableSizeBehaviour.IgnoreMTU; m_loss = 0.0f; m_minimumOneWayLatency = 0.0f; m_randomOneWayLatency = 0.0f; m_duplicates = 0.0f; m_isLocked = false; }
/// <summary> /// NetPeer constructor /// </summary> public NetPeer(NetPeerConfiguration config, NetGameConfiguration netGameConfiguration) { m_configuration = config; NetGameConfiguration = netGameConfiguration; m_statistics = new NetPeerStatistics(this); m_releasedIncomingMessages = new NetQueue <NetIncomingMessage>(4); m_unsentUnconnectedMessages = new NetQueue <NetTuple <IPEndPoint, NetOutgoingMessage> >(2); m_connections = new List <NetConnection>(); m_connectionLookup = new Dictionary <IPEndPoint, NetConnection>(); m_handshakes = new Dictionary <IPEndPoint, NetConnection>(); IPAddress iPAddress = netGameConfiguration.NetIPAddress; m_senderRemote = (EndPoint) new IPEndPoint(iPAddress, 0); m_status = NetPeerStatus.NotRunning; m_receivedFragmentGroups = new Dictionary <NetConnection, Dictionary <int, ReceivedFragmentGroup> >(); }
// 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(); //} }
/// <summary> /// NetClient constructor /// </summary> /// <param name="config"></param> /// <param name="netGameConfiguration"></param> public NetClient(NetPeerConfiguration config, NetGameConfiguration netGameConfiguration) : base(config, netGameConfiguration) { config.AcceptIncomingConnections = false; }
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(); } }
/// <summary> /// NetServer constructor /// </summary> public NetServer(NetPeerConfiguration config, NetGameConfiguration netGameConfiguration) : base(config, netGameConfiguration) { config.AcceptIncomingConnections = true; }