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