简体   繁体   English

NSOutputStream,NSStreamEventHasSpaceAvailable事件通过后如何正确输出数据?

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

I add messages I want to send to a server to a queue: 我将要发送到服务器的消息添加到队列中:

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

When I get the event NSStreamEventHasSpaceAvailable I send the first message in the queue then remove it so the next message is ready. 当我收到事件NSStreamEventHasSpaceAvailable时,我发送队列中的第一条消息,然后将其删除,以便准备好下一条消息。 If the queue if empty I set a flag so I can send the next message straight away without adding it to the queue because the stream is supposed to be ready and the queue is empty. 如果队列为空,则设置一个标志,这样我就可以立即发送下一条消息而无需将其添加到队列中,因为该流应该已经准备好并且队列为空。

This is the code after getting the event: 这是获取事件后的代码:

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;

The login works fine. 登录正常。 After the login is complete the program starts using the queue to send messages when needed. 登录完成后,程序将在需要时开始使用队列发送消息。

Here the the code which adds data to the end of the queue: 这是将数据添加到队列末尾的代码:

- (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
}

The problem is the server does not recognise the data which is sent though the queue. 问题是服务器无法识别通过队列发送的数据。 The data is printed correctly at the printf calls. 数据在printf调用中正确打印。 The login works absolutely fine. 登录工作正常。 It appears to fail each time if the data is sent outside the event method and fails some of the time when it is in the event method where the server will act as if it got corrupted data. 如果数据是从事件方法外部发送的,则每次都会失败,而在事件方法中的某些时间,服务器将好像已损坏的数据一样,将失败。

How is this supposed to be done? 应该怎么做?

Thank you. 谢谢。

需要删除const,我将strlen添加到maxLength。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM