本文整理汇总了C++中Package::ParseFromArray方法的典型用法代码示例。如果您正苦于以下问题:C++ Package::ParseFromArray方法的具体用法?C++ Package::ParseFromArray怎么用?C++ Package::ParseFromArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Package
的用法示例。
在下文中一共展示了Package::ParseFromArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dataService
void dataService(int client_sock, void* buff, sockaddr_in fromAddr,
NoVoHT* pmap) {
// cout << strlen((char*)buff) << "{" << ((char*)buff) << "}" << endl;
//cout<<"dataService: from port "<< fromAddr.sin_port<<endl;
srand(getpid() + clock());
//srand(kyotocabinet::getpid() + clock());
//cout << "Current service thread ID = " << pthread_self()<< ", dbService() begin..." << endl;
// char buff[Env::MAX_MSG_SIZE];
int32_t operation_status = -99; //
// sockaddr_in toAddr;
int r;
void* buff1;
Package package;
package.ParseFromArray(buff, Env::MAX_MSG_SIZE);
string result;
// cout << endl << endl << "in dbService: received replicano = "<< package.replicano() << endl;
// cout << "Server got package size: " << package.ByteSize() << endl;
// cout <<"Package content: "<< (char*)buff<<endl;
switch (package.operation()) {
case 1: //lookup
{
// cout << "Lookup..." << endl;
if (package.virtualpath().empty()) {
// cerr << "Bad key: nothing to find" << endl;
operation_status = -1;
} else {
//result = HB_lookup(db, package);
// cout << "Lookup...2" << endl;
//cout<<"Will lookup key: "<< package.virtualpath()<<endl;
// result = HB_lookup(hmap, package);
result = HB_lookup(pmap, package);
// cout << "Lookup...3" << endl;
//don't really send result back to client now, do it latter.
if (result.compare("Empty") == 0) {
operation_status = -2;
} else {
operation_status = 0;
}
}
/*
* pack the status and lookup-result into one string
*/
buff1 = &operation_status;
char statusBuff[3];
sprintf(statusBuff, "%03d", operation_status);
string sAllInOne;
sAllInOne.append(statusBuff);
sAllInOne.append(result);
generalSendBack(client_sock, sAllInOne.c_str(), sAllInOne.size(),
fromAddr, 0, TCP);
}
break;
case 4: {
if (package.virtualpath().empty()) {
operation_status = -1;
} else {
// cout << "Server: append..." << endl;
//operation_status = HB_insert(db, package);
operation_status = HB_append(pmap, package);
// cout << "Server: append ret = "<< operation_status <<endl;
//cout<<"Inserted: key: "<< package.virtualpath()<<endl;
// cout << "insert finished, return: " << operation_status << endl;
}
buff1 = &operation_status;
if (TCP == true) {
r = send(client_sock, &operation_status, sizeof(int32_t), 0);
} else {
r = sendto(client_sock, &operation_status, sizeof(int32_t), 0,
(struct sockaddr *) &fromAddr, sizeof(struct sockaddr));
}
if (r <= 0) {
cout << "Append: Server could not send acknowledgement to client: sendto r = " << r << endl;
}
}
break;
case 2: {
//remove
// cout << "Remove..." << endl;
//cout << "Package:key "<<package.virtualpath()<<endl;
if (package.virtualpath().empty()) {
cerr << "Bad key: nothing to remove" << endl;
operation_status = -1;
} else {
//operation_status = HB_remove(db, package);
// operation_status = HB_remove(hmap, package);
//.........这里部分代码省略.........
示例2: clock
//raman-replication-e
void *dbService(void *threadarg) {
srand(kyotocabinet::getpid() + clock());
//cout << "Current service thread ID = " << pthread_self()<< ", dbService() begin..." << endl;
char buff[MAX_MSG_SIZE];
int32_t operation_status;
sockaddr_in toAddr;
int client_sock, r;
void* buff1 = (void*) malloc(sizeof(int32_t));
#if TRANS_PROTOCOL ==USE_TCP
client_sock = ((struct threaddata *) threadarg)->socket;
#elif TRANS_PROTOCOL == USE_UDP
toAddr = ((struct threaddata *) threadarg)->sockinfo;
strcpy( buff, ((struct threaddata *) threadarg)->buffer ); //here the buffer(it seems a global variable) should be refreshed so clean it.
#endif
//raman-s
// if (size > 0) { //if size < 0, consider it as a "quit command"
//raman-e
cout << "Thread: receive request from client..." << endl;
#if TRANS_PROTOCOL == USE_TCP
r = d3_svr_recv(client_sock, buff, MAX_MSG_SIZE * sizeof(char), 0, &toAddr);
if (r <= 0) {
cout << "Server could not recv data" << endl;
}
#endif
Package package;
package.ParseFromArray(buff, MAX_MSG_SIZE);
string result;
cout << endl << endl << "in dbService: received replicano = "
<< package.replicano() << endl;
cout << "package size: " << package.ByteSize() << endl;
// cout <<"Package content: "<< buff<<endl;
switch (package.operation()) {
case 3: //insert
cout << "Insert..."<< endl;
operation_status = HB_insert(db, package);
break;
case 1: //lookup
cout << "Lookup..."<< endl;
if (package.virtualpath().empty()) {
cerr << "Bad key: nothing to find" << endl;
operation_status = -1;
} else {
result = HB_lookup(db, package);
//don't really send result back to client now, do it latter.
if (result.empty()) {
operation_status = -2;
buff1 = &operation_status;
} else { //find result, send back here
cout << "Lookup: Result found, sending back..." << endl;
//=====================================
//how to know where to send back? Use the one that created when receive request as above.
//d3_send_data(client_sock, (void*)result.c_str(),result.length(), 0, &toAddr);
//just a try, not use simpleSend, because no need to make a new socket and connection..
if (TRANS_PROTOCOL == USE_TCP) {
d3_send_data(client_sock, (void*) result.c_str(), result.length(), 0, &toAddr); //TCP reuse the socket and connection.
} else if (TRANS_PROTOCOL == USE_UDP) {
HostEntity dest;
char host[INET_ADDRSTRLEN];
cout << "before inet_ntop" << endl;
inet_ntop(AF_INET, &(toAddr.sin_addr), host, INET_ADDRSTRLEN); //get a hostname from toAddr
cout << "before inet_ntop" << endl;
string hostName = string(host);
cout << "string made" << endl;
dest.host = hostName;
dest.port = toAddr.sin_port; //this is arandom port used by client just once, it was discarded after once use.
//dest.port = 50001; working, but ugly and dangerous
cout << "send back result to host: " << dest.host
<< "; port: " << dest.port << endl;
int sock = 0;
cout << "host entity ready, now simpleSend..." << endl;
simpleSend(result, dest, sock);
//d3_closeConnection(sock);//may be necessary.
// Do nothing below.----------------
operation_status = 0;
}
}
}
break;
case 2: //remove
cout << "Remove..."<< endl;
if (package.virtualpath().empty()) {
cerr << "Bad key: nothing to remove" << endl;
operation_status = -1;
} else {
operation_status = HB_remove(db, package.virtualpath());
}
//.........这里部分代码省略.........