Module: TonClient::TonBinding

Extended by:
FFI::Library
Defined in:
lib/everscale-client-ruby/Binding/struct.rb,
lib/everscale-client-ruby/Binding/binding.rb,
lib/everscale-client-ruby/Binding/binding.rb

Defined Under Namespace

Classes: Response, TcStringDataT

Class Method Summary collapse

Class Method Details

.make_string(string) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 112

def self.make_string(string)
  # p 1
  result = TonBinding::TcStringDataT.new
  bytes_count = string.unpack("C*").size
  ptr1 = FFI::MemoryPointer.new(:char, bytes_count)
  ptr1.put_bytes(0, string, 0, bytes_count)
  # ptr1.autorelease = false
  # p ptr1.autorelease?
  result[:content] = ptr1
  # result[:content] = FFI::MemoryPointer.from_string(string)
  # result[:len] = string.bytesize
  result[:len] = ptr1.size
  # p 2
  result
end

.read_string(tc_string_handle) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 128

def self.read_string(tc_string_handle)
  is_ref = tc_string_handle.class == FFI::Pointer
  if is_ref
    string = tc_read_string(tc_string_handle)
  else
    string = tc_string_handle
  end

  if string[:content].address > 1
    result = string[:content].read_string(string[:len])
    if is_ref
      tc_destroy_string(tc_string_handle)
      # free(tc_string_handle)
    end
    result
  else
    nil
  end
end

.read_string_to_hash(tc_string_handle_t_ref) ⇒ Object



148
149
150
151
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 148

def self.read_string_to_hash(tc_string_handle_t_ref)
  json_string = read_string(tc_string_handle_t_ref)
  JSON.parse(json_string, {max_nesting: false}) if json_string
end

.requestLibrary(context: nil, request_id: nil, requests: nil, sm: nil, method_name: '', payload: {}, &block) ⇒ Object

block = { |response| }



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 168

def self.requestLibrary(context: nil, request_id: nil, requests: nil, sm: nil, method_name: '', payload: {}, &block)
  raise 'context not found' unless context
  raise 'method_name is empty' if method_name.empty?
  # raise "context: #{context}. request_id not is nil. Client dealloc." unless request_id
  unless request_id
    # p "context: #{context}. request_id is nil. Client deallocated."
    block.call(Response.new(request_id, "Client deallocated", -1, true)) if block
    return
  end
  method_name_string = make_string(method_name)
  payload_string = make_string(payload.to_json)

  request_id = request_id.increment
  requests[request_id] = block

  tc_request(context, method_name_string, payload_string, request_id) do |request_id, string_data, response_type, finished|
    begin
      request = requests[request_id]
      if request
          request.call(Response.new(request_id, string_data, response_type, finished))
        requests.delete(request_id) if finished
      end
    rescue => ex
      block.call(Response.new(request_id, ex.message, -1, true)) if block
    end
  end
rescue => ex
  block.call(Response.new(request_id, ex.message, -1, true)) if block
end

.send_request_sync(context: 1, method_name: '', payload: {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 153

def self.send_request_sync(context: 1, method_name: '', payload: {})
  raise 'context not found' unless context
  raise 'method_name is empty' if method_name.empty?

  method_name_string = make_string(method_name)
  payload_string = make_string(payload.to_json)

  sdk_json_response = tc_request_sync(context, method_name_string, payload_string)
  response = read_string_to_hash(sdk_json_response)
  
  return response['result'] if response['result']
  return response['error'] if response['error']
end

.setup_bindingsObject

# memory movers attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer attach_function :bcopy, [:pointer, :pointer, :size_t], :void



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/everscale-client-ruby/Binding/binding.rb', line 66

def self.setup_bindings

  # tc_string_handle_t* tc_create_context(tc_string_data_t config);
  # attach_function :tc_create_context, [TcStringDataT.by_value], TcStringHandleT.by_ref
  attach_function :tc_create_context, [TcStringDataT.by_value], :pointer

  # fn tc_destroy_context(context: InteropContext)
  attach_function :tc_destroy_context, [:uint32], :void

  # tc_string_handle_t* tc_request_sync(
  #   uint32_t context,
  #   tc_string_data_t function_name,
  #   tc_string_data_t function_params_json);
  # attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], TcStringHandleT.by_ref
  attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], :pointer

  # enum tc_response_types_t {
  #     tc_response_success = 0,
  #     tc_response_error = 1,
  #     tc_response_nop = 2,
  #     tc_response_custom = 100,
  # };
  # typedef void (*tc_response_handler_t)(
  #   uint32_t request_id,
  #   tc_string_data_t params_json,
  #   uint32_t response_type,
  #   bool finished);
  callback :tc_response_handler_t, [:uint32, TcStringDataT.by_value, :uint32, :bool], :void

  # void tc_request(
  #   uint32_t context,
  #   tc_string_data_t function_name,
  #   tc_string_data_t function_params_json,
  #   uint32_t request_id,
  #   tc_response_handler_t response_handler);
  attach_function :tc_request, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value, :uint32, :tc_response_handler_t], :void

  # tc_string_data_t tc_read_string(const tc_string_handle_t* handle);
  # attach_function :tc_read_string, [TcStringHandleT.by_ref], TcStringDataT.by_value
  attach_function :tc_read_string, [:pointer], TcStringDataT.by_value

  # void tc_destroy_string(const tc_string_handle_t* handle)
  # attach_function :tc_destroy_string, [TcStringHandleT.by_ref], :void
  attach_function :tc_destroy_string, [:pointer], :void
end