Giter Site home page Giter Site logo

chuck's Introduction

#chuck

  • first, Chuck is my son's name.

  • second, Chuck is a high perference,asynchronous and easily use C/Lua network library under Linux/MacOS.

#build

download and make lua 5.3

static library for c:

make libchuck

dynamic library for lua:

make chuck.so

#examples

##httpserver.lua

package.path = './lib/?.lua;'
package.cpath = './lib/?.so;'

local chuck = require("chuck")
local http = require("http")
local event_loop = chuck.event_loop.New()



local ret = http.easyServer(function (request,response)
	response:SetHeader("Content-Type","text/plain")
	response:SetHeader("A","a")
	response:SetHeader("B","b")
	response:AppendBody("hello everyone")
	response:Finish("200","OK")
end):Listen(event_loop,"0.0.0.0",8010)

if "OK" == ret then
	local timer1 = event_loop:AddTimer(1000,function ()
		collectgarbage("collect")
	end)
	event_loop:Run()
end

##echo.lua

package.path = './lib/?.lua;'
package.cpath = './lib/?.so;'
local chuck = require("chuck")
local socket = chuck.socket

local event_loop = chuck.event_loop.New()

local server = socket.stream.ip4.listen(event_loop,"127.0.0.1",8010,function (fd)
	local conn = socket.stream.New(fd,4096)
	if conn then
		conn:Bind(event_loop,function (data)
			if data then 
				print(data:Content())
				local response = data:Clone()
				response:AppendStr("hello world\r\n")
				conn:Send(response)
			else
				print("client disconnected") 
				conn:Close() 
			end
		end)
	end
end)

if server then
	event_loop:Run()
end

##broadcast_svr.lua package.path = './lib/?.lua;' package.cpath = './lib/?.so;' local chuck = require("chuck") local socket = chuck.socket local packet = chuck.packet

local event_loop = chuck.event_loop.New()

local clients = {}
local client_count = 0
local packet_count = 0

local server = socket.stream.ip4.listen(event_loop,"127.0.0.1",8010,function (fd)
	local conn = socket.stream.New(fd,65536,packet.Decoder())
	if conn then
		clients[fd] = conn
		client_count = client_count + 1
		conn:Bind(event_loop,function (data)
			if data then 
				for k,v in pairs(clients) do
					packet_count = packet_count + 1
					v:Send(data)
				end
			else
				client_count = client_count - 1
				print("client disconnected") 
				conn:Close()
				clients[fd] = nil 
			end
		end)
	end
end)

local timer1 = event_loop:RegTimer(1000,function ()
	collectgarbage("collect")
	print(client_count,packet_count)
	packet_count = 0
end)

if server then
	event_loop:Run()
end

##broadcast_cli.lua

package.path = './lib/?.lua;'
package.cpath = './lib/?.so;'
local chuck = require("chuck")
local socket = chuck.socket
local packet = chuck.packet

local event_loop = chuck.event_loop.New()

local connections = {}
local packet_count = 0

for i=1,500 do
	socket.stream.ip4.dail(event_loop,"127.0.0.1",8010,function (fd)
		local conn = socket.stream.New(fd,65536,packet.Decoder())
		if conn then
		connections[fd] = conn
		conn:Bind(event_loop,function (data)
				if data then 
					packet_count = packet_count + 1
				else
					print("client disconnected") 
					conn:Close()
					connections[fd] = nil 
				end
			end)
		end
	end)
end

local timer1 = event_loop:RegTimer(1000,function ()
	print(packet_count)
	collectgarbage("collect")
	packet_count = 0
end)

local timer2 = event_loop:RegTimer(300,function ()
	for k,v in pairs(connections) do
		local buff = chuck.buffer.New()
		local w = packet.Writer(buff)
		w:WriteStr("hello")
		v:Send(buff)
	end
end)

event_loop:Run()

#customer

chuck's People

Contributors

sniperhw avatar tiancaiamao avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.