add libvncserver
[presencevnc] / libvnc / examples / backchannel.c
1 #include <rfb/rfb.h>
2
3 /*
4  * This is a simple example demonstrating a protocol extension.
5  *
6  * The "back channel" permits sending commands between client and server.
7  * It works by sending plain text messages.
8  *
9  * As suggested in the RFB protocol, the back channel is enabled by asking
10  * for a "pseudo encoding", and enabling the back channel on the client side
11  * as soon as it gets a back channel message from the server.
12  *
13  * This implements the server part.
14  *
15  * Note: If you design your own extension and want it to be useful for others,
16  * too, you should make sure that
17  *
18  * - your server as well as your client can speak to other clients and
19  *   servers respectively (i.e. they are nice if they are talking to a
20  *   program which does not know about your extension).
21  *
22  * - if the machine is little endian, all 16-bit and 32-bit integers are
23  *   swapped before they are sent and after they are received.
24  *
25  */
26
27 #define rfbBackChannel 155
28
29 typedef struct backChannelMsg {
30         uint8_t type;
31         uint8_t pad1;
32         uint16_t pad2;
33         uint32_t size;
34 } backChannelMsg;
35
36 rfbBool enableBackChannel(rfbClientPtr cl, void** data, int encoding)
37 {
38         if(encoding == rfbBackChannel) {
39                 backChannelMsg msg;
40                 const char* text="Server acknowledges back channel encoding\n";
41                 uint32_t length = strlen(text)+1;
42                 int n;
43
44                 rfbLog("Enabling the back channel\n");
45
46                 msg.type = rfbBackChannel;
47                 msg.size = Swap32IfLE(length);
48                 if((n = rfbWriteExact(cl, (char*)&msg, sizeof(msg))) <= 0 ||
49                                 (n = rfbWriteExact(cl, text, length)) <= 0) {
50                         rfbLogPerror("enableBackChannel: write");
51                 }
52                 return TRUE;
53         }
54         return FALSE;
55 }
56
57 static rfbBool handleBackChannelMessage(rfbClientPtr cl, void* data,
58                 const rfbClientToServerMsg* message)
59 {
60         if(message->type == rfbBackChannel) {
61                 backChannelMsg msg;
62                 char* text;
63                 int n;
64                 if((n = rfbReadExact(cl, ((char*)&msg)+1, sizeof(backChannelMsg)-1)) <= 0) {
65                         if(n != 0)
66                                 rfbLogPerror("handleBackChannelMessage: read");
67                         rfbCloseClient(cl);
68                         return TRUE;
69                 }
70                 msg.size = Swap32IfLE(msg.size);
71                 if((text = malloc(msg.size)) == NULL) {
72                         rfbErr("Could not allocate %d bytes\n", msg.size);
73                         return TRUE;
74                 }
75                 if((n = rfbReadExact(cl, text, msg.size)) <= 0) {
76                         if(n != 0)
77                                 rfbLogPerror("handleBackChannelMessage: read");
78                         rfbCloseClient(cl);
79                         return TRUE;
80                 }
81                 rfbLog("got message:\n%s\n", text);
82                 free(text);
83                 return TRUE;
84         }
85         return FALSE;
86 }
87
88 static int backChannelEncodings[] = {rfbBackChannel, 0};
89
90 static rfbProtocolExtension backChannelExtension = {
91         NULL,                           /* newClient */
92         NULL,                           /* init */
93         backChannelEncodings,           /* pseudoEncodings */
94         enableBackChannel,              /* enablePseudoEncoding */
95         handleBackChannelMessage,       /* handleMessage */
96         NULL,                           /* close */
97         NULL,                           /* usage */
98         NULL,                           /* processArgument */
99         NULL                            /* next extension */
100 };
101
102 int main(int argc,char** argv)
103 {                                                                
104         rfbScreenInfoPtr server;
105
106         rfbRegisterProtocolExtension(&backChannelExtension);
107
108         server=rfbGetScreen(&argc,argv,400,300,8,3,4);
109         server->frameBuffer=(char*)malloc(400*300*4);
110         rfbInitServer(server);           
111         rfbRunEventLoop(server,-1,FALSE);
112         return(0);
113 }