Some summary of mobile app / server development (2)

Previous article talked about data storage and common transmission protocols, but left a pit for custom transport protocols, just a little time, hurry up 🙂

Since choosing native socket, then a basic choice is TCP / UDP problem.

This actually still looks at the business’s own choice, just if you choose UDP, then many questions can not be considered, such as viscous bag issues. However, the UDP has a limit that the size of the data per transmission cannot exceed 64K, and this should be noted.

In order to consider complex situations, we still say that TCP is realized, this article first talks about some libraries and codes of Socket use

Android development

For Android, we have two main options: blocking Socket and Non-blocking Socket.

Blocking Socket is normal Socket, when the RECV is called, the data is blocked until the data is returned.

Non-blocking Socket can use NIO directly on Android because he has always made C ++ and Python before you have, so in fact, when you start NIO, you really don’t use it, put a few points to pay attention to:

1. When the network is disconnected, some mobile phones such as S4 will have this problem, and Channel.FinishConnect () will wait until 60 seconds. A TimeOUtexception will then be thrown.

The solution is:

Channel.socket (). Setsotimeout (5)

To set to just wait for 5 seconds.

It’s more strange to obviously asynchronous IO, why still have a waiting timeout. It is also necessary to study.

2. When the network is not online, launch the connection, and can still enter the ISCONNECTABLE, and FinishConnect can be called.

Solution: FinishConnect, call the Write an empty string, if you throw an exception, you have no link success.

In addition, different RECVs with Linux native non-blocking socket, NIO’s READ results and significance are as follows

-1: Socket is closed, close Channel, and cancel Key

0: There is no data readable, directly return

More than 0: The length of the data read

Server development

The choice of the Server side is more rich, and it is also a split plug and a non-blocking socket.

Blocking Socket

Blocking Socket’s simplest, the standard library comes with ThreadingTCPServer, which starts a thread when each request is coming. This method of server and pre-allocation process is actually a nature, and performance is a big problem.

Non-blocking SOCKET

Non-blocking Socket selection also has a lot, such as Asyncore and Asynchat in the Python Standard Library, but this is really a very old library, only support SELECT and POLL, and even Epoll cannot support. But use is no problem

As an alternative to Asyncore, Tornado provides a better choice. Of course, many people know that Tornado is because he is a high-performance HTTP Server, but in fact, the IOLOOP of the underlying packaged iOloop can also be used alone.

But Server based on asynchronous IO is actually a painful because all business operations cannot be blocked. Of course, you can use task assignment tools like Celery, but multi-level process communication can result in more performance.

Fortunately, the Python World has another option: gevent, gevent-based TCP Server is taking into account performance, most of the blocking request code can be used directly.

I like Tornado and GEVENT, I like it, so I have encapsulated Tkola and Gkola on the basis, and their underlying is dependent on Kola.

The reason why this package is to be aware of the Flask @route’s writing, and such a package is to achieve this style of encoding, of course, there are some more interesting things inside.

The sample code of GKOLA is as follows: