aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/async_ports_SUITE_data/cport.c
blob: 033aff382a866cb27c6b1e66e47d96ce6cf953aa (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef __WIN32__
#  include "windows.h"
#  include "winbase.h"
#else
#  include <unistd.h>
#endif

typedef unsigned char byte;

int read_cmd(byte *buf)
{
  int len;
  if (read_exact(buf, 4) != 4)
      return(-1);

  len = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  return read_exact(buf, len);
}

int write_cmd(byte *buf, int len)
{
  byte li[4];
  li[0] = (len >> 24) & 0xff;
  li[1] = (len >> 16) & 0xff;
  li[2] = (len >> 8)  & 0xff;
  li[3] = len  & 0xff;
  write_exact(&li, 4);

  return write_exact(buf, len);
}

int read_exact(byte *buf, int len)
{
  int i, got=0;
  do {
    if ((i = read(0, buf+got, len-got)) <= 0)
      {
	return(i);
      }
    got += i;
  } while (got<len);
  return len;
}

int write_exact(byte *buf, int len)
{
  int i, wrote = 0;
  do {
    if ((i = write(1, buf+wrote, len-wrote)) < 0)
      return (i);
    wrote += i;
  } while (wrote<len);
  return len;
}

byte static_buf[31457280]; // 30 mb

int main(int argc, char **argv) {
  int sleep_time = atoi(argv[1]);
  int fn, arg, res;
  byte *buf = &static_buf[0];
  int len = 0;
  if (sleep_time <= 0)
    sleep_time = 0;
#ifdef __WIN32__
  else
    sleep_time = ((sleep_time - 1) / 1000) + 1; /* Milli seconds */
#endif
  while ((len = read_cmd(buf)) > 0) {
#ifdef __WIN32__
    Sleep((DWORD) sleep_time);
#else
    usleep(sleep_time);
#endif
    write_cmd(buf, len);
  }
}