MiniGUI-Processes uses UNIX domain socket to build the communication between the server and the clients.
You can also use the underlay interfaces which MiniGUI uses to create your own UNIX domain socket.
Example:
#define LISTEN_SOCKET "/var/tmp/mysocket" static int listen_fd; BOOL listen_socket (HWND hwnd) { if ((listen_fd = serv_listen (LISTEN_SOCKET)) < 0) return FALSE; return RegisterListenFD (fd, POLL_IN, hwnd, NULL); } /* * When the server receives the request to connect from a client, * the window hwnd will receive a MSG_FDEVENT message. * Now the server can accept the request. */ int MyWndProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam) { switch (message) { ... case MSG_FDEVENT: if (LOWORD (wParam) == listen_fd) { /* This message comes from the listen socket fd. */ pid_t pid; uid_t uid; int conn_fd; conn_fd = serv_accept (listen_fd, &pid, &uid); if (conn_fd >= 0) { RegisterListenFD (conn_fd, POLL_IN, hwnd, NULL); } } else { /* Client send a request. */ int fd = LOWORD(wParam); /* Handle the request from client. */ sock_read_t (fd, ...); sock_write_t (fd, ....); } break; ... } } /* * Clients can use the following code to connect itself to the server. */ int conn_fd; if ((conn_fd = cli_conn (LISTEN_SOCKET, 'b')) >= 0) { /* Send a request to the server. */ sock_write_t (fd, ....); /* Get the reply from the server. */ sock_read_t (fd, ....); }
#define sock_read | ( | fd, | |||
buff, | |||||
count | ) | sock_read_t(fd, buff, count, 0) |
The blocking version of sock_read_t function.
#define sock_write | ( | fd, | |||
buff, | |||||
count | ) | sock_write_t(fd, buff, count, 0) |
The blocking version of sock_write_t function.
int cli_conn | ( | const char * | name, | |
char | project | |||
) |
Used by clients to connect to a server.
This function is used by clients to connect to a server.
The created socket will be located at the directory '/var/tmp', and with name of '/var/tmp/xxxxx-c', where 'xxxxx' is the pid of client. and 'c' is a character to distinguish different projects.
Note that MiniGUI itself uses 'a' as the project character to create socket between 'mginit' and clients.
name | The name of the well-known listen socket (created by server). | |
project | A character to distinguish different projects (Do NOT use 'a'). |
int serv_accept | ( | int | listenfd, | |
pid_t * | pidptr, | |||
uid_t * | uidptr | |||
) |
Waits for a client connection to arrive, and accept it.
This function is used by the server to wait a connection and accept it.
After creating a listening socket by calling serv_listen, you can call this function to create a connection with a client. We also obtain the client's PID and UID from the pathname that it must bind before calling us.
listenfd | The fd of listen socket. | |
pidptr | The client PID will be saved to this buffer when this function returns. | |
uidptr | The client UID will be saved to this buffer when this function returns. |
int serv_listen | ( | const char * | name | ) |
Creates a listen socket.
This function is used by the server to create a listening socket. Any MiniGUI-Processes application can call this function to create a listening socket. The server, i.e. mginit, of MiniGUI-Processes uses this function to create its listening socket, and named the socket to '/var/tmp/minigui'.
name | The path name of the listening socket. |
int sock_read_t | ( | int | fd, | |
void * | buff, | |||
int | count, | |||
unsigned int | timeout | |||
) |
Reads data from socket.
This function reads data which is count bytes long to the buffer buff from the socket fd.
fd | The file descriptor of the socket. | |
buff | The buffer used to save the data. | |
count | The length in bytes of the buffer. | |
timeout | An upper bound on the amount of time elapsed before sock_read_t returns. When it is zero, sock_read_t can block indefinitely. The timeout value is in the tick count of MiniGUI, and tick count of MiniGUI is in unit of 10 milliseconds. |
SOCKERR_OK | Read data successfully. | |
SOCKERR_IO | There are some I/O errors occurred. | |
SOCKERR_CLOSED | The socket has been closed by the peer. | |
SOCKERR_INVARG | You passed invalid arguments. | |
SOCKERR_TIMEOUT | Timeout. |
int sock_write_t | ( | int | fd, | |
const void * | buff, | |||
int | count, | |||
unsigned int | timeout | |||
) |
Writes data to socket.
This function writes the data block pointed to by buff which is count bytes long to the socket fd.
fd | The file descriptor of the socket. | |
buff | The buffer contains the data. | |
count | The length in bytes of the buffer. | |
timeout | An upper bound on the amount of time elapsed before sock_write_t returns. When it is zero, sock_write_t can block indefinitely. The timeout value is in tick count, and tick count of MiniGUI is in unit of 10 milliseconds. |
SOCKERR_OK | Read data successfully. | |
SOCKERR_IO | There are some I/O errors occurred. | |
SOCKERR_CLOSED | The socket has been closed by the peer. | |
SOCKERR_INVARG | You passed invalid arguments. | |
SOCKERR_TIMEOUT | Timeout. |