Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- s.Bind (new IPEndPoint (IPAddress.Any, port));
- while (true) {
- now = stopwatch.ElapsedTicks;
- deltaTime = (now - lastTime) / (float)Stopwatch.Frequency;
- lastTime = now;
- time += deltaTime;
- if (time > 30 || gameOver) {
- SendToAll (new byte[1] { (byte)(waitingForPlayers + 64) });
- s.Close ();
- return;
- }
- if (s.Poll (1000, SelectMode.SelectRead)) {
- int bytes = s.ReceiveFrom (buffer, ref recvEndpoint);
- if (bytes > 48) {
- sender = (IPEndPoint)recvEndpoint;
- byte[] data = new byte[bytes];
- Array.Copy (buffer, data, bytes);
- for (byte i = 0; i < 10; i++) {
- if (sender.Equals (connections[i])) {
- bool duplicate = false;
- foreach (IPEndPoint ip in team1Connections) {
- if (ip.Equals (sender)) {
- duplicate = true;
- break;
- }
- }
- if (duplicate)
- break;
- foreach (IPEndPoint ip in team2Connections) {
- if (ip.Equals (sender)) {
- duplicate = true;
- break;
- }
- }
- if (duplicate)
- break;
- s.SendTo (new byte[1] { (byte)(192 + waitingForPlayers) }, sender);
- new Player (this, i, sender, Encoding.ASCII.GetString (data, 55, data.Length - 55), data);
- if (i % 2 == 0)
- team1Connections.Add (sender);
- else
- team2Connections.Add (sender);
- }
- }
- }
- if (players.Count == maxPlayers)
- break;
- }
- Thread.Sleep (10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement