Skip to content

Instantly share code, notes, and snippets.

View eahydra's full-sized avatar

Joseph eahydra

View GitHub Profile
@eahydra
eahydra / timer_pool_impl.hpp
Last active January 12, 2016 04:55
A simple wrapper with windows TimerQueue.
#ifndef TIMER_POOL_IMPL_HPP_
#define TIMER_POOL_IMPL_HPP_
#include <vector>
#include <algorithm>
#include <functional>
#include <windows.h>
#include <assert.h>
#include <mutex>
@eahydra
eahydra / coroutine_with_fsm.cpp
Created March 22, 2013 14:53
a implement of co-routine based on FSM. Or you can see it based on Duff's Device!
#define begin() \
static unsigned int state_ = 0;\
switch (state_) \
case 0:
#define _yield_impl(x_, z_) \
do { \
state_ = z_; \
##x_; \
goto exit__;\
@eahydra
eahydra / server.cpp
Last active December 15, 2015 09:19
asio/example/http/server4
#include "yield.hpp" // Enable the pseudo-keywords reenter, yield and fork.
void server::operator()(boost::system::error_code ec, std::size_t length)
{
// In this example we keep the error handling code in one place by
// hoisting it outside the coroutine. An alternative approach would be to
// check the value of ec after each yield for an asynchronous operation.
if (!ec)
{
// On reentering a coroutine, control jumps to the location of the last
@eahydra
eahydra / coroutine_executor_ex.cpp
Last active September 25, 2021 09:16
asio's truth code
void coroutine_executor_ex() {
std::cout<<__FUNCTION__<<std::endl;
coroutine_ref _coro_value = this;
switch (_coro_value)
{
case -1:
{
if (_coro_value)
{
goto terminate_coroutine;
@eahydra
eahydra / self_coroutine.cpp
Last active December 15, 2015 09:19
自己实现的基于FSM的coroutine
#define begin() \
static unsigned int state_ = 0;\
switch (state_) \
case 0:
#define _yield_impl(x_, z_) \
do { \
state_ = z_; \
##x_; \
goto exit__;\
void coroutine_executor() {
reenter(this) {
std::cout<<"before yield 1"<<std::endl;
yield task_thread_pool_.post_task([&](){
std::cout<<"yield 1"<<std::endl;
coroutine_executor();
});
std::cout<<"after yield 1"<<std::endl;
yield task_thread_pool_.post_task([&](){
@eahydra
eahydra / producer_task.hpp
Last active December 15, 2015 11:28
a producer/consumer wrapper. don't care producer async or sync. don't need use lock. but...the type of implemention is so heavy. Must associate some async module like task_pool_t.post_task(Closure). If it likes LUA's coroutine that's very fine.
#ifndef PRODUCER_TASK_HPP
#define PRODUCER_TASK_HPP
#include <deque>
#include <utility>
#include <functional>
#include <base/coroutine.hpp>
template <typename data_t, typename task_pool_t>
class producer_task {
inline std::string base64_encode(std::string src)
{
std::string tail(3, '\0');
std::vector<char> result(src.length()/3*4+6);
unsigned int one_third_len = src.length()/3;
unsigned int len_rounded_down = one_third_len*3;
unsigned int j = len_rounded_down + one_third_len;
@eahydra
eahydra / parse_rss.go
Created April 7, 2013 02:33
Let's Go! It's my first go test!
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
)
type RssItem struct {
package main
import (
"crypto/tls"
"net"
"net/http"
"time"
"fmt"
"errors"
)