File: cosocket.lua

package info (click to toggle)
lua-copas 4.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 700 kB
  • sloc: makefile: 62; sh: 43
file content (44 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-------------------------------------------------------------------------------
-- Copas - Coroutine Oriented Portable Asynchronous Services
--
-- Copas Wrapper for socket.http module
--
-- Written by Leonardo Godinho da Cunha
-------------------------------------------------------------------------------
local copas = require("copas")
local socket = require("socket")

local cosocket = {}

-- Meta information is public even begining with an "_"
cosocket._COPYRIGHT   = "Copyright (C) 2004-2006 Kepler Project"
cosocket._DESCRIPTION = "Coroutine Oriented Portable Asynchronous Services Wrapper for socket module"
cosocket._NAME        = "Copas.cosocket"
cosocket._VERSION     = "0.1"

function cosocket.tcp ()
	local skt = socket.tcp()
	local w_skt_mt = { __index = skt }
	local ret_skt = setmetatable ({ socket = skt }, w_skt_mt)
	ret_skt.settimeout = function (self,val)
				return self.socket:settimeout (val)
      end
	ret_skt.connect = function (self,host, port)
				local ret,err = copas.connect (self.socket,host, port)
				local d = copas.wrap(self.socket)

				self.send= function(client, data)
					local ret,val=d.send(client, data)
					return ret,val
				end
        self.receive=d.receive
        self.close = function (w_socket)
          ret=w_socket.socket:close()
          return ret
        end
				return ret,err
			end
	return  ret_skt
end

return cosocket