Screenshot.setFromByteArray C# (CSharp) Method

setFromByteArray() public method

public setFromByteArray ( byte bytes, bool meta_only = false ) : void
bytes byte
meta_only bool
return void
    public void setFromByteArray(byte[] bytes, bool meta_only = false)
    {
        UnicodeEncoding encoding = new UnicodeEncoding();

        int array_index = 0;
        index = KLFCommon.intFromBytes(bytes, array_index);
        array_index += 4;

        int string_size = KLFCommon.intFromBytes(bytes, array_index);
        array_index += 4;

        player = encoding.GetString(bytes, array_index, string_size);
        array_index += string_size;

        string_size = KLFCommon.intFromBytes(bytes, array_index);
        array_index += 4;

        description = encoding.GetString(bytes, array_index, string_size);
        array_index += string_size;

        image = new byte[bytes.Length-array_index];
        Array.Copy(bytes, array_index, image, 0, image.Length);
    }

Usage Example

Example #1
0
    protected void handleMessage(KLFCommon.ServerMessageID id, byte[] data)
    {
        switch (id)
        {
            case KLFCommon.ServerMessageID.HANDSHAKE:

                Int32 protocol_version = KLFCommon.intFromBytes(data);

                if (data.Length >= 8)
                {
                    Int32 server_version_length = KLFCommon.intFromBytes(data, 4);

                    if (data.Length >= 12 + server_version_length)
                    {
                        String server_version = encoder.GetString(data, 8, server_version_length);
                        clientID = KLFCommon.intFromBytes(data, 8 + server_version_length);

                        Console.WriteLine("Handshake received. Server is running version: " + server_version);
                    }
                }

                //End the session if the protocol versions don't match
                if (protocol_version != KLFCommon.NET_PROTOCOL_VERSION)
                {
                    Console.WriteLine("Server version is incompatible with client version.");
                    endSession = true;
                    intentionalConnectionEnd = true;
                }
                else
                {
                    sendHandshakeMessage(); //Reply to the handshake
                    lock (udpTimestampLock)
                    {
                        lastUDPMessageSendTime = stopwatch.ElapsedMilliseconds;
                    }
                    handshakeCompleted = true;
                }

                break;

            case KLFCommon.ServerMessageID.HANDSHAKE_REFUSAL:

                String refusal_message = encoder.GetString(data, 0, data.Length);

                endSession = true;
                intentionalConnectionEnd = true;

                enqueuePluginChatMessage("Server refused connection. Reason: " + refusal_message, true);

                break;

            case KLFCommon.ServerMessageID.SERVER_MESSAGE:
            case KLFCommon.ServerMessageID.TEXT_MESSAGE:

                if (data != null)
                {

                    InTextMessage in_message = new InTextMessage();

                    in_message.fromServer = (id == KLFCommon.ServerMessageID.SERVER_MESSAGE);
                    in_message.message = encoder.GetString(data, 0, data.Length);

                    //Queue the message
                    enqueueTextMessage(in_message);
                }

                break;

            case KLFCommon.ServerMessageID.PLUGIN_UPDATE:

            if (data != null)
                    sendClientInteropMessage(KLFCommon.ClientInteropMessageID.PLUGIN_UPDATE, data);

                break;

            case KLFCommon.ServerMessageID.SERVER_SETTINGS:

                lock (serverSettingsLock)
                {
                    if (data != null && data.Length >= KLFCommon.SERVER_SETTINGS_LENGTH && handshakeCompleted)
                    {

                        updateInterval = KLFCommon.intFromBytes(data, 0);
                        screenshotInterval = KLFCommon.intFromBytes(data, 4);

                        lock (clientDataLock)
                        {
                            int new_screenshot_height = KLFCommon.intFromBytes(data, 8);
                            if (screenshotSettings.maxHeight != new_screenshot_height)
                            {
                                screenshotSettings.maxHeight = new_screenshot_height;
                                lastClientDataChangeTime = stopwatch.ElapsedMilliseconds;
                                enqueueTextMessage("Screenshot Height has been set to " + screenshotSettings.maxHeight);
                            }

                            if (inactiveShipsPerUpdate != data[12])
                            {
                                inactiveShipsPerUpdate = data[12];
                                lastClientDataChangeTime = stopwatch.ElapsedMilliseconds;
                            }
                        }
                    }
                }

                break;

            case KLFCommon.ServerMessageID.SCREENSHOT_SHARE:
                if (data != null && data.Length > 0 && data.Length < screenshotSettings.maxNumBytes
                    && watchPlayerName.Length > 0)
                {
                    //Cache the screenshot
                    Screenshot screenshot = new Screenshot();
                    screenshot.setFromByteArray(data);
                    cacheScreenshot(screenshot);

                    //Send the screenshot to the client
                    sendClientInteropMessage(KLFCommon.ClientInteropMessageID.SCREENSHOT_RECEIVE, data);
                }
                break;

            case KLFCommon.ServerMessageID.CONNECTION_END:

                if (data != null)
                {
                    String message = encoder.GetString(data, 0, data.Length);

                    endSession = true;

                    //If the reason is not a timeout, connection end is intentional
                    intentionalConnectionEnd = message.ToLower() != "timeout";

                    enqueuePluginChatMessage("Server closed the connection: " + message, true);
                }

                break;

            case KLFCommon.ServerMessageID.UDP_ACKNOWLEDGE:
                lock (udpTimestampLock)
                {
                    lastUDPAckReceiveTime = stopwatch.ElapsedMilliseconds;
                }
                break;

            case KLFCommon.ServerMessageID.CRAFT_FILE:

                if (data != null && data.Length > 4)
                {
                    //Read craft name length
                    byte craft_type = data[0];
                    int craft_name_length = KLFCommon.intFromBytes(data, 1);
                    if (craft_name_length < data.Length - 5)
                    {
                        //Read craft name
                        String craft_name = encoder.GetString(data, 5, craft_name_length);

                        //Read craft bytes
                        byte[] craft_bytes = new byte[data.Length - craft_name_length - 5];
                        Array.Copy(data, 5 + craft_name_length, craft_bytes, 0, craft_bytes.Length);

                        //Write the craft to a file
                        String filename = getCraftFilename(craft_name, craft_type);
                        if (filename != null)
                        {
                            try
                            {
                                File.WriteAllBytes(filename, craft_bytes);
                                enqueueTextMessage("Received craft file: " + craft_name);
                            }
                            catch
                            {
                                enqueueTextMessage("Error saving received craft file: " + craft_name);
                            }
                        }
                        else
                            enqueueTextMessage("Unable to save received craft file.");
                    }
                }

                break;

            case KLFCommon.ServerMessageID.PING_REPLY:
                if (pingStopwatch.IsRunning)
                {
                    enqueueTextMessage("Ping Reply: " + pingStopwatch.ElapsedMilliseconds + "ms");
                    pingStopwatch.Stop();
                    pingStopwatch.Reset();
                }
                break;
        }
    }
All Usage Examples Of Screenshot::setFromByteArray