5dab67c4142175c4039d29b6739637e1717a9b03
[sdk] / samples / net / SocketRx / socketRx.ec
1 import "ecere"
2
3 // We'll use TCP/IP port 5623 for this sample
4 define samplePort = 5623;
5
6 // We will use this simple structure for our messages
7 struct SamplePacket
8 {
9    int stringLen;
10    // stringLen + 1 bytes are actually used (variable size depending on string)
11    char string[1];   
12 };
13
14 class SampleService : Service
15 {
16    void OnAccept()
17    {
18       // When we get an incoming connection to our service, we spawn a SampleSocket
19       SampleSocket { this };
20    }
21 }
22
23 class SampleSocket : Socket
24 {
25    unsigned int OnReceive(unsigned char * buffer, unsigned int count)
26    {
27       // We only process the data if we've received enough bytes to make up the message
28       // This first if just checks if we have reveived enough bytes for the header
29       if(count >= sizeof(SamplePacket))
30       {
31          SamplePacket * packet = (SamplePacket *) buffer;
32          uint size = sizeof(SamplePacket) + packet->stringLen;
33          // Here we check if we've actually received the entire message
34          if(count >= size)
35          {
36             // We've received a complete message, so we change the contents of the recvString EditBox
37             log.PutS(packet->string);
38             log.PutS("\n");
39             // and we return the size of the message we've processed.
40             // If more data is already buffered, this method will be called again right away.
41             return size;
42          }
43       }
44       // We haven't received enough data to process this message yet: return 0 bytes processed
45       // This method will be called again once more data has been received.
46       return 0;
47    }
48 }
49
50 // The service
51 SampleService service { port = samplePort };
52
53 EditBox log
54 {
55    caption = "RX Sample";
56    hasClose = true;
57    readOnly = true;
58    noCaret = true;
59    size = { 640, 480 };
60    hasVertScroll = true;
61    multiLine = true;
62 };
63 class RXApp : GuiApplication
64 {
65    bool Init()
66    {
67       service.Start();
68       return true;
69    }
70 }