public GameUpdateEventArgs(GameMessage message, byte[] gameState, int length) { this.Message = message; this.GameState = new byte[length]; Array.Copy(gameState, 0, this.GameState, 0, length); }
public void ProcessMessage(GameMessage gameMessage, byte[] data) { Trace.WriteLine(string.Format("Recv Messsage: {0} - {1} - {2}", this.Device.User, gameMessage.ToString(), data.Length)); LastRecvTime = DateTime.Now; Database.GameState state = new Database.GameState(); state.PkGameState = Guid.NewGuid(); state.Game = this.Device.Game; state.Master = false; state.Device = this.Device.Device; state.Data = data; state.Date = DateTime.Now; state.Active = false; state.Received = true; try { lock (_gameDb) { _gameDb.GameStates.InsertOnSubmit(state); _gameDb.SubmitChanges(); } } catch { } // we got our game state if (null != GotGameUpdate) { GotGameUpdate(this, new GameUpdateEventArgs(gameMessage, data, data.Length)); } }
public void SendUpdate(GameMessage message, byte[] data) { try { LastSendTime = DateTime.Now; Trace.WriteLine(string.Format("Send Messsage: {0} - {1} - {2}", this.Device.User, message.ToString(), data.Length)); SendUpdate(message, _client, _endPoint, data, _identifier++); } catch (Exception ex) { Console.WriteLine("ERROR SendUpdate"); Console.WriteLine(ex.ToString()); this.Close(); } }
public static void SendUpdate(GameMessage message, UdpClient client, IPEndPoint endPoint, byte[] data, Int32 identifier) { int timeStamp = identifier;// (int)((int)(DateTime.Now.Ticks - StartTicks)); byte[] timeStampBytes = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeStamp)); byte[] package = new byte[5 + data.Length]; timeStampBytes.CopyTo(package, 0); package[4] = (byte)message; data.CopyTo(package, 5); client.Send(package, package.Length, endPoint); }
public void EnqueuePacket(IPEndPoint endPoint, Int32 timeStamp, GameMessage message, byte[] data) { lock (_packets) { GamePacket packet = new GamePacket(); packet.EndPoint = endPoint; packet.TimeStamp = timeStamp; packet.Message = message; packet.Data = data; _packets.Add(packet); } _waitEvent.Set(); }
static void ProcessMessage(UdpClient client, byte[] bytes, IPEndPoint endPoint) { // Send this - TIME (4-bytes)|MessageType|MessageData int messageOffset = 0; Int32 timeStamp = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, messageOffset)); messageOffset += 4; GameMessage gameMessage = (GameMessage)bytes[messageOffset]; messageOffset += 1; byte[] data = new byte[bytes.Length - messageOffset]; Array.Copy(bytes, messageOffset, data, 0, data.Length); int hash = endPoint.ToString().GetHashCode(); if (_mappings.ContainsKey(hash) && !_mappings[hash].Game.Completed) { GameInstance instance = _mappings[hash]; instance.EnqueuePacket(endPoint, timeStamp, gameMessage, data); } else { // We aren't a game member yet int offset = 0; Int16 queryStringLength = 0; if (data.Length >= 2) { queryStringLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, offset)); offset += 2; } if (gameMessage == GameMessage.Replay) { Thread thread = new Thread(new ParameterizedThreadStart(ReplayGame)); thread.Start(new object[] { client, endPoint }); } else if (gameMessage == GameMessage.Create) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received Create: {0}", queryString)); Int16 mapDataLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, offset)); offset += 2; // Create the game and farm it out if (offset + mapDataLength <= data.Length && mapDataLength > 0) { byte[] mapData = new byte[mapDataLength + 1]; Array.Copy(data, offset, mapData, 0, mapDataLength); GameInstance instance = GameInstance.CreateInstance(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString), mapData); instance.GameEnded += new EventHandler(instance_GameEnded); lock (_mappings) { _mappings.Add(hash, instance); } } // We had something bad } else if (gameMessage == GameMessage.Join) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received Join: {0}", queryString)); // Create the game and farm it out GameInstance instance = GameInstance.JoinGame(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString)); lock (_mappings) { _mappings.Add(hash, instance); } } else if (gameMessage == GameMessage.List) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received List: {0}", queryString)); GameInstance.ListGames(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString)); } else if (gameMessage == GameMessage.NOP) { Trace.WriteLine(string.Format("Received NOP")); // If we get a NOP, just ignore it //GameInstance.GamePlayer.SendUpdate(GameMessage.NOP, client, endPoint, new byte[0], 1); } } }
//分发消息 void DispatchMsg(GameMessage msg) { _msgQueue.Enqueue(msg); }