Пример #1
0
void testReadOverload(const char* url) {
	Handle conn = maConnect(url);
	if(conn < 0) {
		printf("maConnect error %i\n", conn);
		return;
	}

	int result = 0;
	char buffer[1024];
	while(result >= 0) {
		EVENT event;
		while(maGetEvent(&event)) {
			if(event.type == EVENT_TYPE_CLOSE ||
				(event.type == EVENT_TYPE_KEY_PRESSED && event.key == MAK_0))
			{
				maExit(0);
			} else if(event.type == EVENT_TYPE_CONN) {
				printf("Op %i result %i\n", event.conn.opType, event.conn.result);
				MAASSERT(event.conn.handle == conn);
				result = event.conn.result;
				MAASSERT(result != 0);
			}
		}
		if(result == 0) {
			maWait(0);
		} else {
			maConnRead(conn, buffer, sizeof(buffer));
		}
	}
	printf("Done.\n");
}
Пример #2
0
extern "C" int MAMain() {
    InitConsole();
    gConsoleLogging = 1;

    static const char data[] = "userid=joe&password=guessme";
    int size = sizeof(data) - 1;
    char buffer[64];

    printf("HTTP POST test\n");
    Handle http = maHttpCreate("http://msdev.mine.nu:8080/testing/posttest.php", HTTP_POST);
    CT(http);

    maHttpSetRequestHeader(http, "X-MoSync-test", "terue");

    _itoa(size, buffer, 10);
    maHttpSetRequestHeader(http, "Content-Length", buffer);

    maHttpSetRequestHeader(http, "Content-Type", "application/x-www-form-urlencoded");

    printf("write\n");
    maConnWrite(http, data, size);
    if(waitConn(http) < 0)
        Freeze(0);

    printf("finish\n");
    maHttpFinish(http);
    if(waitConn(http) < 0)
        Freeze(0);

    int res = maHttpGetResponseHeader(http, "Content-Length", buffer, sizeof(buffer));
    if(res <= 0 || res >= (int)sizeof(buffer)) {
        printf("CLerr %i\n", res);
        Freeze(0);
    }
    printf("Content-Length: %s\n", buffer);

    res = 0;
    while(true) {
        maConnRead(http, buffer, sizeof(buffer)-1);
        size = waitConn(http);
        if(size < 0)
            break;
        res += size;
        buffer[size] = 0;
        printf(buffer);
    }
    printf("Bytes read: %i\n", res);
    Freeze(0);
    return 0;
}
Пример #3
0
int ConnReadAtLeast(Handle conn, int min, int max, char* dst) {
	int lPos = gPos;
	int pos = 0;
	while(pos < min) {
		int res;
		maConnRead(conn, dst + pos, max - pos);
		gPos = lPos;
		res = AnimatedConnWait();
		if(res <= 0) {
			return res;
		}
		pos += res;
	}
	return pos;
}
Пример #4
0
// returns the contents of the given url
char *Controller::readConnection(const char *url) {
  char *result = NULL;
  logEntered();
  _output->print("\033[ LLoading...");

  MAHandle conn = maConnect(url);
  if (conn < 1) {
    logPrint("Failed connecting to %s\n", url);
  } else {
    _runMode = conn_state;
    logPrint("Connecting to %s\n", url);

    bool connected = false;
    byte buffer[1024];
    int length = 0;
    int size = 0;
    int now = maGetMilliSecondCount();
    MAEvent event;

    // pause until connected
    while (_runMode == conn_state) {
      event = processEvents(EVENT_WAIT_INFINITE, EVENT_TYPE_CONN);
      if (event.type == EVENT_TYPE_CONN) {
        switch (event.conn.opType) {
        case CONNOP_CONNECT:
          // connection established
          if (!connected) {
            connected = (event.conn.result > 0);
            if (connected) {
              memset(buffer, 0, sizeof(buffer));
              maConnRead(conn, buffer, sizeof(buffer));
            } else {
              logPrint("Connection error\n");
              _runMode = init_state;
            }
          }
          break;
        case CONNOP_READ:
          // connRead completed
          if (event.conn.result > 0) {
            size = event.conn.result;
            if (result == NULL) {
              result = (char *)tmp_alloc(size + 1);
              memcpy(result, buffer, size);
              length = size;
            } else {
              result = (char *)tmp_realloc(result, length + size + 1);
              memcpy(result + length, buffer, size);
              length += size;
            }
            result[length] = 0;
            memset(buffer, 0, sizeof(buffer));
            maConnRead(conn, buffer, sizeof(buffer));
          } else {
            // no more data
            _runMode = init_state;
          }
          break;
        default:
          logPrint("Connection error\n");
          _runMode = init_state;
        }
      }
    }
    logPrint("Loaded in %d msecs\n", maGetMilliSecondCount() - now);
  }

  maConnClose(conn);
  return result;
}