public IEnumerator Accepted(int roomPort, bool side, bool testing = false) { if (testing) { PacketLib.SendMessage(server, writer, (int)PostCode.TEST); yield return(new WaitForSeconds(0.5f)); } left = side; Debug.Log("Joining match in testMode: " + testing); StopAllCoroutines(); client.Stop(); yield return(new WaitForSeconds(.5f)); //join lobby StartCoroutine(Poll(roomPort)); yield return(new WaitForSeconds(1f)); if (!testing) { PacketLib.SendMessage(server, writer, (int)PostCode.INVITE, side); } else { PacketLib.SendMessage(server, writer, (int)PostCode.TEST); } //send test flips StartCoroutine(mGame.SendTestFlips()); }
private IEnumerator ManageQueue(NetDataWriter writer) { bool roomReady = false; int roomPort = 0; while (mQueue.Count > 1) { roomReady = false; foreach (KeyValuePair <int, IEnumerator> kvp in gameLobbies) { if (kvp.Value == null) { roomReady = true; roomPort = kvp.Key; IEnumerator gameLobby = ServeUDP(roomPort); gameLobbies[kvp.Key] = gameLobby; StartCoroutine(gameLobby); break; } } NetPeer left = mQueue.Dequeue(); NetPeer right = mQueue.Dequeue(); PacketLib.SendMessage(left, writer, (int)PostCode.INVITE, roomPort, true); PacketLib.SendMessage(right, writer, (int)PostCode.INVITE, roomPort, false); Console.WriteLine("2x[client] Match is ready on port: {0}!", roomPort); yield return(new WaitForSeconds(1)); } }
private IEnumerator Poll(int port) { writer = new NetDataWriter(); EventBasedNetListener listener = new EventBasedNetListener(); client = new NetManager(listener); client.Start(); client.Connect(matchMakeUrl /* host ip or name */, port /* port */, "TestKey" /* text key or NetDataWriter */); listener.PeerConnectedEvent += (fromPeer) => { print("PeerConnectedEvent"); server = fromPeer; PacketLib.SendMessage(server, writer, (int)PostCode.SHAKE); }; listener.NetworkReceiveEvent += (peer, reader, deliveryMethod) => { curCode = -1; try{ curCode = reader.GetInt(); } catch (Exception e) { debugText.text = "Error. malformatted packet"; } switch (curCode) { case 0: debugText.text = "Success. Connected to Server"; JoinQueue(); break; case 1: debugText.text = "Finding match..."; break; case 2: debugText.text = "Match invite received"; mGame.ReceiveMatchInvite(reader.GetInt(), reader.GetBool()); break; case 3: int startsIn = reader.GetInt(); if (startsIn > 0) { debugText.text = "Game starting in: " + startsIn; } else { debugText.text = "Game started!"; } break; case 4: Log("Received flip message on side: " + left); mGame.Flip(reader.GetBool(), reader.GetBool()); break; case 7: mGame.UpdateBall(reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat()); break; case 8: mGame.FullSyncBall(reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat()); break; case -1: default: break; } reader.Recycle(); }; while (true) { client.PollEvents(); yield return(new WaitForSeconds(0.015f)); } client.Stop(); }
public void Log(string logLine) { PacketLib.SendMessage(server, writer, (int)PostCode.LOG, logLine); }
public void ActivateFlipper(bool left, bool active) { Debug.Log("Sending flipper activation message"); PacketLib.SendMessage(server, writer, (int)PostCode.FLIP, left, active); }
public void JoinQueue() { PacketLib.SendMessage(server, writer, (int)PostCode.QUEUE); }
private IEnumerator ServeUDP(int port) { int curCode = -1; NetDataWriter writer = new NetDataWriter(); EventBasedNetListener listener = new EventBasedNetListener(); NetManager server = new NetManager(listener); Transform ball = null; Rigidbody ballBody = null; bool syncBall = false; Console.WriteLine("Starting Server on {0}", port); Debug.Log("Starting Server on " + port); server.Start(port); //left and right flipper for this port Transform left, right; FlipperButton fbLeft = null, fbRight = null; NetPeer lClient = null, rClient = null; bool bothPlayers = true; Transform playField = null; IEnumerator timeout = null;; if (port != 3000) { timeout = WaitForTimeout(port, server); StartCoroutine(timeout); Console.WriteLine("Finding play field"); playField = tables.Find(port.ToString()); Console.WriteLine("Finding ball"); ball = playField.Find("Ball"); ballBody = ball.GetComponent <Rigidbody>(); Console.WriteLine("Finding FlipperButton left"); fbLeft = playField.Find("PlayerInput").Find("PlayerLeft").Find("ButtonParent").GetChild(0).GetChild(0).GetComponent <FlipperButton>(); fbLeft.enabled = true; Console.WriteLine("Finding FlipperButton right"); fbRight = playField.Find("PlayerInput").Find("PlayerRight").Find("ButtonParent").GetChild(0).GetChild(0).GetComponent <FlipperButton>(); fbRight.enabled = true; Console.WriteLine("Found both flippers"); } listener.ConnectionRequestEvent += request => { if (server.PeersCount < 10) { request.AcceptIfKey("TestKey"); } else { request.Reject(); } }; listener.PeerConnectedEvent += peer => { Console.WriteLine("PeerConnectedEvent"); if (testMode) { lClient = peer; } }; listener.PeerDisconnectedEvent += (peer, info) => { //remove player from queue for (int i = 0; i < mQueue.Count; i++) { NetPeer nxt = mQueue.Dequeue(); if (nxt.ConnectionNum != peer.ConnectionNum) { mQueue.Enqueue(nxt); } else { Console.WriteLine("Dequeueing [client] due to disconnect event"); } } //remove player from game lobby if (lClient != null && rClient != null) { if (peer.ConnectionNum == lClient.ConnectionNum) { if (bothPlayers) { Console.WriteLine("dispatch lClient leave"); fbLeft.enabled = false; bothPlayers = false; } else { TeardownServer(server, port, 1); } } else if (peer.ConnectionNum == rClient.ConnectionNum) { if (bothPlayers) { Console.WriteLine("dispatch rClient leave"); fbRight.enabled = false; bothPlayers = false; } else { TeardownServer(server, port, 1); } } } }; listener.NetworkReceiveEvent += (peer, reader, delivery) => { curCode = -1; try{ curCode = reader.GetInt(); } catch (Exception e) { Console.WriteLine("error. malformatted packet"); } switch (curCode) { case 0: //handshake if (port == 3000) { Console.WriteLine("Client Handshake Received"); UnityEngine.Debug.Log("Client Handshake Received"); PacketLib.SendMessage(peer, writer, (int)PostCode.SHAKE); } break; case 1: //queue //add peer to queue mQueue.Enqueue(peer); StartCoroutine(ManageQueue(writer)); PacketLib.SendMessage(peer, writer, (int)PostCode.QUEUE); break; case 2: //invite Console.WriteLine("[client] has joined game lobby on port: " + port); Debug.Log("[client] has joined game lobby on port: " + port); //assign left and right flipper if (reader.GetBool()) { lClient = peer; if (rClient != null) { StartCoroutine(StartGame(lClient, rClient, writer, timeout, port, ball)); syncBall = true; } } else { rClient = peer; if (lClient != null) { StartCoroutine(StartGame(lClient, rClient, writer, timeout, port, ball)); syncBall = true; } } //get a handle to the table/playfield for this port break; case 4: //flip //rotate server paddle if (port != 3000) { bool isLeft = reader.GetBool(); bool active = reader.GetBool(); if (rClient != null) { PacketLib.SendMessage(rClient, writer, (int)PostCode.FLIP, isLeft, active); } if (lClient != null) { PacketLib.SendMessage(lClient, writer, (int)PostCode.FLIP, isLeft, active); } //move paddles Debug.Log("Animating paddle movement server-side"); if (fbLeft != null && isLeft) { fbLeft.Flip(active); } else if (fbRight != null && !isLeft) { fbRight.Flip(active); } } break; case 5: //log Console.WriteLine(reader.GetString()); break; case 6: //test if (timeout == null) { SystemTest(); } else { StopCoroutine(timeout); } break; case -1: default: break; } }; while (!Console.KeyAvailable) { server.PollEvents(); if (syncBall && port != 3000) { PacketLib.SendMessage(lClient, writer, (int)PostCode.BALL, ball.position, ballBody.angularVelocity); PacketLib.SendMessage(rClient, writer, (int)PostCode.BALL, ball.position, ballBody.angularVelocity); } yield return(new WaitForSeconds(0.015f)); } Console.WriteLine("Gracefully killing server"); server.Stop(); yield return(new WaitForSeconds(5f)); // wait 5 seconds after stopping server to kill app Application.Quit(); }