PHPPthreadsocket obtains the object value in the global static object

Source: Internet
Author: User
PHP Pthread

I have finished playing cards, game clients, and servers.

Currently, you can play cards for many people, but the socket uses a single thread.

When you play a card, you have to pause.
Sleep is used in many places.
Just sleep.
The whole server gets stuck because it is a single process and a single thread ..
So I want to solve this problem. I plan to use pthread. a player is a thread, which stores threads in the Player object and facilitates communication and latency.
After I run the Thread, it seems that I can't get data with the main process, or can I have a player object that is a Thread?
Because I have never used a thread too much and I don't know how to implement it.

Ask the experts to give me a thought Card. after a week of development, I can't change the language. then, the server is windows, because the code is too much and simple, and I can't get it by sticking to the code. **
Then I have a good idea to tell you that the sleep pause and communication section .....
Class WaittingRoom {
Public static $ Rooms = array ();
Public static $ Gamers = array ();
Public function addGamer ($ gamer_id ){
Db query
Self: $ Gamers = new Gamer ($ gamer_id );
}
}
WaittingRoom: addGamer ("game_id ");

Class Connection extends Thread {
Public $ connection;
Public $ uid = null;
Public $ is_free;
Public function _ construct ($ client ){
$ This-> is_free = false;
$ This-> connection = $ client;
$ This-> start ();
}
}
Connections = array ();
Do {
If there is a connection
Connections [] = new Con ($ sockit );
} While

Class Con (){
Public function _ construct ($ client ){
$ This-> connection = $ client;
$ This-> start ();
}
Public run (){
While ($ this-connection read data ){
Then process the data here
Use global static variables when processing data
* ***** WaittingRoom: $ Gamers [identifier]-> the database export operation cannot be obtained by the specified function or variable ********

}

}
}


Reply to discussion (solution)

Is there a problem with your thinking? It may also be because I cannot understand how your multithreading works.
A socket server can only listen on one port. how do you set a listening port with multiple threads? How do users know that Port is idle?

Why do you say sleep is widely used?
Since there is a loop wait (I guess), sleep will inevitably slow down the processing process.

If I guess it is unfortunate, you are using a point-to-point socket architecture.
The select architecture (point-to-point) should be used according to the application you described ):
Create a master listener
Add it to the connection word list sockets [] = master
In a permanent loop (it is not nice to say that an infinite loop ):
Get the copy of the connection word list changed = sockets
Read the socket_select (changed) status of each connection word (because socket_select changes the changed content, the copy is required in the previous step)
Socket_select returns only when the connection word list changes, so sleep is not required.
Traverse the valid connection word list foreach (changed as socket ):
If (socket = master) usually indicates a new connection request. add the socket to sockets and respond.
Else indicates that a user has data passed in, read and respond

As you can see, this architecture is not as complicated as you think.

Understanding what you said has already implemented socket to receive data is no problem. Multiple users can also accept data to receive data and use the data to create the object, that is, the server page for reading and writing operations. the object Hall object room object Player object Hall stores players and room objects. when you want to play the game, enter the room object and place the gamer object in the room object. after the player completes, start game creation. the result calculation of the referee object in the game engine
The licensing event synchronization and other issues have to wait for one or two seconds and there will be computers in it .... If the computer doesn't give sleep, it's your turn to deal with the computer ..

Alas, I spoke a lot about my limited language skills... A web page is a user page .. However, if socket is used, multiple users can read and write data on the same page.
If sleep is displayed on a page, all the players entering the page and the players in the room cannot receive the data. Therefore, multithreading is required.
At present, the main reason is that multithreading is not very good .. The multiple threads sleep will not affect the main thread .. Other users can read and write data.
Then, paste the code .. When a thread cannot read global objects .... Created object... That is, the player object.

No way? online?

To use multiple threads
If (socket = master) usually indicates a new connection request,Start a thread here to completeAdd socket to sockets and respond
Else indicates that a user has data input,Start a thread here to completeRead and respond

The main thread is responsible for scheduling, and the sub-thread is responsible for operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.