Example #1
0
void _ortc_send(ortc_context* context, char* channel, char* message){
  int i;
  size_t len;
  char *hash = _ortc_get_channel_permission(context, channel);
  char messageId[9], sParts[15], sMessageCount[15];
    int messageCount = 0;
  char* messagePart, *m;
  size_t parts = strlen(message) / ORTC_MAX_MESSAGE_SIZE;

  _ortc_random_string(messageId, 9);
  if(strlen(message) % ORTC_MAX_MESSAGE_SIZE > 0)
    parts++;
  sprintf(sParts, "%d", (int)parts);

  for(i=0; i<parts; i++){
    size_t messageSize;
    char *messageR;
    messageSize = strlen(message) - i * ORTC_MAX_MESSAGE_SIZE;
    if(messageSize > ORTC_MAX_MESSAGE_SIZE)
      messageSize = ORTC_MAX_MESSAGE_SIZE;
    
    messageCount = i + 1;    
    
    sprintf(sMessageCount, "%d", messageCount);

    messagePart = (char*)malloc(messageSize+1);
    if(messagePart==NULL){
      _ortc_exception(context, "malloc() failed in ortc send!");
      return;
    }
    memcpy(messagePart, message + i * ORTC_MAX_MESSAGE_SIZE, messageSize);
    messagePart[messageSize] = '\0';

    messageR = _ortc_escape_sequences_before(messagePart);
  
    len = 15 + strlen(context->appKey) + strlen(context->authToken) + strlen(channel) + strlen(hash) + strlen(messageId) + strlen(sParts) + strlen(sMessageCount) + strlen(messageR); 
    m = (char*)malloc(len + 1);
    if(m == NULL){
      _ortc_exception(context, "malloc() failed in ortc send!");
      free(messagePart);
      free(messageR);
      return;
    }
    snprintf(m, len, "\"send;%s;%s;%s;%s;%s_%d-%d_%s\"", context->appKey, context->authToken, channel, hash, messageId, messageCount, (int)parts, messageR);
    free(messagePart);
    free(messageR);
    _ortc_send_message(context, m);
  }
}
Example #2
0
void _ortc_subscribe(ortc_context* context, char* channel, int subscribeOnReconnected, int toBeSaved, void (*onMessage)(ortc_context*, char*, char*)){
  char *hash = _ortc_get_channel_permission(context, channel);
  int len = 16 + strlen(context->appKey) + strlen(context->authToken) + strlen(channel) + strlen(hash);
  char *subscribeCommand = (char*)malloc(len + 1);

  if(subscribeCommand == NULL){
    _ortc_exception(context,  "malloc() failed in ortc subscribe");
    return;
  }
  subscribeOnReconnected = (subscribeOnReconnected>0)?1:0;
  if(toBeSaved)
    _ortc_dlist_insert(context->channels, channel, NULL, NULL, subscribeOnReconnected, (void(*)(ortc_context*, char*, char*))onMessage);
  snprintf(subscribeCommand, len, "\"subscribe;%s;%s;%s;%s\"", context->appKey, context->authToken, channel, hash);
  _ortc_send_command(context, subscribeCommand);
}