yifu / chess

Chess is attempt to write a chess client and server for online playing in c++. SDL is use on the client side for graphics.
0 stars 0 forks source link

Refactor the pipe into a msg queue. #19

Open yifu opened 9 years ago

yifu commented 9 years ago

Under linux msgqueue descriptor are file descriptor:

http://man7.org/linux/man-pages/man7/mq_overview.7.html

Polling message queue descriptors On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable.

yifu commented 9 years ago

Or deal with partial read. From the specification of read():

The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading.

A signal may be an hardware signal?

yifu commented 9 years ago

Or implement a SOCK_SEQPACKET, as from the the man unix page it's preserves msg boundaries!

and (since Linux 2.6.4) SOCK_SEQPACKET, for a connection-oriented socket that preserves message boundaries and delivers messages in the order that they were sent.

yifu commented 9 years ago

Or use fdopen to transform the file descriptor into a FILE handle, and use the fwrite()/fread() functions.

Cons: Actually it's buffered, event though it's optional, I don't think it's a good idea.

yifu commented 9 years ago

read() man page:

   MSG_WAITALL (since Linux 2.2)
          This flag requests that the operation block until the full request is sat‐
          isfied.   However, the call may still return less data than requested if a
          signal is caught, an error or disconnect occurs, or the next  data  to  be
          received is of a different type than that returned.

Cons: I do not know the exact message size before calling recv(). I cannot use that option.