/// <summary> /// 获取大交通 /// </summary> /// <param name="StartCityName">出发城市</param> /// <param name="trafficType">交通</param> /// <returns></returns> protected string GetBigTraffic(string StartCityName, TrafficType trafficType, string EndCityName, TrafficType endTraffic) { string traffic = string.Empty; traffic = StartCityName + trafficType.ToString() + "出发 " + endTraffic.ToString() + EndCityName + " 回"; return(traffic); }
// Start is called before the first frame update void Start() { //Find all possible routes from current spawn location List <RoutesList> possibleRoutes = null; switch (VehicleType) { case TrafficType.Car: possibleRoutes = FindObjectOfType <TrafficSpawner>().CarRoutes.Where(r => r.From == spawnLocation).ToList(); break; case TrafficType.Train: possibleRoutes = FindObjectOfType <TrafficSpawner>().TrainRoutes.Where(r => r.From == spawnLocation).ToList(); break; case TrafficType.Boat: possibleRoutes = FindObjectOfType <TrafficSpawner>().BoatRoutes.Where(r => r.From == spawnLocation).ToList(); break; case TrafficType.Bicycle: possibleRoutes = FindObjectOfType <TrafficSpawner>().CyclistRoutes.Where(r => r.From == spawnLocation).ToList(); hasAnimations = true; animator = GetComponent <Animator>(); break; case TrafficType.Pedestrian: possibleRoutes = FindObjectOfType <TrafficSpawner>().PedestrianRoutes.Where(r => r.From == spawnLocation).ToList(); hasAnimations = true; animator = GetComponent <Animator>(); break; } //Select random route if there are valid routes if (possibleRoutes != null) { var index = UnityEngine.Random.Range(0, possibleRoutes.Count - 1); route = possibleRoutes[index]; } else //No valid routes found { Debug.LogError($"ERROR: Failed to create route for vehicle type: {VehicleType.ToString()}"); Destroy(this); } //Set first waypoint nextWaypoint = route.Route[waypointIndex]; //Set correct rotation facing the target location Quaternion targetRotation = Quaternion.LookRotation(nextWaypoint.transform.position - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1000); }
/// <summary> /// Starts listening in an infinite loop for incomming connections from RegistrarClient. Here are the /// scenarios: /// Client Request Client Status in Table Table Action Server Response to Client /// ------------------- ---------------------- ------------ ------------------------- /// 1 Msg(Join,GIP,GP,N) (CIP, GIP, GP) entry NOT FOUND Insert Msg(Confirm,GIP,GP,RndConfirmNum) /// 2 Msg(Join,GIP,GP,N) (CIP, GIP, GP) entry FOUND None Msg(ReConfirm,GIP,GP,FoundConfirmNum) /// 3 Msg(Leave,GIP,GP,ConfirmNum) Entry Found ConirmNum match Delete Msg(Confirm,GIP,GP,FoundConfirmNum) /// 4 Msg(Leave,GIP,GP,ConfirmNum) Entry Found ConirmNum mismatch None Msg(ConfirmNumMismatchError,0,0,0) /// 5 Msg(Leave,GIP,GP,ConfirmNum) Entry Not Found None Msg(LeaveWithoutJoinError,0,0,0) /// 6 Msg(Leave,GIP,GP,ConfirmNum) Lookup Error None Msg(UnknownError,0,0,0) /// /// </summary> public void Start() { TrafficType traffTypes = TrafficType.None; if (idxRSThreadTypeData < ThreadTypeData.Length) { lock (ThreadTypeData) { traffTypes = ThreadTypeData[idxRSThreadTypeData]; System.Threading.Thread.CurrentThread.Name = "Reflector_RegistrarServer_" + traffTypes.ToString(); idxRSThreadTypeData++; } } else { throw new Exception("Number of created threads exceed the number of thread types defined."); } IPEndPoint localEP; if ((traffTypes & TrafficType.IPv6) == TrafficType.IPv6) { localEP = new IPEndPoint(IPAddress.IPv6Any, ReflectorMgr.RegistrarServerPort); } else { localEP = new IPEndPoint(IPAddress.Any, ReflectorMgr.RegistrarServerPort); } // Initializing the TCP server NetworkStream netStream; BinaryFormatter bf = new BinaryFormatter(); Object obj; RegisterMessage regMsg, regMsgResponse; // Initializing the TcpListener TcpListener tcpL = new TcpListener(localEP); if ((traffTypes & TrafficType.IPv6) == TrafficType.IPv6) { tcpLv6 = tcpL; } else { tcpLv4 = tcpL; } tcpL.Start(); Socket srvS = null; // An inifinite loop which works as follows: // 1. Block until a connection arrives. // 2. If it is a join request // Lookup (ClientIP,GroupIP,GroupPort) and send back Confirm or ReConfirm // 2. If it is a leave request // Lookup (ClientIP,GroupIP,GroupPort) and if exist send back Confirm or ConfirmNumMismatch // if not exist send back LeaveWithoutJoinError // if unknown error send back UnknownError // 2. If neither join nor leave // send back InvalidRequest // 3. Close the connection. // while (true) { try { srvS = tcpL.AcceptSocket(); // Can't use AcceptTcpClient since the client information is protected netStream = new NetworkStream(srvS, System.IO.FileAccess.ReadWrite, true); //Getting a network stream so I can serialize the class into it. // Getting the request Object from stream obj = bf.Deserialize(netStream); regMsg = (RegisterMessage)obj; eventLog.WriteEntry("RegistrarServer: Received Request:" + regMsg.ToString(), EventLogEntryType.Information, (int)ReflectorEventLog.ID.JoinLeave); switch (regMsg.msgType) { case MessageType.Join: // Processing Join Request regMsgResponse = ProcessJoinRequest(regMsg, ((IPEndPoint)srvS.RemoteEndPoint).Address); break; case MessageType.Leave: // Processing Leave Request regMsgResponse = ProcessLeaveRequest(regMsg, ((IPEndPoint)srvS.RemoteEndPoint).Address); break; default: // Invalid Message Type regMsgResponse = new RegisterMessage(MessageType.InvalidRequest, IPAddress.Any); break; } bf.Serialize(netStream, regMsgResponse); netStream.Close(); srvS.Close(); } // On stopping the service, avoid the AbortException written in the event viewer catch (ThreadAbortException) {} catch (Exception e) { eventLog.WriteEntry("RegistrarServer: Network Error while receiving/sending request message. Client: " + ((srvS != null)?((IPEndPoint)srvS.RemoteEndPoint).ToString():"") + " " + e.ToString(), EventLogEntryType.Error, (int)ReflectorEventLog.ID.Error); } } // end while } // end Start method