-- Auto-generated protocol definition
-- DO NOT EDIT - This file is generated by Morel
local Protocol = {}
Protocol.name = "txse_rake_tcp"
Protocol.description = "txse_rake_tcp"
-- Enum definitions
Protocol.enums = {}
Protocol.enums.LogonResponseCode = {
  [0] = "SUCCESS",
  [1] = "INCORRECT_SENDER_COMP",
  [2] = "INCORRECT_SESSION",
  [3] = "INVALID_NEXT_SEQUENCE",
  [4] = "INVALID_CONFIGURATION",
  [5] = "INCORRECT_TOKEN",
}
-- Field definitions
-- Returns a function that creates Fields table given TXSE lib
Protocol.createFields = function(TXSE)
  local Fields = {}
  local Enums = Protocol.enums
  Fields["msgType"] = {
    length = 1,
    abbr = "txse_rake_tcp.msgType",
    name = "msgType",
    ftype = ftypes.CHAR,
    encoding = ENC_ASCII,
    value_map = {
      [48] = "Debug",
      [49] = "LogonResponse",
      [50] = "TcpSequencedMessage",
      [51] = "ServerHeartbeat",
      [52] = "EndOfSession",
      [53] = "LogonRequest",
      [54] = "TcpUnsequencedMessage",
      [55] = "MemberHeartbeat",
    },
  }
  Fields["highestKnownSequenceNumber"] = {
    length = 8,
    abbr = "txse_rake_tcp.highestKnownSequenceNumber",
    name = "highestKnownSequenceNumber",
    ftype = ftypes.INT64,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "kotlin.Long",
  }
  Fields["instance"] = {
    length = 4,
    abbr = "txse_rake_tcp.instance",
    name = "instance",
    ftype = ftypes.INT32,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "kotlin.Int",
  }
  Fields["logonResponseCode"] = {
    length = 1,
    abbr = "txse_rake_tcp.logonResponseCode",
    name = "logonResponseCode",
    ftype = ftypes.INT8,
    encoding = ENC_NA,
    kotlin_type = "com.txse.foundation.protocols.rake.morel.define.LogonResponseCode",
    value_map = Enums.LogonResponseCode,
  }
  Fields["nextSequenceNumber"] = {
    length = 8,
    abbr = "txse_rake_tcp.nextSequenceNumber",
    name = "nextSequenceNumber",
    ftype = ftypes.INT64,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "kotlin.Long",
  }
  Fields["numberStreamIDs"] = {
    length = 1,
    abbr = "txse_rake_tcp.numberStreamIDs",
    name = "numberStreamIDs",
    ftype = ftypes.INT8,
    encoding = ENC_NA,
    kotlin_type = "kotlin.Byte",
  }
  Fields["senderComp"] = {
    length = 8,
    abbr = "txse_rake_tcp.senderComp",
    name = "senderComp",
    ftype = ftypes.INT64,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "com.txse.foundation.protocols.rake.morel.define.SenderComp",
  }
  Fields["session"] = {
    length = 8,
    abbr = "txse_rake_tcp.session",
    name = "session",
    ftype = ftypes.INT64,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "com.txse.avail.Session",
  }
  Fields["streamId"] = {
    length = 1,
    abbr = "txse_rake_tcp.streamId",
    name = "streamId",
    ftype = ftypes.INT8,
    encoding = ENC_NA,
    kotlin_type = "com.txse.avail.StreamId",
  }
  Fields["token"] = {
    length = 8,
    abbr = "txse_rake_tcp.token",
    name = "token",
    ftype = ftypes.INT64,
    encoding = ENC_LITTLE_ENDIAN,
    kotlin_type = "com.txse.foundation.protocols.rake.morel.define.RakeToken",
  }
  return Fields
end
-- Message definitions
Protocol.messages = {
  {
    name = "Debug",
    type = "0",
    fields = {
      "msgType",
    },
    trailing_payload = "ascii",
  },
  {
    name = "EndOfSession",
    type = "4",
    fields = {
      "msgType",
    },
  },
  {
    name = "LogonRequest",
    type = "5",
    fields = {
      "msgType",
      "session",
      "senderComp",
      "token",
      "nextSequenceNumber",
    },
  },
  {
    name = "LogonResponse",
    type = "1",
    fields = {
      "msgType",
      "session",
      "nextSequenceNumber",
      "highestKnownSequenceNumber",
      "logonResponseCode",
      "numberStreamIDs",
      "instance",
    },
  },
  {
    name = "MemberHeartbeat",
    type = "7",
    fields = {
      "msgType",
    },
  },
  {
    name = "ServerHeartbeat",
    type = "3",
    fields = {
      "msgType",
    },
  },
  {
    name = "TcpSequencedMessage",
    type = "2",
    fields = {
      "msgType",
      "streamId",
    },
    trailing_payload = "binary",
  },
  {
    name = "TcpUnsequencedMessage",
    type = "6",
    fields = {
      "msgType",
    },
    trailing_payload = "binary",
  },
}
-- Dissector setup
-- opts.payload_decoder: optional function(buffer, pinfo, tree_item) for trailing payload
Protocol.register = function(opts)
  local TXSE = require("txse-lib")
  local proto, messages = TXSE.Proto.new(TXSE.createSpec(Protocol, opts))
  function proto.dissector(buffer, pinfo, tree)
    local length = buffer:len()
    if length < 1 then
      return 0
    end
    local msg_type = buffer(0, 1):uint()
    local msg = messages.mapping[msg_type]
    if not msg then
      return 0
    end
    pinfo.cols.protocol = proto.name
    pinfo.cols.info = msg.name
    local subtree = tree:add(proto, buffer, msg.name)
    msg:parse(buffer, pinfo, subtree)
    return buffer:len()
  end
  return proto
end
return Protocol
