/* *功能: 发数据(一直发完nBytes字节为止,或者出错为止,或者超时为止) *输入参数: const void *pBuffer:待发送的缓冲区 unsigned int &nBytes:缓冲区的长度 int timeout: 超时的毫秒数(1秒=1000毫秒) -1表阻塞式接收 0:不可发时立即返回,不等待 >0:不可发时等待至超时 int flags:标志 *输出参数: unsigned int &nBytes:返回已发送的字节数 *返回值: -2:timeout -1:失败,具体错误可以通过GetLastError()获取 0:对方已关闭 >0:收到的数据长度 */ int CBaseSocket::SendN(unsigned int &nwrite, const void *pBuffer, unsigned int nBytes,int timeout /*= -1*/, int flags /*= 0*/) { if(m_iSocket < 0) { return -1; } nwrite = 0; if(timeout < 0) { return WriteN(nwrite, pBuffer, nBytes); } struct timeval start; struct timezone tz; gettimeofday(&start, &tz); unsigned int left = nBytes; int n = 0; char *ptr = (char *)pBuffer; while(1) { n = WaitWrite(timeout); if(n < 0) //error { return n; } n = send(m_iSocket, ptr, left, flags); if( n < 0) { if(errno == EINTR) { continue; } return n; } if(n == 0) { return 0; } left -= n; ptr += n; nwrite += n; if(left <= 0) { break; } timeout -= DiffMillSec(start); if(timeout <= 0) //timeout { return -2; } } return nwrite; }
bool CRemoteControl::SendPacket(CIrssMessage& message) { int iSize = 0; char* bytes = message.ToBytes(iSize); char buffer[4]; uint32_t len = htonl(iSize); memcpy(&buffer[0], &len, 4); bool bResult = WriteN(&buffer[0], 4); if (bResult) { bResult = WriteN(bytes, iSize); } delete[] bytes; if (!bResult) { Close(); return false; } return true; }
int main(){ void WriteX(unsigned,ofstream&); void WriteMX1(unsigned,ofstream&); void WriteR(unsigned,ofstream&); void WriteRO(ofstream&); void WriteP(unsigned,ofstream&); void WriteA(unsigned,ofstream&); void WriteL(unsigned,ofstream&); void WriteN(ofstream&); void WriteB(unsigned,ofstream&); unsigned dimension; string file; ofstream sfile; cout<<"This file is used to write the connections of the nodes in the .mx file"<<endl; cout<<"Give one of the dimensions of the MX1 matrix"<<endl; cin>>dimension; cout<<"Cool thanks"<<endl; cout<<"Please give a file name "<<endl; cin>>file; file = file + ".mx"; sfile.open(file,ios::app); if(sfile.fail()){ cerr<<"this file '"<<file<<"' does not exist in the form of .mx"<<endl; return false; } else{ cout<<"file "<<file<<" is k"<<endl; cout<<"On to the processing!!!"<<endl; //sfile<<"END CONNECTIONS"<<endl; WriteX(dimension,sfile); WriteMX1(dimension,sfile); WriteR(dimension,sfile); WriteRO(sfile); WriteP(dimension,sfile); WriteA(dimension,sfile); WriteL(dimension,sfile); WriteN(sfile); WriteB(dimension,sfile); sfile<<"END CONNECTIONS"<<endl; sfile.close(); } cout<<"File closed. TERMINATING....."<<endl; return 0; }
/* Handle client request */ void * HandleRequest(void *args) { char str[BUFSIZE]; TSocket cliSock; /* Extract socket file descriptor from argument */ cliSock = ((struct TArgs *) args) -> cliSock; free(args); /* deallocate memory for argument */ for(;;) { /* Receive the request */ if (ReadLine(cliSock, str, BUFSIZE-1) < 0) { ExitWithError("ReadLine() failed"); } else { executa(str); } /* Send the response */ if (WriteN(cliSock, str, strlen(str)) <= 0) { ExitWithError("WriteN() failed"); } } }
//将明细记录写入明细缓存中,如果缓存满,将缓存写入tmp文件,继续写缓存; int write_list_log_buff(char *data_file_name, int row_num, char *errcode, char *error_line) { int ret = 1 ; char filename[250]; char line[500]; int linelen = 0; int ltmpfd; memset(line, '\0', sizeof(line)); sprintf(line, "%s,%d,%s,%s\n", data_file_name, row_num, errcode, error_line); pthread_mutex_lock(&llb.lmutex); linelen = strlen(line); //printf("llb. linelen:[%d],lnum:[%d], fbuffersize:[%d]\n", linelen, llb.lnum, FBUFFSIZE); if ((linelen + llb.lnum) < LLOGBUFFSIZE) { strcpy(llb.lbuf + llb.lnum, line); llb.lnum += linelen; linelen = 0; } else { if (llb.fd <= 0) { //printf("llb. open file again.[%s]\n",llb.ltmpfname); llb.fd = open(llb.ltmpfname, O_WRONLY | O_CREAT, 0644); } if (llb.fd > 0) { //printf("llb. write to tmp file.[%d]\n",llb.fd); WriteN(llb.fd, llb.lbuf, llb.lnum); } else { perror("Can't open result file for list log file\n"); exit(-1); } memset(llb.lbuf, '\0', LLOGBUFFSIZE); llb.lnum = 0; strcpy(llb.lbuf, line); llb.lnum += linelen; linelen = 0; } pthread_mutex_unlock(&llb.lmutex); return ret; }
//将缓存和临时文件tmp中的明细记录写入明细文件中; int write_list_log_file() { int ret = 0; int ltmpfd; int lfd; int num; char buff[300]; pthread_mutex_lock(&llb.lmutex); if (llb.lnum > 0) { if (llb.fd <= 0) { //printf("llb. open file again.[%s]\n",llb.ltmpfname); llb.fd = open(llb.ltmpfname, O_WRONLY | O_CREAT, 0644); } if (llb.fd > 0) { WriteN(llb.fd, llb.lbuf, llb.lnum); memset(llb.lbuf, '\0', LLOGBUFFSIZE); llb.lnum = 0; close(llb.fd); llb.fd = NULL; } else { perror("Can't open result file for list log file\n"); exit(-1); } printf("写入正式明细文件。"); memset(buff, 0, sizeof(buff)); sprintf(buff, "mv %s %s", llb.ltmpfname, llb.lfname); printf("llb...buff = [%s]\n", buff); system(buff); } pthread_mutex_unlock(&llb.lmutex); return ret; }
int main(int argc, char *argv[]) { TSocket sock; char *servIP; /* server IP */ unsigned short servPort; /* server port */ char str[100]; int n; if (argc != 3) { ExitWithError("Usage: client <remote server IP> <remote server Port>"); } servIP = argv[1]; servPort = atoi(argv[2]); /* Create a connection */ sock = ConnectToServer(servIP, servPort); for(;;) { /* Write msg */ scanf("%99[^\n]%*c",str); n = strlen(str); str[n] = '\n'; if (WriteN(sock, str, ++n) <= 0) { ExitWithError("WriteN() failed"); } if (strncmp(str, "quit", 4) == 0) break; /* Receive the response */ if (ReadLine(sock, str, 99) < 0) { ExitWithError("ReadLine() failed"); } else printf("%s",str); } close(sock); return 0; }
bool OutStream::WriteAggregate (const HQuaternion& datum) { return WriteN(4, (const float*)datum); }
bool OutStream::WriteAggregate (const Float2& datum) { return WriteN(2, (const float*)datum); }
void FinishWriteFile(OStream *file, const Animation::WriteOptions& options, vector<string>& warnings, void *state) { PrivateData *pd = reinterpret_cast<PrivateData *>(state); if(pd == NULL) throw POV_EXCEPTION_CODE(kNullPointerErr); POV_LONG stsz_size = 20 + (pd->imagesizes.size() * 4); POV_LONG stsc_size = 28; POV_LONG stts_size = 32; POV_LONG stsd_size = 102; POV_LONG stbl_size = 8 + stsd_size + stts_size + stsc_size + stsz_size; POV_LONG vmhd_size = 20; POV_LONG minf_size = 8 + vmhd_size + stbl_size; POV_LONG hdlr_size = 32; POV_LONG mdhd_size = 32; POV_LONG mdia_size = 8 + mdhd_size + hdlr_size + minf_size; POV_LONG tkhd_size = 112; POV_LONG trak_size = 8 + tkhd_size + mdia_size; POV_LONG mvhd_size = 108; POV_LONG moov_size = 8 + mvhd_size + trak_size; int duration = pd->frameduration * pd->imagesizes.size(); // write movie atom WriteAtomHeader(file, 'moov', moov_size); // write movie header atom WriteAtomHeader(file, 'mvhd', mvhd_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 0); // creation time WriteInt4(file, 0); // modification time WriteInt4(file, pd->timescale); // time scale WriteInt4(file, duration); // duration WriteInt4(file, 1 << 16); // preferred playback rate WriteInt2(file, 1 << 8); // preferred sound volume WriteN(file, 10, 0); // ten reserved bytes WriteInt4(file, 1 << 16); // matrix a WriteInt4(file, 0); // matrix b WriteInt4(file, 0); // matrix u WriteInt4(file, 0); // matrix c WriteInt4(file, 1 << 16); // matrix d WriteInt4(file, 0); // matrix v WriteInt4(file, 0); // matrix tx WriteInt4(file, 0); // matrix ty WriteInt4(file, 1 << 30); // matrix w WriteInt4(file, 0); // preview time WriteInt4(file, 0); // preview duration WriteInt4(file, 0); // poster time WriteInt4(file, 0); // selection time WriteInt4(file, 0); // selection duration WriteInt4(file, 0); // current time WriteInt4(file, 2); // next track id (this code uses track 1) // write track atom WriteAtomHeader(file, 'trak', trak_size); // write track header atom WriteAtomHeader(file, 'tkhd', tkhd_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 0); // creation time WriteInt4(file, 0); // modification time WriteInt4(file, 1); // track id WriteN(file, 4, 0); // four reserved bytes WriteInt4(file, duration); // duration WriteN(file, 8, 0); // eight reserved bytes WriteInt2(file, 1); // layer WriteInt2(file, 0); // alternate group WriteInt2(file, 1 << 8); // sound volume WriteN(file, 2, 0); // two reserved bytes WriteInt4(file, 1 << 16); // matrix a WriteInt4(file, 0); // matrix b WriteInt4(file, 0); // matrix u WriteInt4(file, 0); // matrix c WriteInt4(file, 1 << 16); // matrix d WriteInt4(file, 0); // matrix v WriteInt4(file, 0); // matrix tx WriteInt4(file, 0); // matrix ty WriteInt4(file, 1 << 30); // matrix w WriteInt4(file, pd->width << 16); // track width WriteInt4(file, pd->height << 16); // track height // write media atom WriteAtomHeader(file, 'mdia', mdia_size); // write header media atom WriteAtomHeader(file, 'mdhd', mdhd_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 0); // creation time WriteInt4(file, 0); // modification time WriteInt4(file, pd->timescale); // time scale WriteInt4(file, duration); // duration WriteInt2(file, 0); // language WriteInt2(file, 0); // quality // write handler atom WriteAtomHeader(file, 'hdlr', hdlr_size); WriteInt4(file, 0); // version and flags WriteType(file, pd->componenttype); // component type WriteType(file, 'vide'); // component subtype (this media is video) WriteInt4(file, 0); // reserved WriteInt4(file, 0); // reserved WriteInt4(file, 0); // reserved // write media information atom WriteAtomHeader(file, 'minf', minf_size); // write video media information header atom WriteAtomHeader(file, 'vmhd', vmhd_size); WriteInt4(file, 0); // version and flags WriteInt2(file, 0); // graphics mode WriteInt2(file, 0); // opcolor red WriteInt2(file, 0); // opcolor green WriteInt2(file, 0); // opcolor blue // write sample table atom WriteAtomHeader(file, 'stbl', stbl_size); // write sample description atom WriteAtomHeader(file, 'stsd', stsd_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 1); // number of entries (this code only needs one entry) WriteInt4(file, 86); // description size WriteType(file, pd->componenttype); // data format WriteInt4(file, 0); // reserved WriteInt2(file, 0); // reserved WriteInt2(file, 0); // data reference index WriteInt2(file, 0); // version WriteInt2(file, 0); // revision level WriteType(file, 'appl'); // vendor WriteInt4(file, 0); // temporal quality WriteInt4(file, 512); // spacial quality WriteInt2(file, pd->width); // width WriteInt2(file, pd->height); // height WriteInt4(file, 72 << 16); // horizontal resolution WriteInt4(file, 72 << 16); // vertical resolution WriteInt4(file, 0); // data size (required to be zero according to Apple documentation) WriteInt4(file, 1); // frame count WriteN(file, 1, 4); // name (32-byte Pascal string, so first byte is length!) WriteType(file, pd->componenttype); // name (continued, uses codec type for simplicity) WriteN(file, 27, 0); // name (continued, unused) WriteInt2(file, options.bpcc * (3 + (pd->alphachannel ? 1 : 0))); // depth WriteInt2(file, -1); // color table id // write time-to-sample atom WriteAtomHeader(file, 'stts', stts_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 1); // number of entries (this code only needs one entry) WriteInt4(file, pd->imagesizes.size()); // sample count WriteInt4(file, pd->frameduration); // sample duration // write sample-to-chunk atom WriteAtomHeader(file, 'stsc', stsc_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 1); // number of entries (this code only needs one entry) WriteInt4(file, 1); // first chunk WriteInt4(file, pd->imagesizes.size()); // samples per chunk WriteInt4(file, 1); // sample description id // write sample size atom WriteAtomHeader(file, 'stsz', stsz_size); WriteInt4(file, 0); // version and flags WriteInt4(file, 0); // sample size (all samples have different sizes, so this needs to be zero) WriteInt4(file, pd->imagesizes.size()); // number of entries for(vector<int>::const_iterator i = pd->imagesizes.begin(); i != pd->imagesizes.end(); i++) WriteInt4(file, *i); // sample size entry delete pd; }
bool OutStream::WriteAggregateN (int numElements, const Float2* data) { return WriteN(2*numElements, (const float*)data); }
bool OutStream::WriteAggregateN (int numElements, const Vector3f* data) { return WriteN(3*numElements, (const float*)data); }
bool OutStream::WriteAggregate (const Vector3f& datum) { return WriteN(3, (const float*)datum); }
bool OutStream::WriteAggregateN (int numElements, const HQuaternion* data) { return WriteN(4*numElements, (const float*)data); }
bool OutStream::WriteAggregate (const AVector& datum) { return WriteN(4, (const float*)datum); }
bool OutStream::WriteAggregate (const HPlane& datum) { return WriteN(4, (const float*)datum); }
bool OutStream::WriteAggregateN (int numElements, const HMatrix* data) { return WriteN(16*numElements, (const float*)data); }
bool OutStream::WriteAggregate (const HMatrix& datum) { return WriteN(16, (const float*)datum); }