public void queueOutgoingMessage(byte[] message_bytes) { //Figure out if this is a high or low priority message int sortMessageId = KMPCommon.intFromBytes(message_bytes, 0); switch (sortMessageId) { case (int)KMPCommon.ServerMessageID.HANDSHAKE: case (int)KMPCommon.ServerMessageID.HANDSHAKE_REFUSAL: case (int)KMPCommon.ServerMessageID.SERVER_MESSAGE: case (int)KMPCommon.ServerMessageID.TEXT_MESSAGE: case (int)KMPCommon.ServerMessageID.MOTD_MESSAGE: case (int)KMPCommon.ServerMessageID.SERVER_SETTINGS: case (int)KMPCommon.ServerMessageID.KEEPALIVE: case (int)KMPCommon.ServerMessageID.CONNECTION_END: case (int)KMPCommon.ServerMessageID.UDP_ACKNOWLEDGE: case (int)KMPCommon.ServerMessageID.PING_REPLY: queuedOutMessagesHighPriority.Enqueue(message_bytes); break; default: queuedOutMessages.Enqueue(message_bytes); break; } }
public Boolean saveScreenshot(ServerClient client) { byte[] screenshotData = getScreenshot(client); if (screenshotData == null) { return(false); } if (!Directory.Exists(SCREENSHOT_DIR)) { //Create the screenshot directory try { if (!Directory.CreateDirectory(SCREENSHOT_DIR).Exists) { return(false); } } catch (Exception) { Log.Error("Unable to save screenshot for " + client.username + "."); return(false); } } //Build the filename StringBuilder sb = new StringBuilder(); sb.Append(SCREENSHOT_DIR); sb.Append('/'); sb.Append(KMPCommon.filteredFileName(client.username)); sb.Append(' '); sb.Append(System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); sb.Append(".png"); //Write the screenshot to file String filename = sb.ToString(); if (!File.Exists(filename)) { try { //Read description length int description_length = KMPCommon.intFromBytes(screenshotData, 0); //Trim the description bytes from the image byte[] trimmed_bytes = new byte[screenshotData.Length - 4 - description_length]; Array.Copy(screenshotData, 4 + description_length, trimmed_bytes, 0, trimmed_bytes.Length); File.WriteAllBytes(filename, trimmed_bytes); } catch (Exception) { } } return(true); }
//Messages private void messageReceived(KMPCommon.ClientMessageID id, byte[] data) { if (id == KMPCommon.ClientMessageID.SPLIT_MESSAGE) { if (splitMessageReceiveIndex == 0) { //New split message int split_message_length = KMPCommon.intFromBytes(data, 4); splitMessageData = new byte[8 + split_message_length]; data.CopyTo(splitMessageData, 0); splitMessageReceiveIndex = data.Length; } else { //Continued split message data.CopyTo(splitMessageData, splitMessageReceiveIndex); splitMessageReceiveIndex = splitMessageReceiveIndex + data.Length; } //Check if we have filled the byte array, if so, handle the message. if (splitMessageReceiveIndex == splitMessageData.Length) { //Parse the message and feed it into the client queue KMPCommon.ClientMessageID joined_message_id = (KMPCommon.ClientMessageID)KMPCommon.intFromBytes(splitMessageData, 0); int joined_message_length = KMPCommon.intFromBytes(splitMessageData, 4); byte[] joined_message_data = new byte[joined_message_length]; Array.Copy(splitMessageData, 8, joined_message_data, 0, joined_message_length); byte[] joined_message_data_decompressed = KMPCommon.Decompress(joined_message_data); parent.queueClientMessage(this, joined_message_id, joined_message_data_decompressed); splitMessageReceiveIndex = 0; } } else { parent.queueClientMessage(this, id, data); } }
private void handleReceive() { while (receiveHandleIndex < receiveIndex) { //Read header bytes if (currentMessageHeaderIndex < KMPCommon.MSG_HEADER_LENGTH) { //Determine how many header bytes can be read int bytes_to_read = Math.Min(receiveIndex - receiveHandleIndex, KMPCommon.MSG_HEADER_LENGTH - currentMessageHeaderIndex); //Read header bytes Array.Copy(receiveBuffer, receiveHandleIndex, currentMessageHeader, currentMessageHeaderIndex, bytes_to_read); //Advance buffer indices currentMessageHeaderIndex += bytes_to_read; receiveHandleIndex += bytes_to_read; //Handle header if (currentMessageHeaderIndex >= KMPCommon.MSG_HEADER_LENGTH) { int id_int = KMPCommon.intFromBytes(currentMessageHeader, 0); //Make sure the message id section of the header is a valid value if (id_int >= 0 && id_int < Enum.GetValues(typeof(KMPCommon.ClientMessageID)).Length) { currentMessageID = (KMPCommon.ClientMessageID)id_int; } else { currentMessageID = KMPCommon.ClientMessageID.NULL; } int data_length = KMPCommon.intFromBytes(currentMessageHeader, 4); if (data_length > 0) { //Init message data buffer currentMessageData = new byte[data_length]; currentMessageDataIndex = 0; } else { currentMessageData = null; //Handle received message messageReceived(currentMessageID, null); //Prepare for the next header read currentMessageHeaderIndex = 0; } } } if (currentMessageData != null) { //Read data bytes if (currentMessageDataIndex < currentMessageData.Length) { //Determine how many data bytes can be read int bytes_to_read = Math.Min(receiveIndex - receiveHandleIndex, currentMessageData.Length - currentMessageDataIndex); //Read data bytes Array.Copy(receiveBuffer, receiveHandleIndex, currentMessageData, currentMessageDataIndex, bytes_to_read); //Advance buffer indices currentMessageDataIndex += bytes_to_read; receiveHandleIndex += bytes_to_read; //Handle data if (currentMessageDataIndex >= currentMessageData.Length) { //Handle received message messageReceived(currentMessageID, currentMessageData); currentMessageData = null; //Prepare for the next header read currentMessageHeaderIndex = 0; } } } } //Once all receive bytes have been handled, reset buffer indices to use the whole buffer again receiveHandleIndex = 0; receiveIndex = 0; }