簡體   English   中英

NSOutputStream,NSStreamEventHasSpaceAvailable事件通過后如何正確輸出數據?

[英]NSOutputStream, how to output data properly after the NSStreamEventHasSpaceAvailable event has passed?

我將要發送到服務器的消息添加到隊列中:

typedef struct StreamOutputQueue{
    char message[513];
    struct StreamOutputQueue * next;
} StreamOutputQueue;

當我收到事件NSStreamEventHasSpaceAvailable時,我發送隊列中的第一條消息,然后將其刪除,以便准備好下一條消息。 如果隊列為空,則設置一個標志,這樣我就可以立即發送下一條消息而無需將其添加到隊列中,因為該流應該已經准備好並且隊列為空。

這是獲取事件后的代碼:

case NSStreamEventHasSpaceAvailable:
            NSLog(@"Space Available!");
            if (login_start) { //Login
                range = [nick_field.text rangeOfString: @" "]; //Find space in nickname text
                if (range.location != NSNotFound) { //Found so include only the text up to the space.
                    used_nick = [nick_field.text substringToIndex: range.location];
                }else{ //Else include it all
                    used_nick = nick_field.text;
                }
                [irc_output_stream write:(const uint8_t *)[[NSString stringWithFormat:@"USER %@ * * :%@ \r\nNICK %@\r\n", used_nick, nick_field.text,used_nick,nil] UTF8String] maxLength:1024]; //Send USER and NICK IRC commands
                login_start = NO; //Login done.
            }else if (output_queue){ //Queue exists
                printf("OUTPUT QUEUE HAS DATA - %s\n",output_queue->message);
                [irc_output_stream write: (const uint8_t *)output_queue->message maxLength:512]; //Send message to server.
                StreamOutputQueue * next = output_queue->next;
                free(output_queue);
                output_queue = next; //Queue pointer points to next node.
                space_available = NO;
            }else{
                space_available = YES; //Nothing sent, space available for immediate data delivery to server. 
            }
            break;

登錄正常。 登錄完成后,程序將在需要時開始使用隊列發送消息。

這是將數據添加到隊列末尾的代碼:

- (void) appendToOutputQueue: (char *) message{
    if (space_available) { //Space available with no queue so send the next one now.
        printf("SPACE AVAILABLE NO QUEUE - %s",message);
        [irc_output_stream write: (const uint8_t *)message maxLength:512];
        space_available = NO; //Wait until space is available again
        return; //Do not continue to add to queue
    }
    //Add to queue
    StreamOutputQueue * new;
    new = malloc(sizeof(*new)); //Allocate new node
    new->next = NULL; //Next must be null to signify end
    strcpy(new->message,message); //Copy message data
    if (output_queue) { //If the queue exists add the node to the end
        output_queue_end->next = new;
    }else{ //Else make the queue start at this node
        output_queue = new;
    }
    output_queue_end = new; //The end node is now this one
}

問題是服務器無法識別通過隊列發送的數據。 數據在printf調用中正確打印。 登錄工作正常。 如果數據是從事件方法外部發送的,則每次都會失敗,而在事件方法中的某些時間,服務器將好像已損壞的數據一樣,將失敗。

應該怎么做?

謝謝。

需要刪除const,我將strlen添加到maxLength。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM