카사블랑카 cloud-based client-server communication in native C++

MS에서 흥미로운 SDK를 내놨네요.

http://msdn.microsoft.com/en-us/devlabs/casablanca

Casablanca is a Microsoft incubation effort to support cloud-based client-server communication in native code using a modern asynchronous C++ API design.

  • Support for accessing REST services from native code on Windows Vista, Windows 7, and Windows 8 Consumer Preview by providing asynchronous C++ bindings to HTTP, JSON, and URIs
  • A Visual Studio extension SDK to help you write C++ HTTP client side code in your Windows 8 Metro style app
  • Support for writing native-code REST for Azure, including Visual Studio integration
  • Convenient libraries for accessing Azure blob and queue storage from native clients as a first class Platform-as-a-Service (PaaS) feature
  • A consistent and powerful model for composing asynchronous operations based on C++ 11 features
  • A C++ implementation of the Erlang actor-based programming model
  • A set of samples and documentation
샘플코드를 보니 http client나 lisener를 쉽게 구현할 수 있더군요. 

    g_listener = http_listener::create(uri.to_uri(),
        // GET
        [](http_request message)
        {
            message.reply(http::status_codes::OK, "Hello World!");
            log::post(actors::LOG_INFO, "Serviced a GET request for " + message.request_uri().to_string());
        },
        // PUT
        [](http_request message)
        {
            message.reply(http::status_codes::OK, "Hello World!");
            log::post(actors::LOG_INFO, "Serviced a PUT request " + message.request_uri().to_string());
        },
        // POST
        [](http_request message)
        {
            message.reply(http::status_codes::OK, "Hello World!");
            log::post(actors::LOG_INFO, "Serviced a POST request " + message.request_uri().to_string());
        },
        // DELETE
        [](http_request message)
        {
            message.reply(http::status_codes::OK, "Hello World!");
            log::post(actors::LOG_INFO, "Serviced a DELETE request " + message.request_uri().to_string());
        });

    g_listener.open();

이런식으로 마치 웹서버 처럼 특정 URI의 http request에 대응하는 lambda함수를 등록시켜서 사용할 수도 있구요. ^^

http_client bing( L”http://www.bing.com/search” );
bing.request( methods::GET, L”?q=S.Somasegar” )
.then( []( http_response response ) {
cout << “HTML SOURCE:” << endl << response.to_string() << endl; })
.wait();

이런식으로 client도 C++11스타일로 간단하게 구현이 가능하더군요.

잘만 된다면 게임서버가 마치 웹서버처럼 동작하면서 브라우저와 통신이 가능하게 만들수 있을거 같더라구요.

공유하기 버튼

 

snap shot

A를 before , B를 After data의 vector라고 했을때 

A-B 는 지워야 할 데이터.
B-A 는 추가해야 할 데이터
A교집합B는 체크해야할 데이터.

key값을 vector로 가지고 있다면.

template <class T1, class T2 , class R >
void mak_snap_shot(T1 before_begin, T1 before_end, T2 after_begin, T2 after_end, R& insert_vec ,R& delete_vec , R& check_vec  )
{
std::set_difference(before_begin , before_end , after_begin , after_end , back_inserter(delete_vec));
std::set_difference(after_begin , after_end , before_begin , before_end , back_inserter(invert_vec));
std::set_intersection( before_begin , before_end , after_begin , after_end ,back_inserter(check_vec));
return;
}

코드로 짜면 이런 식. before , after 벡터는 sort되어 있어야 함. 

사용 방법. 

mak_snap_shot( old_isns.begin() , old_isns.end() , cur_isns.begin() , cur_isns.end() ,insert_isns , del_isns , check_isns );

공유하기 버튼

 

if문에 {}는 왠만하면 쓰자.

if ( condition.name_part.GetLength() > 0 ) // 이름 조건이 있을때 
if ( pinfo->name.Find( condition.name_part.GetString() ) != -1 ) // 이름이 안맞다면 넣지 않는다.
search_itemcodes.push_back( pinfo->itemcode );
else
search_itemcodes.push_back( pinfo->itemcode );

-_-;;


의도한건

if ( condition.name_part.GetLength() > 0 ) // 이름 조건이 있을때 
{
if ( pinfo->name.Find( condition.name_part.GetString() ) != -1 ) // 이름이 안맞다면 넣지 않는다.
{
search_itemcodes.push_back( pinfo->itemcode );
}
}
else
{
search_itemcodes.push_back( pinfo->itemcode );
}

이건데..


if ( condition.name_part.GetLength() > 0 ) // 이름 조건이 있을때 
{
if ( pinfo->name.Find( condition.name_part.GetString() ) != -1 ) // 이름이 안맞다면 넣지 않는다.
{
search_itemcodes.push_back( pinfo->itemcode );
}else
{
search_itemcodes.push_back( pinfo->itemcode );
}
}

이렇게 되버렸다 -_-;

공유하기 버튼

 

shared_ptr의 장점. 프로그래밍 코드&지식모음

아래와 같은 코드가 있다고 했을때.

auto pitem = inven.GetItemByISN( isn );
if ( pitem != nullptr )
{
    inven.DeleteItemByISN( isn ); // pitem에 해당하는 아이템을 인벤에서 삭제.
    ItemFlowLog(isn, pitem->itemcode, ItemFlowActType_ChangeAttribute , ItemFlowActDetail_ChangeByRegAuction,1);
}

아이템을 경매에 붙이고 인벤토리에서는 삭제하는 코드인데 마지막줄의 ItemFlowLog라는건 나중에 붙었다고 가정해 보자.
그냥 포인터로 받았다면 DeleteItemByISN에 의해서 pitem은 삭제되버린 아이템이 되 버린다. 
따라서 ItemFlowLog에서 사용한 pitem->itemcode는 쓰레기값이 되버린다. 

하지만 inven 내부에서도 shared_ptr로 item을 관리하고 pitem을 shared_ptr로 받았다면 이 함수를 벗어나기 전까지는 
pitem객체는 남아있고 안전하게 동작하는 코드가 된다. 
프로그래머가 해당 객체가 스코프내에서 삭제를 신경쓸 필요가 없으므로 shared_ptr은 좀더 유연한 코딩이 가능하다.

예를들면 60프레임마다 돌면서 매번 생성/삭제를 반복하는 객체가 아니라면(그런 코드가 있어도 좀 문제일듯) 
shared_ptr은 꼭써라! 두번써라! 

자주 불러지는 부분에서 소유권 변경이나 생성/삭제에 관여하지 않는 코드라면 성능을 위해 그냥 * 로 받아서 써도 됨

공유하기 버튼

 

map의 배열을 만들면 안된다고! 프로그래밍 코드&지식모음

map<int , int>  aa[10];

[] 연산자를 재정의한 컨테이너의 배열을 만들면 안된다고!! 

가끔 똑같은 실수를 반복하네 -_-;

공유하기 버튼

 

vector의 값이 중복되어 있는지 검사하는 코드 프로그래밍 코드&지식모음

template <class T>
bool CheckDuplacation(std::vector<T>& check_vec  )
{
std::sort( check_vec.begin() , check_vec.end() );
auto pos = std::unique( check_vec.begin(), check_vec.end() );
if ( pos == check_vec.end()  ) return true;
return false;
}

중복되지 않으면 true
중복된 값이 있으면 false

만들고 보니 반환값이 애매하네.-_-;

공유하기 버튼

 

1 2 3 4 5 6 7 8 9 10 다음