File: socketRx.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (70 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import "ecere"

// We'll use TCP/IP port 5623 for this sample
define samplePort = 5623;

// We will use this simple structure for our messages
struct SamplePacket
{
   int stringLen;
   // stringLen + 1 bytes are actually used (variable size depending on string)
   char string[1];
};

class SampleService : Service
{
   void OnAccept()
   {
      // When we get an incoming connection to our service, we spawn a SampleSocket
      SampleSocket { this };
   }
}

class SampleSocket : Socket
{
   unsigned int OnReceive(const unsigned char * buffer, unsigned int count)
   {
      // We only process the data if we've received enough bytes to make up the message
      // This first if just checks if we have reveived enough bytes for the header
      if(count >= sizeof(SamplePacket))
      {
         SamplePacket * packet = (SamplePacket *) buffer;
         uint size = sizeof(SamplePacket) + packet->stringLen;
         // Here we check if we've actually received the entire message
         if(count >= size)
         {
            // We've received a complete message, so we change the contents of the recvString EditBox
            logBox.PutS(packet->string);
            logBox.PutS("\n");
            // and we return the size of the message we've processed.
            // If more data is already buffered, this method will be called again right away.
            return size;
         }
      }
      // We haven't received enough data to process this message yet: return 0 bytes processed
      // This method will be called again once more data has been received.
      return 0;
   }
}

// The service
SampleService service { port = samplePort };

EditBox logBox
{
   caption = "RX Sample";
   hasClose = true;
   readOnly = true;
   noCaret = true;
   size = { 640, 480 };
   hasVertScroll = true;
   multiLine = true;
};
class RXApp : GuiApplication
{
   bool Init()
   {
      service.Start();
      return true;
   }
}