public static chatPacket Deserialize(byte[] data) { chatPacket result = new chatPacket(); using (MemoryStream m = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(m)) { result.packetID = reader.ReadInt32(); result.textString = System.Text.Encoding.Default.GetString(reader.ReadBytes(Constants.BUFFER_SIZE)); } } return(result); }
void processNetPackets() { Packet incomingPacket = Net.ReadPacket(); while (incomingPacket != null) { byte[] rawBits = new byte[incomingPacket.Size]; incomingPacket.ReadBytes(rawBits); chatPacket newMessage = chatPacket.Deserialize(rawBits); printOutputLine("Chat Text: " + newMessage.textString.ToString()); printOutputLine("Received Packet from UserID: " + incomingPacket.SenderID.ToString()); printOutputLine("Received Packet ID: " + newMessage.packetID.ToString()); // Look to see if there's another packet waiting incomingPacket = Net.ReadPacket(); } }
void sendChat(string chatMessage) { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: case states.REQUEST_FIND: case states.FINDING_ROOM: case states.REQUEST_JOIN: case states.REQUEST_CREATE: case states.REQUEST_LEAVE: case states.IN_EMPTY_ROOM: printOutputLine("You need to be in a room with another player to send a message."); break; case states.IN_FULL_ROOM: { chatPacket newMessage = new chatPacket(); // Create a packet to send with the packet ID and string payload lastPacketID++; newMessage.packetID = lastPacketID; newMessage.textString = chatMessage; Oculus.Platform.Net.SendPacket(remoteUser.ID, newMessage.Serialize(), SendPolicy.Reliable); } break; default: printOutputLine("You have hit an unknown state."); break; } }