public void UpdateMessagePump() { if (!isStarted) { return; } int recieavedHostID; int chanelId; byte[] recvBuffer = new byte[BUFFER_SIZE]; int DataSize; NetworkEventType type = NetworkTransport.Receive(out recieavedHostID, out connectionId, out chanelId, recvBuffer, BUFFER_SIZE, out DataSize, out error); switch (type) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: Debug.Log("sucsessfully connected to the server as user: "******""; if (!isConnected) { disconnectedMsg = "can't connect to server."; Debug.Log(disconnectedMsg); } else { disconnectedMsg = "disconnected from the server."; Debug.Log(disconnectedMsg); } // disconnect client and set disconnected message disconnect(); break; case NetworkEventType.DataEvent: ClientNET_RecieaveFunction.OnDataRecieaved(recvBuffer); // Debug.Log("recieaved data type: " + recvBuffer[0]); break; default: case NetworkEventType.BroadcastEvent: Debug.Log("unexpected network event type."); break; } }
public void init() { // protocol init NetworkTransport.Init(); ClientNET_RecieaveFunction.init(this); ClientNET_SendFunction.init(this); ConnectionConfig cc = new ConnectionConfig(); reliableChanel = cc.AddChannel(QosType.Reliable); // ReliableSequenced, Reliable unReliableChanel = cc.AddChannel(QosType.Unreliable); HostTopology topo = new HostTopology(cc, MAX_USER); hostId = NetworkTransport.AddHost(topo, 0); /*--------------------------------------------------*/ Debug.Log(ServerHost); ConnectionMenu.setConnectionStatusMessage("connecting to " + ServerHost); if (ServerHost != "localhost" && ServerHost != "127.0.0.1") // FIXME: check if is on local area // check if player is connected { try { IPAdress = System.Net.Dns.GetHostEntry("google.com").AddressList[0].ToString(); ConnectionMenu.setConnectionStatusMessage("Checking connection status."); Debug.Log("Checking connection status."); } catch (System.Exception e) { ConnectionMenu.setConnectionStatusMessage("error: you are not connected to the internet."); Debug.Log("error: you are not connected to the internet."); disconnect(); // disconnect client } } // resolve hostname try { IPAdress = System.Net.Dns.GetHostEntry(ServerHost).AddressList[0].ToString(); ConnectionMenu.setConnectionStatusMessage("Resolving hostname " + ServerHost); canConnect = true; } catch (System.Exception e) { ConnectionMenu.setConnectionStatusMessage("error: hostname " + ServerHost + " dosen't exist"); Debug.Log("error: hostname " + ServerHost + "dosen't exist"); disconnect(); // disconnect client } /*--------------------------------------------------*/ if (canConnect) { // try to connect connectionId = NetworkTransport.Connect(hostId, IPAdress, PORT, 0, out error); Debug.Log("try to connect to " + ServerHost + " | " + IPAdress + ":" + PORT); ConnectionMenu.setConnectionStatusMessage("Connecting to " + ServerHost + ":" + PORT + "..."); if (error != 0) { Debug.Log("can't connect to server | error: " + error + " | retriying..."); ConnectionMenu.setConnectionStatusMessage("can't connect to server | error: " + error + " | retriying..."); init(); } else { isStarted = true; } } }