Table of Contents
About Poco::TCPServer framework (select model is used under windows) study notes., pocotcpserver
Description
Things to know before you start:
Understanding of constructor initialization sequence
Start
The relationship between the main classes used in the code
Main categories:
Flowchart:
Let’s take a look at the process of PooledThread first
Let’s take another look at the process dominated by TCPServer
The image is too wide and cannot be displayed (please open the image in a new tab. Thank you.)
Home Backend Development PHP Tutorial About Poco::TCPServer framework (select model is used under windows) study notes., pocotcpserver_PHP tutorial

About Poco::TCPServer framework (select model is used under windows) study notes., pocotcpserver_PHP tutorial

Jul 13, 2016 am 10:01 AM
poco

About Poco::TCPServer framework (select model is used under windows) study notes., pocotcpserver

Description

Why am I writing this article? I have read Ah Er’s Dream Boat’s http://www.cppblog.com/richbirdandy/archive/2010/09/10/123994 .html

Unfortunately, there are too many codes and it looks cumbersome. So I prepared a flow chart to simplify it for easy understanding and convenience for future use.

The content of this article is based on window api analysis.

The poco of this article is version 1.4.6p4 (2014-04-18). Although the poco version is now 1.6, the call has not changed much.

poco download address: http://pocoproject.org/releases/

This article uses TimeServer.cpp as the entry point for analysis:

Things to know before you start:

1, Inline inline function: You can refer to:

http://blog.sina.com.cn/s/blog_90e888f50100zgo2.html

Mainly to improve execution efficiency.

2, Overloading, rewriting, hiding of class member functions,

Reference:

dazhong159's

http://blog.csdn.net/dazhong159/article/details/7844369

Extensively used, rewritten, and hidden in the code.

3, Principle of select model:

Quote

A humorous explanation of the six Socket I/O models

http://blog.csdn.net/normalnotebook/article/details/999840

Contents of

:

for i:=0 to fd_read.fd_count-1 do //Note, fd_count

It is a synchronous operation.

Old Chen really wants to see his daughter’s letter. So much so that he goes downstairs to check the mailbox every 10 minutes to see if there is a letter from his daughter~~~~~
In this case, "going downstairs to check the mailbox" and then going back upstairs took up so much time that Chen couldn't do other work.
The select model is very similar to Lao Chen’s situation: check over and over again...if there is data...receive/send...

<span>.....
 MainSock :</span>=<span> socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
 addr.sin_family :</span>=<span> AF_INET;
 addr.sin_port :</span>= htons(<span>5678</span><span>);
 addr.sin_addr.S_addr :</span>=<span> htonl(INADDR_ANY);
 bind( MainSock, @addr, </span><span>sizeof</span><span>(addr) );
 listen( MainSock, </span><span>5</span><span> );
 
 </span><span>while</span> (not Terminated) <span>do</span><span>
 begin
 FD_ZERO( fd_read );
 FD_SET( MainSock, fd_read );
 timeout.tv_sec :</span>= <span>0</span><span>;
 timeout.tv_usec :</span>= <span>500</span><span>;
 </span><span>if</span> <span>select</span>( <span>0</span>, @fd_read, nil, nil, @timeout ) > <span>0</span> then <span>//</span><span>至少有1个等待Accept的connection</span>
<span> begin
 </span><span>if</span><span> FD_ISSET( MainSock, fd_read ) then
 begin
 </span><span>for</span> i:=<span>0</span> to fd_read.fd_count-<span>1</span> <span>do</span> <span>//</span><span>注意,fd_count <= 64,也就是说select只能同时管理最多64个连接</span>
<span> begin
 len :</span>= <span>sizeof</span><span>(addr);
 ASock :</span>=<span> accept( MainSock, addr, len );
 </span><span>if</span> ASock <><span> INVALID_SOCKET then
 ....</span><span>//</span><span>为ASock创建一个新的线程,在新的线程中再不停地select</span>
<span> end; 
 end; 
 end; 
 end; </span><span>//</span><span>while (not self.Terminated)</span>
<span> 
 shutdown( MainSock, SD_BOTH );
 closesocket( MainSock );
 end;<br /><br /></span>
Copy after login

So, the select model can only be used for general small connections....high concurrency is not possible.

4,

Understanding of constructor initialization sequence

C constructors are called in the following order:
(1) The constructors of any virtual base class are constructed in the order in which they are inherited;
(2) The constructors of any non-virtual base class are constructed in the order in which they are inherited;
(3) The constructors of any member objects are called in the order in which they are declared;
(4) The class's own constructor.

5, About FastMutex mutex variables

bool NotificationQueue::empty() const
{
FastMutex::ScopedLock lock(_mutex);
return _nfQueue.empty();
}

After empty() is executed, call the ~FastMutex::ScopedLock destructor to release it.

The critical section used under window:

class Foundation_API MutexImpl
{
protected:
MutexImpl();
~MutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();

private:
CRITICAL_SECTION _cs;//Critical section
};

You can see the critical section used in the Windows environment.

6, About threads:

Use under window

_thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId);

Execute thread operations.

7, waiting for events, synchronization of connection requests is used

WaitForSingleObject(This is also my favorite)

Activate reset through SetEvent (),ResetEvent().

8, static_cast<> reinterpret_cast<> dynamic_cast<> usage.

Please refer to:

http://www.cnblogs.com/bastard/archive/2011/12/14/2288117.html

http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html

Like in code:

void* pThread;

reinterpret_cast(pThread)->_pRunnableTarget->run();

//reinterpret_cas This conversion is the most "unsafe". Conversion between two unrelated class pointers can be achieved using this conversion. For example

_threadId = static_cast(threadId);

//static_cast is used for basic data type conversion (char, int), and conversion between pointers

9, About the smart (smart) pointer auto_ptr.

auto_ptr To put it simply, it ensures that the created resources can be freed when exiting (regardless of whether there is an exception or not)

std::auto_ptr pConnection(_pConnectionFactory->createConnection(pCNf->socket()));

AutoPtr pNf = _queue.waitDequeueNotification(idleTime);

can be found directly in

template
class auto_ptr
{ // wrap an object pointer to ensure destruction

You can refer to:

More Effective C Chinese version.pdf 7.4 Item M28: Smart Pointer Chapter (find and download on baidu)

http://blog.chinaunix.net/uid-9525959-id-2001803.html

Fragment from

:

How to avoid the pitfalls of using auto_ptr

Auto_ptr is not perfect. It is indeed very convenient, but it also has pitfalls. You should pay attention to avoid it when using it. First of all, do not use auto_ptr objects as elements of STL containers. The C standard explicitly prohibits this, otherwise unforeseen results may be encountered.
Another flaw of auto_ptr is to use an array as a parameter of auto_ptr:
auto_ptr pstr (new char[12] ); //Array; is defined

Remember that whenever you use the new operation of an array, you must use delete[] to destroy the array. Because the destructor of auto_ptr only works on non-array types. So if the array cannot be destroyed correctly, the behavior of the program is unclear. In short, auto_ptr controls a single object pointer allocated by new and nothing more.

However, this problem is solved in the C 11 standard:

unique_ptr

smart pointer with unique object ownership semantics

There can only be one owner pointer, which can be used for STL containers

shared_ptr

smart pointer with shared object ownership semantics

Sharable pointers

weak_ptr

weak reference to an object managed by std::shared_ptr

Weak reference pointer

auto_ptr

smart pointer with strict object ownership semantics

There can only be one owner pointer and cannot be used in STL containers

I have gone far and want to go deeper (don’t think too much -_-), please baidu...

After reading the above, I found out that all kinds of knowledge have been consolidated.

So we still have to look at open source code. In the past, the company didn’t use open source at all... Hey...

Start

The relationship between the main classes used in the code

The picture is too wide and cannot be displayed (please open the picture in a new tab. Thank you.)

Main categories:

1, TCPServer main service, responsible for calling the select model to handle changes in connection messages.

2. PooledThread is a thread pool. When activated, TCPServerDispatcher::run() is called to handle the specific request after receiving the package. And TCPServerDispatcher::run() is called

 TimeServerConnection.run(). TimeServerConnection.run() is hidden by subclasses to implement programmer-defined functions. I have to say the advantages and disadvantages of writing POCO.

3, TCPServerDispatcher, dispatch manager (let’s call it that for now). Receives message changes, puts them into the queue, and manages the number of connections.

When placed in the queue, the event in PooledThread will be activated.

PooledThread in turn activates TCPServerDispatcher::run() [let’s call it mutual activation when conditions permit]

4, TCPServerConnection. To implement specific behaviors, customize the implementation function by inheriting the run() of the subclass.

5,TCPServerConnectionFactory is responsible for creating and managing TCPServerConnection.

6, TCPServerParams parameter management, I won’t talk about it. You know.

7,NotificationQueue puts the connection into the queue for management.

After reading the introduction of the main categories, you should have a general understanding of other processes.

Flowchart:

Because the picture is too long, there are many reasons,

The picture is too wide and cannot be displayed (please open the picture in a new tab. Thank you.)

Let’s take a look at the process of PooledThread first

Let’s take another look at the process dominated by TCPServer
The image is too wide and cannot be displayed (please open the image in a new tab. Thank you.)

The performance of select under windows is indeed not very good, and the linux version uses epoll.

epoll is more efficient than select. Please refer to: http://blog.csdn.net/legion8169/article/details/1637154

但poco tcpserver 中是有线程池操作的,所以说来效率不会太低.

先到这儿,还没有写完.

我们可以改变什么:

    ThreadPool(<span>int</span> minCapacity = <span>2</span><span>,
        </span><span>int</span> maxCapacity = <span>16</span><span>,
        </span><span>int</span> idleTime = <span>60</span><span>,
        </span><span>int</span> stackSize =<span> POCO_THREAD_STACK_SIZE);
        </span><span>///</span><span> Creates a thread pool with minCapacity threads.
        </span><span>///</span><span> If required, up to maxCapacity threads are created
        </span><span>///</span><span> a NoThreadAvailableException exception is thrown.
        </span><span>///</span><span> If a thread is running idle for more than idleTime seconds,
        </span><span>///</span><span> and more than minCapacity threads are running, the thread
        </span><span>///</span><span> is killed. Threads are created with given stack size.</span>
Copy after login

增加线程池中线程数(费话!),来加快select 中处理.

在中等程序中,增加  TCPSelect Manage进程,  来负责与多个  TcpServer 的进程通信.

即再增加一个管理者(中间键,或activemq之类),来加强并发能力,

或者直接使用linux 环境 ,即用了epoll 来实现高效性.

 

个人愚见,可能有些没写明白.还望高手指点.

谢谢.

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/971772.htmlTechArticle关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.,pocotcpserver 说明 为何要写这篇文章 ,之前看过阿二的梦想船的Poco::TCPServer框...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Early Xiaomi POCO F7 Pro sighting reveals link with Redmi K80 Early Xiaomi POCO F7 Pro sighting reveals link with Redmi K80 Jul 31, 2024 am 08:48 AM

The POCO F6 Pro (curr. $512.50 on Amazon) has not been out long, having been announced on May 23. Nonetheless, Gizmochina has already spotted an early sighting of its successor. As is often the case, the website has discovered the first reference to

Xiaomi POCO F7 revealed to receive dedicated global and Indian releases Xiaomi POCO F7 revealed to receive dedicated global and Indian releases Aug 08, 2024 pm 09:31 PM

Xiaomi looks to be preparing another slew of Redmi and POCO smartphones that could be with us later this year. In recent days, evidence that the company was developing the POCO F7 Pro for global audiences, which it will offer in China as the Redmi K8

Released on August 1, physical photos of Xiaomi POCO M6 Plus 5G mobile phone and POCO Buds X1 headphones exposed Released on August 1, physical photos of Xiaomi POCO M6 Plus 5G mobile phone and POCO Buds X1 headphones exposed Jul 30, 2024 pm 02:29 PM

According to news on July 30, source Yogesh Brar posted a tweet on the X platform yesterday (July 29), sharing physical photos of Xiaomi POCOM6 Plus 5G mobile phone and POCO Buds X1 headphones. The official announcement of the two products will be released on August 1. Xiaomi POCOM6 Plus 5G mobile phone sources said that Xiaomi POCOM6 Plus 5G mobile phone will be equipped with a 6.8-inch LCD screen, equipped with Qualcomm Snapdragon 4Gen2AE processor, equipped with a 108-megapixel camera on the back, and a 5030mAh capacity battery. As previously reported, this phone is available in three colors: purple, black, and silver. It is roughly the same as the standard POCOM6 phone, but the LED flash ring is relatively more prominent. POCOB

Xiaomi POCO X6 Neo mobile phone will be launched in India in March, POCO F6 is expected to be launched in July Xiaomi POCO X6 Neo mobile phone will be launched in India in March, POCO F6 is expected to be launched in July Feb 15, 2024 pm 02:24 PM

According to news on February 8, @YogeshBrar broke the news that POCOX6Neo will be released in India in March 2024. This smartphone will be a rebranded version of the RedmiNote13RPro launched by Xiaomi in China in November last year. Brar also added that POCOF6 will be launched in India in July 2024. Xiaomi launched the Redmi Note 13R Pro mobile phone directly in the mall last November, and the only 12GB+256GB version sold for 1,999 yuan. The machine is equipped with MediaTek Dimensity 6080 processor (note: the original Dimensity 810), with a maximum frequency of up to 2.4GHz; it has a built-in 5000mAh battery and supports 33W fast charging. The machine also offers three colors and is 7.73mm thick.

Xiaomi POCO Pad 5G tablet released overseas on August 23: 12.1-inch 120Hz screen, supports stylus/external keyboard Xiaomi POCO Pad 5G tablet released overseas on August 23: 12.1-inch 120Hz screen, supports stylus/external keyboard Aug 16, 2024 pm 10:35 PM

According to news from this site on August 16, Xiaomi POCO officially announced today on the A 12.1-inch 120Hz screen. Xiaomi POCO launched the standard version of POCO Pad overseas in May this year. The machine uses a 12.1-inch 1600x2560 resolution 120Hz IPS LCD screen, is equipped with 8GB RAM and 256GB storage space, and is equipped with a Qualcomm Snapdragon 7sGen2 processor. Since the Qualcomm Snapdragon 7sGen2 processor supports 5G communications, the SoC of this POCOPad 5G tablet may not

Xiaomi ThePaper OS international version upgrade plan officially launched Xiaomi ThePaper OS international version upgrade plan officially launched Dec 19, 2023 pm 05:39 PM

Xiaomi has announced the launch of the new Xiaomi Pascal OS. Unlike Huawei's Hongmeng OS, this operating system will not only be suitable for domestic users, but will also be provided to overseas users, replacing the original MIUI system. According to the latest news, Xiaomi's overseas brand POCO announced that their flagship model POCOF5 will be the first to launch the international version upgrade of Xiaomi ThePaper OS, and the upgrade plan for other models will be announced later. According to the editor’s understanding, the first batch of overseas models that can be upgraded to Xiaomi ThePaper OS total 11 models. , including Xiaomi 13T, Xiaomi 13TPro, Xiaomi 12T, Xiaomi 11T, Xiaomi 13, Xiaomi 13Pro, Xiaomi 13 Extreme Commemorative Edition, POCOF5, POCOF5Pro, and RedmiNote12

Xiaomi POCO X6 Neo mobile phone released overseas on March 13: Dimensity 6080, 6.67-inch OLED screen Xiaomi POCO X6 Neo mobile phone released overseas on March 13: Dimensity 6080, 6.67-inch OLED screen Mar 09, 2024 pm 09:07 PM

According to news on March 9, according to the official account of the Xiaomi POCOX platform, the POCOX6 Neo mobile phone will be released on March 13, 2024. This mobile phone is a renamed version of the Redmi Note13R Pro launched by Xiaomi in the Chinese mainland market in November last year. Xiaomi RedmiNote13RPro parameter information is compiled as follows: SoC: Dimensity 6080 processor (Note: original Dimensity 810) Screen: 6.67-inch 2400×1080 resolution OLED flexible straight screen, global excitation brightness 1000nit RAM: 12GB Storage space: 256GB Rear camera: 108MP main camera + 2MP depth of field front camera: 16MP battery: 5000 mAh, supports 33W wired fast charging

Xiaomi reveals launch date of new Poco Pad with 5G connectivity Xiaomi reveals launch date of new Poco Pad with 5G connectivity Aug 17, 2024 am 06:32 AM

Xiaomi has announced a new Android tablet that will launch under the Poco branding. It's called the Poco Pad 5G, and the tab is set to launch in India on August 23. The first-ever table that launched under the Poco brand was revealed in Mayof this ye

See all articles