예제 #1
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
 static void PeerConnected(P2PNetClass netClass, Peer peer)
 {
     Console.WriteLine(string.Format("{0}: {1} connected", netClass.CurrentUser, peer.name));
 }
예제 #2
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
 static void PlayerJoined(P2PNetClass netClass, string username)
 {
     peerStates[netClass.CurrentUser].peerFrameStates[username] = new PeerFrameState();
     peerStates[netClass.CurrentUser].peerFrameStates[username].frameEvents = new Queue<List<GameEvent>>();
     peerStates[netClass.CurrentUser].otherPeers.Add(username);
     Console.WriteLine(string.Format("{0}: {1} joined game", netClass.CurrentUser, username));
 }
예제 #3
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
        static void ParseGameData(P2PNetClass netClass, Peer peer, byte[] data)
        {
            PeerState currentPeerState = peerStates[netClass.CurrentUser];
            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(data))
            {
                using (System.IO.BinaryReader reader = new System.IO.BinaryReader(memStream))
                {
                    List<GameEvent> events = new List<GameEvent>();

                    try
                    {
                        while (true)
                        {
                            GameEventType eventType = (GameEventType)reader.ReadInt32();

                            switch (eventType)
                            {
                                case GameEventType.GameState:
                                    int state = reader.ReadInt32();
                                    events.Add(new GameEvent(eventType, state));
                                    break;
                                case GameEventType.GameEvent:
                                    int gameEvent = reader.ReadInt32();
                                    events.Add(new GameEvent(eventType, gameEvent));
                                    break;
                            }
                        }
                    }
                    catch (System.IO.EndOfStreamException) { }

                    currentPeerState.peerFrameStates[peer.name].frameEvents.Enqueue(events);
                }
            }
        }
예제 #4
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            peerStates = new Dictionary<string, PeerState>();

            server = new P2PNetClass(50000, "server");

            peers.Add(server);

            List<P2PNetClass> clients = new List<P2PNetClass>();
            for (int i = 0; i < NUM_PEERS - 1; ++i)
            {
                P2PNetClass peer = new P2PNetClass(System.Net.IPAddress.Loopback, 50000, "client" + (i + 1));

                peers.Add(peer);
            }

            foreach (P2PNetClass peer in peers)
            {
                PeerState peerState = peerStates[peer.CurrentUser] = new PeerState();
                peerState.localFrameEvents = new Queue<List<GameEvent>>();
                peerState.localStates = new Queue<int>();
                peerState.localPendingEvents = new List<GameEvent>();
                peerState.peerFrameStates = new Dictionary<string, PeerFrameState>();
                peerState.otherPeers = new List<string>();

                peer.JoinedGame += new JoinedGameHandler(JoinGame);
                peer.PeerConnected += new PeerConnectedHandler(PeerConnected);
                peer.PlayerJoined += new PlayerJoinedHandler(PlayerJoined);
                peer.GameStarted += new GameStartedHandler(GameStarted);
                peer.GameDataArrived += new GameDataHandler(GameDataArrived);
            }

            for (int i = 0; ; ++i)
            {
                foreach (P2PNetClass peer in peers)
                {
                    peer.CheckEvents();
                }

                if (i == 3)
                    server.StartGame(1);
                else if (i > 5)
                {
                    if (i > 6)
                        CheckGameStates();

                    foreach (P2PNetClass peer in peers)
                    {
                        PeerState peerState = peerStates[peer.CurrentUser];

                        peerState.localStates.Enqueue(peerState.currentState);
                        peerState.localPendingEvents.Add(new GameEvent(GameEventType.GameState, peerState.currentState));

                        if (i > 6)
                        {
                            int max = ran.Next(0, 5);
                            for (int j = 0; j < max; ++j)
                            {
                                peerState.localPendingEvents.Add(new GameEvent(GameEventType.GameEvent, ran.Next()));
                            }
                        }

                        peer.SendData(P2PNotices.PeerGameData, CreateGameData(peerState));
                    }
                }

                System.Threading.Thread.Sleep(500);
            }
        }
예제 #5
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
        static void JoinGame(P2PNetClass netClass, List<Peer> otherPeers)
        {
            peerStates[netClass.CurrentUser].otherPeers = new List<string>();

            foreach (Peer peer in otherPeers)
            {
                peerStates[netClass.CurrentUser].peerFrameStates[peer.name] = new PeerFrameState();
                peerStates[netClass.CurrentUser].peerFrameStates[peer.name].frameEvents = new Queue<List<GameEvent>>();
                peerStates[netClass.CurrentUser].otherPeers.Add(peer.name);
            }

            Console.WriteLine(string.Format("{0}: joined game with {1}", netClass.CurrentUser,
                string.Join(",", otherPeers.ConvertAll((Peer p) => { return p.name; }).ToArray())));
        }
예제 #6
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
 static void GameStarted(P2PNetClass netClass, int seed)
 {
     peerStates[netClass.CurrentUser].gameStateGenerator = new Random(seed);
     peerStates[netClass.CurrentUser].currentState = seed;
     Console.WriteLine("{0}: Game started with seed {1}", netClass.CurrentUser, seed);
 }
예제 #7
0
파일: Program.cs 프로젝트: ptsoccer/P2PGame
 static void GameDataArrived(P2PNetClass netClass, Peer peer, byte[] data)
 {
     ParseGameData(netClass, peer, data);
 }