/* 這是一個函數的定義,函數參數可以認為是局部變量 */ int fn_c( int x_local) { /* 這是一個未初始化的局部變量的定義 */ int y_local_uninit ; /* 這是一個初始化的局部變量的定義 */ int y_local_init = 3 ; /* 以下代碼通過局部變量、全局變量和函數的名字來使用它們 */ x_global_uninit = fn_a (x_local, x_global_init); y_local_uninit = fn_a (x_local, y_local_init); y_local_uninit += fn_b (z_global); return (x_global_uninit + y_local_uninit); }
void FerryStream::heart(FerryStream* fs) { string truebuffer = ""; bool pckEndReached = false; bool goodPacket = true; string index_str = ""; int index = 0; int dindex = 7; string terminatingSign = ""; int packStartIndex = -1; int packEndIndex = -1; string bridge; while (true && !force_exit) { pckEndReached = false; truebuffer = ""; try { while (!pckEndReached && !force_exit) { fs->buffer = bridge + fs->buffer; if (terminatingSign.length() > 0 && (packEndIndex = fs->buffer.find(terminatingSign, 0)) >= 0) { int elength = packEndIndex + terminatingSign.length() - bridge.length(); truebuffer += fs->buffer.substr(bridge.length(), elength); pckEndReached = true; goodPacket = true; terminatingSign = ""; break; } if ((packStartIndex = (int) fs->buffer.find("{index:", packStartIndex)) >= 0) { if (!goodPacket) { ffl_err(FPL_FSTREAM_HEART, "packet %d corrupted. " "Discarding %dbytes", index, truebuffer.length()); } goodPacket = false; dindex = 7 + packStartIndex; index_str = ""; int i = 0; while (fs->buffer[dindex] != ',' && i < 28) { index_str += fs->buffer[dindex]; dindex++; i++; } index = (int) atof(index_str.c_str()); if (fs->buffer[dindex] == ',' && index > 0) { terminatingSign = "endex:" + index_str + "}"; if (terminatingSign.length() > 0 && (packEndIndex = fs->buffer.find(terminatingSign, packStartIndex)) >= 0) { int eindex = packEndIndex + terminatingSign.length(); truebuffer = fs->buffer.substr(packStartIndex, eindex); packStartIndex += eindex + 1; pckEndReached = true; goodPacket = true; terminatingSign = ""; break; } else { truebuffer = fs->buffer.substr(packStartIndex, fs->buffer.length()); } } else { packStartIndex += 9; } } else if (terminatingSign.length() > 0) { truebuffer += fs->buffer.substr(bridge.length(), fs->buffer.length()); //truebuffer should be assigned prior to bridge } if (terminatingSign.length() > 0) { bridge = fs->buffer.substr(fs->buffer.length() - terminatingSign.length() + 1, fs->buffer.length()); } *fs->source >> fs->buffer; packStartIndex = 0; } try { FFJSON* media_pack = new FFJSON(truebuffer); if (media_pack->isType(FFJSON::OBJ_TYPE::OBJECT) && &(*media_pack)["ferryframes"] != NULL) { ofstream offpmpack; offpmpack.open("offpmpack.json"); offpmpack << truebuffer; offpmpack.close(); vector<FFJSON*>* frames = (*media_pack)["ferryframes"].val.array; //vector<FFJSON*>* sizes = (*media_pack)["framesizes"].val.array; char* ferrymp3 = (*media_pack)["ferrymp3"].val.string; int i = frames->size(); const char* frame; string fn_b(ferrymedia_store_dir + string(itoa((*media_pack)["index"].val.number))); string fn; int fd; while (i > 0) { i--; frame = (const char*) (*frames)[i]; // if ((*frames)[i]->size != (int) (*(*sizes)[i])) { // ffl_err(FPL_FSTREAM_HEART, "FerryStream: %s: packetNo: %s frameNo: %d frame size changed. Sent frame length :%d; Received frame length:%d.", id_path_map[fs->path].c_str(), fn_b.c_str(), i, (int) (*(*sizes)[i]), (*frames)[i]->size); // } fn = fn_b + "-" + string(itoa(i)) + ".jpeg"; fd = open(fn.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0755); int s; s = write(fd, frame, (*frames)[i]->size); close(fd); if (s >= 0) { ffl_debug(FPL_FSTREAM_HEART, "%s: frame %s successfully" " written", id_path_map[fs->path].c_str(), fn.c_str()); } else { ffl_err(FPL_FSTREAM_HEART, "%s: frame %s save" " failed", id_path_map[fs->path].c_str(), fn.c_str()); } } ofstream mp3segment(fn_b + ".mp3", std::ios_base::out | std::ios_base::binary); mp3segment.write(ferrymp3, (*media_pack)["ferrymp3"].size); mp3segment.close(); (*media_pack)["ferryframes"].setEFlag(FFJSON::B64ENCODE); (*media_pack)["ferrymp3"].setEFlag(FFJSON::B64ENCODE); pack_string_map[media_pack] = new string(media_pack->stringify()); packs_to_send[fs->path] = true; new_pck_chk = true; std::list<FFJSON*>* packsbuf = path_packs_map[fs->path]; if (packsbuf->size() >= packBufSize) { FFJSON* head = *packsbuf->begin(); delete pack_string_map[head]; pack_string_map.erase(head); ffl_debug(FPL_FSTREAM_HEART, "Bye %d :) Kicking him out (;", (int) (*head)["index"]); delete head; packsbuf->pop_front(); } packsbuf->push_back(media_pack); } else { delete media_pack; } } catch (FFJSON::Exception e) { ffl_err(FPL_FPORT, "Illegal meadia pack received on %s", id_path_map[fs->path].c_str()); ffl_debug(FPL_FPORT, "FFJSON::Exception: %s", e.what()); } } catch (SocketException e) { ffl_err(FPL_FPORT, "I, %s dying! Connection to %s lost.", id_path_map[fs->path].c_str(), fs->source->getDestinationIP().c_str()); break; } } deadFSList.push_back(fs); }