Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
kevinlynx / dummy_httpd.py
Created March 3, 2015 06:49
create a dummy tcp/http server in python to accept connections only, which can be used in unittest, to mock a tcp/http server
# dummy_httpd.py
# based on python 2.7
import BaseHTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import time
class ThreadedHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
daemon_threads = True
@kevinlynx
kevinlynx / scala-jna.scala
Created October 10, 2013 13:10
JNA used in scala
package test
import com.sun.jna._
import com.sun.jna.ptr._
trait Kernel32 extends Library {
def GetDiskFreeSpaceA(lpRootPathName: String,
lpSectorsPerCluster: IntByReference, lpBytesPerSector: IntByReference,
lpNumberOfFreeClusters: IntByReference, lpTotalNumberOfClusters: IntByReference): Boolean
def GetComputerNameW(outName: Array[Char], size: IntByReference): Boolean
@kevinlynx
kevinlynx / thread_id_pool.cpp
Last active February 16, 2018 06:33
simple read/write thread memory gc
#include "thread_id_pool.h"
#include <pthread.h>
#include <assert.h>
static pthread_key_t s_tkey;
static pthread_once_t s_tkey_once;
static void freeKey(void *p)
{
ThreadIdPool::TVal *val = (ThreadIdPool::TVal*) p;
@kevinlynx
kevinlynx / poster.rb
Last active September 12, 2017 13:59
#!/usr/bin/env ruby
#
# I write this script to post my personl blog codemacro.com posts to my cppblog.com/kevinlynx
# Kevin Lynx 8.7.2012
#
require 'nokogiri'
require 'open-uri'
require 'xmlrpc/client'
require 'yaml'
#include <assert.h>
#include <vector>
#include <map>
class DecisionStmt {
public:
enum { DECISION_RET = (unsigned) -1 };
typedef std::map<unsigned, int> CondResult;
typedef std::vector<CondResult> AccResults;
typedef std::vector<unsigned> CondList;
@kevinlynx
kevinlynx / request-metainfo.scala
Created October 25, 2013 14:28
the second version to request torrent metainfo by protocol bep09/10
/*
* http://www.bittorrent.org/beps/bep_0010.html
* http://www.bittorrent.org/beps/bep_0009.html
*/
package bep.impl
import java.io._
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.buffer._
@kevinlynx
kevinlynx / bt-metainfo.scala
Created October 20, 2013 13:41
request torrent meta info from peer based on netty and scala
/*
* http://www.bittorrent.org/beps/bep_0010.html
* http://www.bittorrent.org/beps/bep_0009.html
*/
package netty.sample
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.buffer.ByteBuf
import io.netty.bootstrap.Bootstrap
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class A
{
};
class B
// e.g: copyDir("folder\\", "destFolder\\", false);
bool copyDir(const char *r_szSrcPath, const char *r_szDesPath, bool recur) {
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[MAX_PATH];
strcpy(l_szTmp, r_szSrcPath);
char l_szSrcPath[MAX_PATH];
char l_szDesPath[MAX_PATH];
strcpy(l_szSrcPath, r_szSrcPath);
@kevinlynx
kevinlynx / netty-timeclient.scala
Created October 13, 2013 14:40
netty time server/client sample in scala
package netty.sample
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.buffer.ByteBuf
import io.netty.bootstrap.Bootstrap
import io.netty.channel._
import io.netty.channel.nio._
import io.netty.channel.socket._
import io.netty.channel.socket.nio._