基于C++11的线程池(threadpool)简洁且可以带任意多的参数
源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob’s Devlog,地址为:A Thread Pool with C++11
timer定时器,多线程,回调为阻塞模式。支持参数绑定,非常好使。
用法:
//////////////////////////////////////////////////////////////////////////
//以下是测试代码
namespace timertester
{
class timertestclass
{
public:
timertestclass(){};
~timertestclass(){};
void testfn()
{
printf("timer callback is class func.\n");
FTimerEvent mTimer(3000, &timertestclass::threadfn, this);
for (int i = 0; i < 5; i++)
{
printf("press any key to ...\n");
getchar();
mTimer.SetTimerEvent();
}
printf("stop timer \n");
mTimer.StopTimer();
}
void threadf(int i)
{
printf("test f i:%d\n", i);
}
void threadfn()
{
printf("test fn d\n");
}
void threadfn2()
{
printf("test fn2222 \n");
}
};
static void testfn1(timertestclass *cls)
{
cls->threadfn();
}
static void testpf()
{
printf("test printf \n");
printf("callback ret. \n");
}
static void prt(int& i)
{
printf("print %d\n", i);
}
static void timertest()
{
int t = 0;
FTimerEvent timerstdref(1000, prt, std::ref(t));
for (int i = 0; i < 10; i++)
{
t = i;
Sleep(1000);
}
timerstdref.StopTimer();
{
printf("timer 0 manual to set\n");
//如果定时为0,则每次为手动触发
FTimerEvent timerman(0, [](){
printf("timerman in.\n");
Sleep(5000);
printf("timerman out.\n");
});
timerman.SetTimerEvent();
timerman.SetTimerEvent();
timerman.StopTimer();
}
printf("stop timer in callback\n");
FTimerEvent timer4;
timer4.InitTimer(1000, [](FTimerEvent *pTimer){ printf("exit timer\n"); pTimer->StopTimer(); }, &timer4);
Sleep(3000);
timer4.StopTimer();
printf("set timer in callback\n");
FTimerEvent timer5;
timer5.InitTimer(2000, [](FTimerEvent *pTimer){
static bool set = false;
printf("timer in\n");
if (!set)
{
printf("set timer\n");
pTimer->SetTimerEvent();
set = true;
}
printf("timer out\n");
}, &timer5);
Sleep(10000);
timer5.StopTimer();
timertestclass test1;
test1.testfn();
int x = 0;
FTimerEvent timerref(1000, [&x]()
{
printf("x: %d \n", x);
});
for (int i = 0; i < 10; i++)
{
x = i;
Sleep(1000);
}
timerref.StopTimer();
FTimerEvent timerx(1000, [&test1]()
{
test1.threadfn2();
});
Sleep(10000);
timerx.StopTimer();
FTimerEvent timer0(1000, testpf);
Sleep(10000);
timer0.StopTimer();
FTimerEvent timer1(1000, testfn1, &test1);
Sleep(10000);
timer1.StopTimer();
FTimerEvent timer2(1000, [](){ printf("lambada no param \n"); });
Sleep(10000);
timer2.StopTimer();
int param = 0;
FTimerEvent timer3(1000, [](int *p){ printf("lambada with param: %d \n", *p); }, ¶m);
Sleep(10000);
timer3.StopTimer();
}
}基于openssl库写的aes_gcm解密方法
兼容gcm 128、192、256位,兼容变长iv算法,与python和java的gcmdecrypt方法结果保持一致。
最恶心的地方在iv长度,要手动设置,否则某些情况下会与python或者java的结果不一致(代码的24行)。
std::string AesGCMDecrypt(const std::string &strKey, const std::string &strIV, const std::string &strBuffer, int nPadding/* = 1*/)
{
std::string strRet;
if (strBuffer.empty())
return strRet;
const EVP_CIPHER *cipher;
if (strKey.length() == 16)
cipher = EVP_aes_128_gcm();
else if (strKey.length() == 24)
cipher = EVP_aes_192_gcm();
else if (strKey.length() == 32)
cipher = EVP_aes_256_gcm();
else
return strRet;
unsigned char *pszDecode = new unsigned char[strBuffer.length() + 32];
ZeroMemory(pszDecode, strBuffer.length() + 32);
unsigned char *pszOut = pszDecode;
int nDecodeLen, nTotalLen = 0;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL);
if (strIV.length() != 12)
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, strIV.length(), NULL);
EVP_DecryptInit_ex(ctx, NULL, NULL, (const unsigned char *)strKey.c_str(), (const unsigned char *)strIV.c_str());
EVP_CIPHER_CTX_set_padding(ctx, nPadding);
EVP_DecryptUpdate(ctx, pszOut, &nDecodeLen, (const unsigned char *)strBuffer.c_str(), strBuffer.length());
nTotalLen = nDecodeLen;
pszOut += nDecodeLen;
EVP_DecryptFinal(ctx, pszOut, &nDecodeLen);
nTotalLen += nDecodeLen;
strRet.assign((const char *)pszDecode, nTotalLen);
delete[] pszDecode;
EVP_CIPHER_CTX_free(ctx);
return strRet;
}在C/C++编码中,open对close,new对delete,malloc对free等等是严格要求的,否则会有各种泄露的奇葩情况。
但是实际编码中,各种异常捕捉和错误判断又让编码变得繁琐臃肿。
后来在使用golang中,defer方法使用非常方便,C++虽然没有自带的,但是可以写嘛!
借助C++类的析构方法和lambada表达式,还有std::function容器,很容易写出来一个自己的defer(以下代码是复制来的,感觉非常的干练,自己写的不好看不贴了,后来也用了这个.h)
有缘人拿去。。。RFC 3394,AESUnwrap。。。某些情况下使用的。
uint8_t AES_unwrap(uint8_t *kek, uint16_t key_len, uint8_t *cipher_text, uint16_t cipher_len, uint8_t *output)
{
uint8_t a[8], b[16];
uint8_t *r;
uint8_t *c;
uint16_t i, j, n;
AES_KEY ctx;
if (! kek || cipher_len < 16 || ! cipher_text || ! output)
{
/* We don't do anything with the return value */
return 1;
}
/* Initialize variables */
n = (cipher_len/8)-1; /* the algorithm works on 64-bits at a time */
memcpy(a, cipher_text, 8);
r = output;
c = cipher_text;
memcpy(r, c+8, cipher_len - 8);
/* Compute intermediate values */
for (j=5; j >= 0; --j)
{
r = output + (n - 1) * 8;
/* DEBUG_DUMP("r1", (r-8), 8); */
/* DEBUG_DUMP("r2", r, 8); */
for (i = n; i >= 1; --i)
{
uint16_t t = (n*j) + i;
/* DEBUG_DUMP("a", a, 8); */
memcpy(b, a, 8);
b[7] ^= t;
/* DEBUG_DUMP("a plus t", b, 8); */
memcpy(b+8, r, 8);
AES_set_decrypt_key(kek, 128, &ctx);
AES_decrypt(b, b, &ctx); /* NOTE: we are using the same src and dst buffer. It's ok. */
/* DEBUG_DUMP("aes decrypt", b, 16) */
memcpy(a,b,8);
memcpy(r, b+8, 8);
r -= 8;
}
}
/* DEBUG_DUMP("a", a, 8); */
/* DEBUG_DUMP("output", output, cipher_len - 8); */
return 0;
}