1---- Licensed to the Apache Software Foundation (ASF) under one
2-- or more contributor license agreements. See the NOTICE file
3-- distributed with this work for additional information
4-- regarding copyright ownership. The ASF licenses this file
5-- to you under the Apache License, Version 2.0 (the
6-- "License"); you may not use this file except in compliance
7-- with the License. You may obtain a copy of the License at
8--
9--   http://www.apache.org/licenses/LICENSE-2.0
10--
11-- Unless required by applicable law or agreed to in writing,
12-- software distributed under the License is distributed on an
13-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14-- KIND, either express or implied. See the License for the
15-- specific language governing permissions and limitations
16-- under the License.
17--
18
19require 'TTransport'
20require 'libluasocket'
21
22-- TSocketBase
23TSocketBase = TTransportBase:new{
24  __type = 'TSocketBase',
25  timeout = 1000,
26  host = 'localhost',
27  port = 9090,
28  handle
29}
30
31function TSocketBase:close()
32  if self.handle then
33    self.handle:destroy()
34    self.handle = nil
35  end
36end
37
38-- Returns a table with the fields host and port
39function TSocketBase:getSocketInfo()
40  if self.handle then
41    return self.handle:getsockinfo()
42  end
43  terror(TTransportException:new{errorCode = TTransportException.NOT_OPEN})
44end
45
46function TSocketBase:setTimeout(timeout)
47  if timeout and ttype(timeout) == 'number' then
48    if self.handle then
49      self.handle:settimeout(timeout)
50    end
51    self.timeout = timeout
52  end
53end
54
55-- TSocket
56TSocket = TSocketBase:new{
57  __type = 'TSocket',
58  host = 'localhost',
59  port = 9090
60}
61
62function TSocket:isOpen()
63  if self.handle then
64    return true
65  end
66  return false
67end
68
69function TSocket:open()
70  if self.handle then
71    self:close()
72  end
73
74  -- Create local handle
75  local sock, err = luasocket.create_and_connect(
76    self.host, self.port, self.timeout)
77  if err == nil then
78    self.handle = sock
79  end
80
81  if err then
82    terror(TTransportException:new{
83      message = 'Could not connect to ' .. self.host .. ':' .. self.port
84        .. ' (' .. err .. ')'
85    })
86  end
87end
88
89function TSocket:read(len)
90  local buf = self.handle:receive(self.handle, len)
91  if not buf or string.len(buf) ~= len then
92    terror(TTransportException:new{errorCode = TTransportException.UNKNOWN})
93  end
94  return buf
95end
96
97function TSocket:write(buf)
98  self.handle:send(self.handle, buf)
99end
100
101function TSocket:flush()
102end
103
104-- TServerSocket
105TServerSocket = TSocketBase:new{
106  __type = 'TServerSocket',
107  host = 'localhost',
108  port = 9090
109}
110
111function TServerSocket:listen()
112  if self.handle then
113    self:close()
114  end
115
116  local sock, err = luasocket.create(self.host, self.port)
117  if not err then
118    self.handle = sock
119  else
120    terror(err)
121  end
122  self.handle:settimeout(self.timeout)
123  self.handle:listen()
124end
125
126function TServerSocket:accept()
127  local client, err = self.handle:accept()
128  if err then
129    terror(err)
130  end
131  return TSocket:new({handle = client})
132end
133