]> Dogcows Code - chaz/carfire/commitdiff
Chat implemented. Needs testing.
authorCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 22 Mar 2010 19:18:01 +0000 (19:18 +0000)
committerCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 22 Mar 2010 19:18:01 +0000 (19:18 +0000)
git-svn-id: https://bd85.net/svn/cs3505_group@28 92bb83a3-7c8f-8a45-bc97-515c4e399668

Project06/CS 3505 Project 06/CS 3505 Project 06/ChatPacket.cs [new file with mode: 0644]
Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs

diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/ChatPacket.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/ChatPacket.cs
new file mode 100644 (file)
index 0000000..15e4470
--- /dev/null
@@ -0,0 +1,52 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using Microsoft.Xna.Framework.Net;\r
+\r
+namespace CS_3505_Project_06\r
+{\r
+    /// <summary>\r
+    /// Small container class for the information concerning a chat packet.\r
+    /// It is immutable.\r
+    /// </summary>\r
+    public class ChatPacket\r
+    {\r
+        // Private member variables\r
+        #region Instance Variables\r
+\r
+        NetworkGamer mSender;\r
+        String mMessage;\r
+\r
+        #endregion\r
+\r
+\r
+        /// <summary>\r
+        /// Get the game who sent the chat packet.\r
+        /// </summary>\r
+        public NetworkGamer Sender\r
+        {\r
+            get { return mSender; }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get the message that was sent by the sender.\r
+        /// </summary>\r
+        public String Message\r
+        {\r
+            get { return mMessage; }\r
+        }\r
+\r
+\r
+        /// <summary>\r
+        /// Construct a chat packet with contents.\r
+        /// </summary>\r
+        /// <param name="sender">The chat sender.</param>\r
+        /// <param name="message">The chat message.</param>\r
+        public ChatPacket(NetworkGamer sender, String message)\r
+        {\r
+            mSender = sender;\r
+            mMessage = message;\r
+        }\r
+    }\r
+}\r
index 95d843ec0b851cdec0efe96de456e6bbfe1f8b93..03b6e90481812e40680b720009379c554f43b089 100644 (file)
@@ -17,37 +17,6 @@ namespace CS_3505_Project_06
     /// </summary>\r
     public class NetworkGame\r
     {\r
-        // Private class variable members\r
-        #region Instance Variables\r
-\r
-        NetworkSession mNetworkSession;\r
-\r
-        JoinedSessionDelegate mJoinedSessionDelegate;\r
-        FoundSessionsDelegate mFoundSessionsDelegate;\r
-\r
-        ILobby mLobby;\r
-        IDeterministicGame mGame;\r
-                                                                                                          \r
-        List<Keys> mLastPressedKeys = new List<Keys>();\r
-        bool mLastButtonPressed;\r
-\r
-        int mLatency;\r
-        long mNextLatencyAdjustmentFrame;\r
-        int mStallCount;\r
-        int mAverageOwd;\r
-\r
-        TimeSpan mTargetTimeSpan = new TimeSpan(166666);\r
-        public TimeSpan TargetTimeSpan\r
-        {\r
-            get\r
-            {\r
-                return mTargetTimeSpan;\r
-            }\r
-        }\r
-\r
-        #endregion\r
-\r
-\r
         /// <summary>\r
         /// Called when a session has been created or joined using CreateSession() or JoinSession().\r
         /// </summary>\r
@@ -149,7 +118,8 @@ namespace CS_3505_Project_06
 \r
 \r
         /// <summary>\r
-        /// Find available sessions to join.\r
+        /// Find available sessions to join.  You should not already be in a session when\r
+        /// calling this method; call LeaveSession first.\r
         /// </summary>\r
         /// <param name="callback">The delegate/method to call when the search finishes.</param>\r
         public void FindSessions(FoundSessionsDelegate callback)\r
@@ -167,7 +137,7 @@ namespace CS_3505_Project_06
 \r
         /// <summary>\r
         /// Join a network session found using FindSessions().  This is for joining a game that\r
-        /// somebody else has already started hosting.\r
+        /// somebody else has already started hosting.  You must not already be in a session.\r
         /// </summary>\r
         /// <param name="availableSession">Pass the session object to try to join.</param>\r
         /// <param name="callback">The delegate/method to call when the search finishes.</param>\r
@@ -191,7 +161,7 @@ namespace CS_3505_Project_06
 \r
         /// <summary>\r
         /// Leave and dispose of any currently associated network session.  You will find yourself\r
-        /// back in the lobby.\r
+        /// back in the lobby.  You must already be in a session to leave it.\r
         /// </summary>\r
         public void LeaveSession()\r
         {\r
@@ -299,13 +269,101 @@ namespace CS_3505_Project_06
         }\r
 \r
 \r
+        /// <summary>\r
+        /// Get the chat messages that have been receive since the last time this\r
+        /// method was called.\r
+        /// </summary>\r
+        /// <returns>List container of the chat messages.</returns>\r
+        public List<ChatPacket> ReceiveChats()\r
+        {\r
+            List<ChatPacket> chats = mChatPackets;\r
+            mChatPackets = new List<ChatPacket>();\r
+            return chats;\r
+        }\r
+\r
+        /// <summary>\r
+        /// Send a chat message to all gamers in the session.  You should already be\r
+        /// in a session before calling this method.\r
+        /// </summary>\r
+        /// <param name="message">The text of the message.</param>\r
+        public void SendChat(String message)\r
+        {\r
+            WriteChat(message);\r
+            LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Send a chat message to a specific gamer in the session.  You should already\r
+        /// be in a session before calling this method.\r
+        /// </summary>\r
+        /// <param name="message">The text of the message.</param>\r
+        /// <param name="recipient">The gamer to receive the message.</param>\r
+        public void SendChat(String message, NetworkGamer recipient)\r
+        {\r
+            WriteChat(message);\r
+            LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient);\r
+        }\r
+\r
+\r
+        // Private class variable members\r
+        #region Instance Variables\r
+\r
+        NetworkSession mNetworkSession;\r
+        PacketReader mPacketReader = new PacketReader();\r
+        PacketWriter mPacketWriter = new PacketWriter();\r
+\r
+        JoinedSessionDelegate mJoinedSessionDelegate;\r
+        FoundSessionsDelegate mFoundSessionsDelegate;\r
+\r
+        ILobby mLobby;\r
+        IDeterministicGame mGame;\r
+\r
+        List<ChatPacket> mChatPackets = new List<ChatPacket>();\r
+\r
+        List<Keys> mLastPressedKeys = new List<Keys>();\r
+        bool mLastButtonPressed;\r
+\r
+        int mLatency;\r
+        long mNextLatencyAdjustmentFrame;\r
+        int mStallCount;\r
+        int mAverageOwd;\r
+\r
+        TimeSpan mTargetTimeSpan = new TimeSpan(166666);\r
+        public TimeSpan TargetTimeSpan\r
+        {\r
+            get\r
+            {\r
+                return mTargetTimeSpan;\r
+            }\r
+        }\r
+\r
+        #endregion\r
+\r
+\r
         // Private implementation methods of the network protocol\r
         #region Private Implementation Methods\r
+        \r
+        enum PacketType\r
+        {\r
+            Chat = 1,\r
+            Event = 2,\r
+            Stall = 3\r
+        }\r
+\r
+        enum EventType\r
+        {\r
+            KeyDown = 1,\r
+            KeyUp = 2,\r
+            MouseDown = 3,\r
+            MouseUp = 4,\r
+            MouseMove = 5\r
+        }\r
+\r
 \r
         /// <summary>\r
         /// Reinitialize the private variables in preparation for new game to start.\r
         /// </summary>\r
-        private void Reset()\r
+        void Reset()\r
         {\r
             mLatency = 1;\r
             mNextLatencyAdjustmentFrame = 1;\r
@@ -323,49 +381,46 @@ namespace CS_3505_Project_06
         /// </summary>\r
         /// <param name="gameTime">Pass the time away.</param>\r
         /// <param name="spriteBatch">The sprite batch.</param>\r
-        private void ReadPackets()\r
+        void ReadPackets()\r
         {\r
-            PacketReader packetReader = new PacketReader();\r
-\r
             foreach (LocalNetworkGamer gamer in mNetworkSession.LocalGamers)\r
             {\r
                 while (gamer.IsDataAvailable)\r
                 {\r
                     NetworkGamer sender;\r
 \r
-                    gamer.ReceiveData(packetReader, out sender);\r
-                    byte packetId = packetReader.ReadByte();\r
+                    gamer.ReceiveData(mPacketReader, out sender);\r
+                    PacketType packetId = (PacketType)mPacketReader.ReadByte();\r
 \r
                     switch (packetId)\r
                     {\r
-                        // Chat Packet\r
-                        case 1:\r
-                            short messageLength = packetReader.ReadInt16();\r
-                            char[] message = packetReader.ReadChars(messageLength);\r
-\r
-                            ChatPacket chatPacket;\r
-                            chatPacket.sender = sender;\r
-                            chatPacket.message = new String(message);\r
+                        case PacketType.Chat:\r
+\r
+                            short messageLength = mPacketReader.ReadInt16();\r
+                            char[] message = mPacketReader.ReadChars(messageLength);\r
+\r
+                            ChatPacket chatPacket = new ChatPacket(sender, new String(message));\r
+                            mChatPackets.Add(chatPacket);\r
                             break;\r
 \r
-                        // Event Packet\r
-                        case 2:\r
-                            short stallCount = packetReader.ReadInt16();\r
-                            short averageOwd = packetReader.ReadInt16();\r
-                            int frameNumber = packetReader.ReadInt32();\r
-                            byte numEvents = packetReader.ReadByte();\r
+                        case PacketType.Event:\r
+\r
+                            short stallCount = mPacketReader.ReadInt16();\r
+                            short averageOwd = mPacketReader.ReadInt16();\r
+                            int frameNumber = mPacketReader.ReadInt32();\r
+                            byte numEvents = mPacketReader.ReadByte();\r
 \r
                             for (byte i = 0; i < numEvents; ++i)\r
                             {\r
-                                ReadEvent(packetReader, sender);\r
+                                ReadEvent(mPacketReader, sender);\r
                             }\r
 \r
                             break;\r
 \r
-                        // Stall Packet\r
-                        case 3:\r
-                            byte numStalledPeers = packetReader.ReadByte();\r
-                            byte[] stalledPeers = packetReader.ReadBytes(numStalledPeers);\r
+                        case PacketType.Stall:\r
+\r
+                            byte numStalledPeers = mPacketReader.ReadByte();\r
+                            byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers);\r
 \r
                             break;\r
                     }\r
@@ -373,39 +428,39 @@ namespace CS_3505_Project_06
             }\r
         }\r
 \r
-        private void ReadEvent(PacketReader packetReader, NetworkGamer sender)\r
+        void ReadEvent(PacketReader packetReader, NetworkGamer sender)\r
         {\r
-            byte eventId = packetReader.ReadByte();\r
+            EventType eventId = (EventType)packetReader.ReadByte();\r
             long applicationFrame = packetReader.ReadInt32();\r
 \r
             switch (eventId)\r
             {\r
-                // Key Down\r
-                case 1:\r
+                case EventType.KeyDown:\r
+\r
                     int keyCode1 = packetReader.ReadInt32();\r
 \r
                     break;\r
 \r
-                // Key Up\r
-                case 2:\r
+                case EventType.KeyUp:\r
+\r
                     int keyCode2 = packetReader.ReadInt32();\r
 \r
                     break;\r
 \r
-                // Mouse Down\r
-                case 3:\r
+                case EventType.MouseDown:\r
+\r
                     byte buttonId1 = packetReader.ReadByte();\r
 \r
                     break;\r
 \r
-                // Mouse Up\r
-                case 4:\r
+                case EventType.MouseUp:\r
+\r
                     byte buttonId2 = packetReader.ReadByte();\r
 \r
                     break;\r
 \r
-                // Mouse Move\r
-                case 5:\r
+                case EventType.MouseMove:\r
+\r
                     short x = packetReader.ReadInt16();\r
                     short y = packetReader.ReadInt16();\r
 \r
@@ -413,8 +468,15 @@ namespace CS_3505_Project_06
             }\r
         }\r
 \r
+        void WriteChat(String message)\r
+        {\r
+            mPacketWriter.Write((byte)PacketType.Chat);\r
+            mPacketWriter.Write((short)message.Length);\r
+            mPacketWriter.Write(message.ToCharArray());\r
+        }\r
+\r
 \r
-        private bool IsLatencyAdjustmentFrame\r
+        bool IsLatencyAdjustmentFrame\r
         {\r
             get\r
             {\r
@@ -423,13 +485,13 @@ namespace CS_3505_Project_06
             }\r
         }\r
 \r
-        private void AdjustLatency()\r
+        void AdjustLatency()\r
         {\r
             // TODO\r
         }\r
 \r
 \r
-        private void SendLocalEvents()\r
+        void SendLocalEvents()\r
         {\r
             // TODO: Not finished.\r
 \r
@@ -455,7 +517,7 @@ namespace CS_3505_Project_06
         }\r
 \r
 \r
-        private bool HaveNeededEvents\r
+        bool HaveNeededEvents\r
         {\r
             get\r
             {\r
@@ -464,13 +526,13 @@ namespace CS_3505_Project_06
             }\r
         }\r
 \r
-        private void ApplyEvents()\r
+        void ApplyEvents()\r
         {\r
             // TODO\r
         }\r
 \r
 \r
-        private int AverageOneWayDelay\r
+        int AverageOneWayDelay\r
         {\r
             get\r
             {\r
This page took 0.036468 seconds and 4 git commands to generate.