// Construct message object from ByteArray public Message(byte[] msg) { int offset = 0; string pstr = Encoding.ASCII.GetString(msg, offset, GameConstant.pstr.Length); offset += GameConstant.pstr.Length; if (pstr.Equals(GameConstant.pstr)) { this.pstr = pstr; Buffer.BlockCopy(msg, offset, this.reservedByte, 0, 8); offset += reservedByte.Length; int messageCode = (int)msg[offset]; offset++; switch (messageCode) { case GameConstant.handshakeCode: this.msgType = MessageType.Handshake; this.username = Encoding.ASCII.GetString(msg, offset, GameConstant.usernameSize).Trim(); break; case GameConstant.handshakeResponseCode: this.msgType = MessageType.HandshakeResponse; byte[] peerHR = new byte[GameConstant.peerInfoSize]; Buffer.BlockCopy(msg, offset, peerHR, 0, GameConstant.peerInfoSize); this.peer = new PeerInfo(peerHR); //this.peerID = BitConverter.ToInt32(msg, offset); offset += 4; //this.port = BitConverter.ToInt32(msg, offset); break; case GameConstant.keepAliveCode: this.msgType = MessageType.KeepAlive; this.peerID = BitConverter.ToInt32(msg, offset); break; case GameConstant.createCode: this.msgType = MessageType.Create; this.peerID = BitConverter.ToInt32(msg, offset); offset += GameConstant.peerIdSize; this.maxPlayer = BitConverter.ToInt32(msg, offset); offset += GameConstant.maxPlayerSize; this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.roomIDSize); this.roomID = this.roomID.Replace("\0", String.Empty).Trim(); break; case GameConstant.listCode: this.msgType = MessageType.List; this.peerID = BitConverter.ToInt32(msg, offset); break; case GameConstant.roomCode: this.msgType = MessageType.Room; this.roomCount = BitConverter.ToInt32(msg, offset); offset += GameConstant.roomCountSize; for (int i = 0; i < this.roomCount; i++) { if ((int)msg[offset] == GameConstant.beginRoomCode) { offset++; int beginRoom = offset; int roomByteLength = 0; while ((int)msg[offset] != GameConstant.endRoomCode) { roomByteLength++; offset++; } offset++; byte[] roomByte = new byte[roomByteLength]; Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength); this.roomList.Add(new Room(roomByte)); } } break; case GameConstant.successCode: this.msgType = MessageType.Success; break; case GameConstant.failedCode: this.msgType = MessageType.Failed; break; case GameConstant.joinCode: this.msgType = MessageType.Join; this.peerID = BitConverter.ToInt32(msg, offset); offset += GameConstant.peerIdSize; this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.roomIDSize); this.roomID = this.roomID.Replace("\0", String.Empty).Trim(); break; case GameConstant.startCode: this.msgType = MessageType.Start; this.peerID = BitConverter.ToInt32(msg, offset); offset += GameConstant.peerIdSize; this.turn = BitConverter.ToInt32(msg, offset); offset += 4; this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.startSize); this.roomID = this.roomID.Replace("\0", String.Empty).Trim(); break; case GameConstant.quitCode: this.msgType = MessageType.Quit; this.peerID = BitConverter.ToInt32(msg, offset); break; case GameConstant.checkCode: this.msgType = MessageType.Check; this.peerIDJoinRoom = BitConverter.ToInt32(msg, offset); break; case GameConstant.checkResponseCode: this.msgType = MessageType.CheckResponse; this.peerIDJoinRoom = BitConverter.ToInt32(msg, offset); offset += GameConstant.peerIdSize; this.isJoinAccepted = BitConverter.ToBoolean(msg, offset); offset += 1; this.port = BitConverter.ToInt32(msg, offset); break; case GameConstant.handshakeCreatorCode: this.msgType = MessageType.HandshakeCreator; byte[] peer = new byte[GameConstant.peerInfoSize]; Buffer.BlockCopy(msg, offset, peer, 0, GameConstant.peerInfoSize); this.peer = new PeerInfo(peer); break; case GameConstant.playerCode: this.msgType = MessageType.Player; int players = BitConverter.ToInt32(msg, offset); offset += 4; for (int i = 0; i < players; i++) { byte[] peerByte = new byte[GameConstant.peerInfoSize]; Buffer.BlockCopy(msg, offset, peerByte, 0, GameConstant.peerInfoSize); offset += GameConstant.peerInfoSize; this.player.Add(new PeerInfo(peerByte)); } break; case GameConstant.joinSuccessCode: this.msgType = MessageType.JoinSuccess; if ((int)msg[offset] == GameConstant.beginRoomCode) { offset++; int beginRoom = offset; int roomByteLength = 0; while ((int)msg[offset] != GameConstant.endRoomCode) { roomByteLength++; offset++; } offset++; byte[] roomByte = new byte[roomByteLength]; Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength); this.room = new Room(roomByte); } break; case GameConstant.creatorQuitCode: this.msgType = MessageType.CreatorQuit; byte[] creatorPeer = new byte[GameConstant.peerInfoSize]; Buffer.BlockCopy(msg, offset, creatorPeer, 0, GameConstant.peerInfoSize); this.peer = new PeerInfo(creatorPeer); break; case GameConstant.RoomInfoCode: this.msgType = MessageType.RoomInfo; if ((int)msg[offset] == GameConstant.beginRoomCode) { offset++; int beginRoom = offset; int roomByteLength = 0; while ((int)msg[offset] != GameConstant.endRoomCode) { roomByteLength++; offset++; } offset++; byte[] roomByte = new byte[roomByteLength]; Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength); this.room = new Room(roomByte); } break; case GameConstant.SelfPromoteCreatorCode: this.msgType = MessageType.SelfPromoteCreator; byte[] promote = new byte[GameConstant.peerInfoSize]; Buffer.BlockCopy(msg, offset, promote, 0, GameConstant.peerInfoSize); offset += GameConstant.peerInfoSize; this.peer = new PeerInfo(promote); Buffer.BlockCopy(msg, offset, promote, 0, GameConstant.peerInfoSize); this.peer2 = new PeerInfo(promote); break; case GameConstant.GameUpdate: this.msgType = MessageType.GameUpdate; this.timestamp = BitConverter.ToInt64(msg, offset); offset += GameConstant.timestampSize; byte[] gameUpdate = new byte[GameConstant.gameUpdateSize]; Buffer.BlockCopy(msg, offset, gameUpdate, 0, GameConstant.gameUpdateSize); this.gameUpdate = new GameUpdate(gameUpdate); break; } } }
// Construct message for GameUpdate message private byte[] constructMessage(MessageType msgType, long timestamp, GameUpdate gameUpdate) { List<byte> msg = new List<byte>(); msg.AddRange(GameConstant.pstrByte); msg.AddRange(GameConstant.reservedByte); msg.Add((byte)GameConstant.GameUpdate); msg.AddRange(BitConverter.GetBytes(timestamp)); msg.AddRange(gameUpdate.toByte()); return msg.ToArray(); }
// Constructor for GameUpdate message public Message(MessageType msgType, long timestamp, GameUpdate gameUpdate) { this.msgType = msgType; this.timestamp = timestamp; this.gameUpdate = gameUpdate; }