サーバーに送信したいメッセージをキューに追加します。
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呼び出しで正しく印刷されます。ログインは完全に正常に機能します。データがイベントメソッドの外部に送信されると毎回失敗するように見え、サーバーが破損したデータを取得したかのように動作するイベントメソッドにある場合は失敗することがあります。
これはどのように行われることになっていますか?
ありがとうございました。