This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- main.lua | |
local cell = require "cell" | |
local function accepter(fd, addr, listen_fd) | |
-- can't read fd in this function, because socket.cell haven't forward data from fd | |
local client = cell.cmd("launch", "test.client",fd, addr) | |
-- return cell the data from fd will forward to, you can also return nil for forwarding to self | |
return client | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Encode: | |
[.c.] = Empty; | |
[.c.]k = First Character in CharStream; | |
while ([.c.]k != EOF ) | |
{ | |
if ( [.c.]k is in the StringTable) | |
{ | |
[.c.] = [.c.]k; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdlib.h> | |
// assert(RAND_MAX >= 0x7fff) | |
float | |
random_0_to_1() { | |
union { | |
uint32_t d; | |
float f; | |
} u; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The biggest 64bit prime | |
#define P 0xffffffffffffffc5ull | |
#define G 5 | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
// calc a * b % p , avoid 64bit overflow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int | |
binary_search_first_position(int *A, int n, int target) { | |
int end[2] = { -1, n }; | |
while (end[0] + 1 < end[1]) { | |
int mid = (end[0] + end[1]) / 2; | |
int sign = (unsigned)(A[mid] - target) >> 31; | |
end[1-sign] = mid; | |
} | |
int high = end[1]; | |
if (high >= n || A[high] != target) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ config | |
root = "./" | |
listen = "127.0.0.1:8786" | |
redisaddr = "127.0.0.1:6379[1]" | |
dbfile = root .. "backup.db" | |
thread = 4 | |
logger = nil | |
harbor = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use clonefunction | |
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} | |
#define UNLOCK(q) __sync_lock_release(&(q)->lock); | |
struct codecache { | |
int lock; | |
lua_State *L; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local skynet = require "skynet" | |
local socket = require "socket" | |
local mode = ... | |
if mode == "agent" then | |
skynet.start(function() | |
skynet.dispatch("lua", function(_,_,id) | |
socket.start(id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local skynet = require "skynet" | |
local mongo = require "mongo" | |
skynet.start(function() | |
local db = mongo.client { host = "127.0.0.1" , username = "user", password = "pass" } | |
local r = db:runCommand "listDatabases" | |
for k,v in ipairs(r.databases) do | |
print(v.name) | |
end |
OlderNewer