第一次握手syn=1,ack=0,表示这是一个请求连接的包,seq number表示发包数据字节序号,由于下层的IP层可能会对TCP的数据拆包,因此IP层会对每个字节编号,但是这是建立连接期间,根本没有数据,发送只是只有首部的空包,因此seq number 是一个随机数,这个随机数是有用处的,用来作为认证标记。
第二次握手syn=1,ack=1,表示这是一个接受连接的包,seq number=y(同理y也是一个随机数),ack number=x+1,ack number 表示期望下一次接受到的包的起始序号,尽管没有收到应用数据,但是TCP规定,一次连接请求包要消耗一个seq number,因此ack number=x+1,必须等于x+1。
由以上分析可知,双方确定对方能收到自己的消息,必须3次握手,其实严格的说是至少3次握手,但是这三次握手必须发生**在一次有效的连接中,**比如A第一次发包,由于网络延迟没有及时到达B,于是A重发,但是之前的那个包B又接受到了,此时A和B的连接已经不是同一次连接了,这种情况肯定经常发生,但是TCP协议本身就很好地解决了这个问题,那就是seq number ack number 认证机制,由于seq这个数是随机数,三次握手都要用到,可保证三次握手是一次有效连接。
// Delete the existing data: Clear(); location.Clear();
// Get the file size, so we can pre-allocate the string. HUGE speed impact. long length = 0; fseek( file, 0, SEEK_END ); length = ftell( file ); fseek( file, 0, SEEK_SET );
// Strange case, but good to handle up front. if ( length <= 0 ) { SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); returnfalse; }
// Subtle bug here. TinyXml did use fgets. But from the XML spec: // 2.11 End-of-Line Handling // ...the XML processor MUST behave as if it normalized all line breaks in external // parsed entities (including the document entity) on input, before parsing, by translating // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to // a single #xA character. // // It is not clear fgets does that, and certainly isn't clear it works cross platform. // Generally, you expect fgets to translate from the convention of the OS to the c/unix // convention, and not work generally.
// Process the buffer in place to normalize new lines. constchar* p = buf; // the read head char* q = buf; // the write head constchar CR = 0x0d; constchar LF = 0x0a;
// Parse away, at the document level. Since a document // contains nothing but other tags, most of what happens // here is skipping white space. if ( !p || !*p ) { SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); return0; }
// Note that, for a document, this needs to come // before the while space skip, so that parsing // starts from the pointer we are given. location.Clear(); if ( prevData ) { location.row = prevData->cursor.row; location.col = prevData->cursor.col; } else { location.row = 0; location.col = 0; } TiXmlParsingData data( p, TabSize(), location.row, location.col ); location = data.Cursor();
if ( encoding == TIXML_ENCODING_UNKNOWN ) { // Check for the Microsoft UTF-8 lead bytes. constunsignedchar* pU = (constunsignedchar*)p; if ( *(pU+0) && *(pU+0) == TIXML_UTF_LEAD_0 && *(pU+1) && *(pU+1) == TIXML_UTF_LEAD_1 && *(pU+2) && *(pU+2) == TIXML_UTF_LEAD_2 ) { encoding = TIXML_ENCODING_UTF8; useMicrosoftBOM = true; } }
// Check for and read attributes. Also look for an empty // tag or an end tag. while ( p && *p ) { pErr = p; p = SkipWhiteSpace( p, encoding ); if ( !p || !*p ) { if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding ); return0; } if ( *p == '/' ) { ++p; // Empty tag. if ( *p != '>' ) { if ( document ) document->SetError( TIXML_ERROR_PARSING_EMPTY, p, data, encoding ); return0; } return (p+1); }
// 读取元素的值 elseif ( *p == '>' ) { // Done with attributes (if there were any.) // Read the value -- which can include other // elements -- read the end tag, and return. ++p; //有可能这个元素没有值,接着又是子元素,如<Person><Boy>Jim</Boy></Person> p = ReadValue( p, data, encoding ); // Note this is an Element method, and will set the error if one happens. if ( !p || !*p ) { // We were looking for the end tag, but found nothing. // Fix for [ 1663758 ] Failure to report error on bad XML if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding ); return0; }
// We should find the end tag now // note that: // </foo > and // </foo> // are both valid end tags. if ( StringEqual( p, endTag.c_str(), false, encoding ) ) { p += endTag.length(); p = SkipWhiteSpace( p, encoding ); if ( p && *p && *p == '>' ) { ++p; return p; } if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding ); return0; } else { if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding ); return0; } }
//读取元素的属性 else { // Try to read an attribute: TiXmlAttribute* attrib = newTiXmlAttribute(); if ( !attrib ) { return0; }
// Read in text and elements in any order. constchar* pWithWhiteSpace = p; p = SkipWhiteSpace( p, encoding );
while ( p && *p ) { if ( *p != '<' ) { // Take what we have, make a text element. TiXmlText* textNode = newTiXmlText( "" );
if ( !textNode ) { return0; }
if ( TiXmlBase::IsWhiteSpaceCondensed() ) { p = textNode->Parse( p, data, encoding ); } else { // Special case: we want to keep the white space // so that leading spaces aren't removed. p = textNode->Parse( pWithWhiteSpace, data, encoding ); }
//一个子元素标签的开始,解析子元素 else { // We hit a '<' // Have we hit a new element or an end tag? This could also be // a TiXmlText in the "CDATA" style. if ( StringEqual( p, "</", false, encoding ) ) { return p; } else { TiXmlNode* node = Identify( p, encoding ); if ( node ) { p = node->Parse( p, data, encoding ); LinkEndChild( node ); } else { return0; } } } pWithWhiteSpace = p; p = SkipWhiteSpace( p, encoding ); }
if ( !p ) { if ( document ) document->SetError( TIXML_ERROR_READING_ELEMENT_VALUE, 0, 0, encoding ); } return p; }
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. /home/kent/StartMain>/dev/null 2>&1 &
/** 单例模式采用boost源码 由于boost单例返回的是引用,下面改成指针 */ template<classT> classBaseSingleton { public: BaseSingleton(){} private: structobject_creator { // This constructor does nothing more than ensure that instance() // is called before main() begins, thus creating the static // T object before multithreading race issues can come up. object_creator() { BaseSingleton<T>::Instance(); } inlinevoiddo_nothing()const{ } }; static object_creator create_object;
//BaseSingleton();
public: typedef T object_type;
// If, at any point (in user code), BaseSingleton<T>::instance() // is called, then the following function is instantiated. static object_type* Instance() { // This is the object that we return a reference to. // It is guaranteed to be created before main() begins because of // the next line. static object_type obj;
// The following line does nothing else than force the instantiation // of BaseSingleton<T>::create_object, whose constructor is // called before main() begins. create_object.do_nothing();