/// </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
\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
\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
\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
}\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
/// </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
}\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
}\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
}\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
}\r
\r
\r
- private bool HaveNeededEvents\r
+ bool HaveNeededEvents\r
{\r
get\r
{\r
}\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