📄 gena_client.c
字号:
return UPNP_E_OUTOF_MEMORY; } sprintf(request,"SID: %s\r\n\r\n",sub_copy.ActualSID); return_code=transferHTTP("UNSUBSCRIBE",request,strlen(request),&response,sub_copy.EventURL); free(request); free_client_subscription(&sub_copy); if (return_code!=HTTP_SUCCESS) { return return_code; } return_code=parse_http_response(response,&parsed_response,strlen(response)); if (return_code==HTTP_SUCCESS) { if (strncasecmp(parsed_response.status.status_code.buff,"200", strlen("200"))) return_code=GENA_E_UNSUBSCRIBE_UNACCEPTED; else return_code=GENA_SUCCESS; free_http_message(&parsed_response); } free(response); return return_code; }//********************************************************//* Name: genaSubscribe//* Description: Subscribes to a PublisherURL//* First Validates & locks handle (this is currently necessary to prevent client//* from recieving events, NOTE: this blocks all events and API//* calls until it finishes, worst case 30 seconds) //* Sends SUBSCRIBE http request to service//* processes request//* Adds a Subscription to the clients subscription list, if service responds with OK//* In: UpnpClient_Handle client_handle//* char * PublisherURL (NULL Terminated, of the form : "http://134.134.156.80:4000/RedBulb/Event")//* int * TimeOut (requested Duration, if -1, then "infinite".//* Out: int * TimeOut (actual Duration granted by Service, -1 for infinite)//* SID out_sid (sid of subscription, memory passed in by caller)//* Return Codes: UPNP_E_SUCCESS : if service responds OK//* Error Codes: UPNP_E_OUTOF_MEMORY//* UPNP_E_SUBSCRIBE_UNACCEPTED : if service responds with other than OK //* UPNP_E_NETWORK_ERROR: error connecting (see upnp.h)//* //* //********************************************************int genaSubscribe(UpnpClient_Handle client_handle, char * PublisherURL, int * TimeOut, Upnp_SID out_sid){ int headers_size=0; char timeout[MAX_SECONDS]; char * headers=NULL; char * response=NULL; int return_code=GENA_SUCCESS; client_subscription * newSubscription=NULL; uuid_t uuid; char temp_sid[SID_SIZE]; char * ActualSID=NULL; token temp_headerValue; http_message parsed_response; struct Handle_Info *handle_info; char * EventURL=NULL; DBGONLY(UpnpPrintf(UPNP_INFO,GENA,__FILE__,__LINE__,"GENA SUBSCRIBE BEGIN")); HandleLock(); //validate handle if ( (GetHandleInfo(client_handle,&handle_info)!=HND_CLIENT)) { HandleUnlock(); return GENA_E_BAD_HANDLE; } HandleUnlock(); if (TimeOut==NULL) sprintf(timeout,"%d",DEFAULT_TIMEOUT); else if ( (*TimeOut) >= 0) sprintf(timeout,"%d",(*TimeOut)); else strcpy(timeout,"infinite"); headers_size=strlen("CALLBACK: <http:///>\r\n") + strlen(LOCAL_HOST) +1+ MAX_PORT_SIZE + strlen("NT: upnp:event\r\n") + strlen("TIMEOUT: Second-\r\n\r\n") + MAX_SECONDS +1; headers=(char *) malloc(headers_size); if (headers==NULL) { HandleUnlock(); return UPNP_E_OUTOF_MEMORY; } sprintf(headers, "CALLBACK: <http://%s:%d/>\r\nNT: upnp:event\r\nTIMEOUT: Second-%s\r\n\r\n", LOCAL_HOST,LOCAL_PORT,timeout); SubscribeLock(); return_code=transferHTTP("SUBSCRIBE",headers,strlen(headers),&response,PublisherURL); free(headers); HandleLock(); if (return_code!=HTTP_SUCCESS) { HandleUnlock(); SubscribeUnlock(); DBGONLY(UpnpPrintf(UPNP_CRITICAL,GENA,__FILE__,__LINE__,"SUBSCRIBE FAILED in transfer error code: %d returned\n", return_code)); return return_code; } if ( (GetHandleInfo(client_handle,&handle_info)!=HND_CLIENT)) { HandleUnlock(); SubscribeUnlock(); free(response); return GENA_E_BAD_HANDLE; } //parse response return_code=parse_http_response(response,&parsed_response,strlen(response)); if (return_code==HTTP_SUCCESS) { return_code=GENA_SUCCESS; if (!strncasecmp(parsed_response.status.status_code.buff,"200",strlen("200"))) { if (search_for_header(&parsed_response,"SID",&temp_headerValue)) { ActualSID=(char * ) malloc(temp_headerValue.size+1); if (ActualSID==NULL) return_code= UPNP_E_OUTOF_MEMORY; else { //store ACTUAL SID and generate client sid memcpy(ActualSID,temp_headerValue.buff, temp_headerValue.size); ActualSID[temp_headerValue.size]=0; uuid_generate(uuid); uuid_unparse(uuid,temp_sid); sprintf((out_sid),"uuid:%s",temp_sid); } } else return_code= GENA_E_BAD_RESPONSE; if (search_for_header(&parsed_response,"TIMEOUT",&temp_headerValue)) { if (sscanf(temp_headerValue.buff,"Second-%d",TimeOut)!=1 ) { if (!strncasecmp(temp_headerValue.buff, "Second-infinite", strlen("Second-infinite"))) (*TimeOut)=-1; else return_code= GENA_E_BAD_RESPONSE; } } else return_code= GENA_E_BAD_RESPONSE; EventURL=(char*) malloc (strlen(PublisherURL)+1); if (EventURL==NULL) return_code=UPNP_E_OUTOF_MEMORY; else strcpy(EventURL, PublisherURL); } else return_code=GENA_E_SUBSCRIPTION_UNACCEPTED; free_http_message(&parsed_response); } free(response); if (return_code==GENA_SUCCESS) { newSubscription=(client_subscription *) malloc(sizeof (client_subscription)); if ( newSubscription==NULL) return_code=UPNP_E_OUTOF_MEMORY; else { newSubscription->EventURL=EventURL; newSubscription->ActualSID=ActualSID; strcpy(newSubscription->sid,out_sid); newSubscription->RenewEventId=-1; newSubscription->next=handle_info->ClientSubList; handle_info->ClientSubList=newSubscription; //schedule expire event return_code=ScheduleGenaAutoRenew(client_handle,(*TimeOut),newSubscription); } } if (return_code!=GENA_SUCCESS) { if (ActualSID) free(ActualSID); if (EventURL) free(EventURL); if (newSubscription) free(newSubscription); } HandleUnlock(); SubscribeUnlock(); return return_code;}//********************************************************//* Name: genaRenewSubscription//* Description: Renews a SID//* First Validates the SID and client_handle//* Locks the Handle//* copies the subscription//* UnLocks the Handle//* Sends RENEW (modified SUBSCRIBE) http request to service//* Locks Handle//* processes request//* Unlocks Handle//* In: UpnpClient_Handle client_handle//* SID in_sid //* int * TimeOut (requested duration)//* Out: int * TimeOut (actual duration)//* Return Codes: UPNP_E_SUCCESS : if service responds OK//* Error Codes: UPNP_E_OUTOF_MEMORY: subscription may or may not be removed from client side.//* UPNP_E_UNSUBSCRIBE_UNACCEPTED : if service responds with other than OK (Note: subscription is //* removed from client side)//* //* //********************************************************int genaRenewSubscription(UpnpClient_Handle client_handle, const Upnp_SID in_sid, int * TimeOut){ int headers_size=0; char timeout[MAX_SECONDS]; char * headers; char * response; int return_code=GENA_SUCCESS; client_subscription * sub; client_subscription sub_copy; upnp_timeout * temp_event; struct Handle_Info *handle_info; void * temp; token temp_headerValue; http_message parsed_response; HandleLock(); //validate handle and sid if ( GetHandleInfo(client_handle,&handle_info)!=HND_CLIENT) { HandleUnlock(); return GENA_E_BAD_HANDLE; } if ( ( ( sub=GetClientSubClientSID(handle_info->ClientSubList,in_sid))==NULL)) { HandleUnlock(); return GENA_E_BAD_SID; } //remove old events if (RemoveTimerEvent(sub->RenewEventId,&temp,&GLOBAL_TIMER_THREAD)) { temp_event=(upnp_timeout *) temp; free_upnp_timeout(temp_event); } DBGONLY(UpnpPrintf(UPNP_INFO,GENA,__FILE__,__LINE__,"REMOVED AUTO RENEW EVENT")); sub->RenewEventId=-1; return_code=copy_client_subscription(sub,&sub_copy); HandleUnlock(); if (return_code!=HTTP_SUCCESS) return return_code; if (TimeOut==NULL) sprintf(timeout,"%d",1801); else if ( (*TimeOut) >= 0) sprintf(timeout,"%d",(*TimeOut)); else sprintf(timeout,"infinite"); headers_size=strlen("SID: \r\n") + strlen(sub_copy.ActualSID) + strlen("TIMEOUT: Second-\r\n\r\n") + MAX_SECONDS +1; headers=(char *) malloc(headers_size); if (headers==NULL) { return UPNP_E_OUTOF_MEMORY; } sprintf(headers, "SID: %s\r\nTIMEOUT: Second-%s\r\n\r\n",sub_copy.ActualSID, timeout); return_code=transferHTTP("SUBSCRIBE",headers,strlen(headers),&response,sub_copy.EventURL); free(headers); free_client_subscription(&sub_copy); HandleLock(); if ( GetHandleInfo(client_handle,&handle_info)!=HND_CLIENT) { HandleUnlock(); if (return_code==HTTP_SUCCESS) free(response); return GENA_E_BAD_HANDLE; } if (return_code!=HTTP_SUCCESS) { //network failure (remove client sub) RemoveClientSubClientSID(&handle_info->ClientSubList,in_sid); HandleUnlock(); return return_code; } //validate sid if ( ( ( sub=GetClientSubClientSID(handle_info->ClientSubList,in_sid))==NULL)) { HandleUnlock(); free(response); return GENA_E_BAD_SID; } //parse response return_code=parse_http_response(response,&parsed_response,strlen(response)); if (return_code==HTTP_SUCCESS) { if (!strncasecmp(parsed_response.status.status_code.buff,"200",strlen("200"))) { return_code=GENA_SUCCESS; //get SID if (search_for_header(&parsed_response,"SID",&temp_headerValue)) { free(sub->ActualSID); sub->ActualSID=(char * ) malloc(temp_headerValue.size+1); if (sub->ActualSID==NULL) return_code = UPNP_E_OUTOF_MEMORY; else { //store ACTUAL SID memcpy(sub->ActualSID,temp_headerValue.buff, temp_headerValue.size); sub->ActualSID[temp_headerValue.size]=0; } } else return_code= GENA_E_BAD_RESPONSE; //get Timeout if (search_for_header(&parsed_response,"TIMEOUT",&temp_headerValue)) { if (sscanf(temp_headerValue.buff,"Second-%d",TimeOut)!=1 ) { if (!strncasecmp(temp_headerValue.buff,"Second-infinite",strlen("Second-infinite"))) (*TimeOut)=-1; else return_code= GENA_E_BAD_RESPONSE; } } else return_code = GENA_E_BAD_RESPONSE; } else return_code=GENA_E_SUBSCRIPTION_UNACCEPTED; free_http_message(&parsed_response); } free(response); if (return_code==GENA_SUCCESS) return_code=ScheduleGenaAutoRenew(client_handle,(*TimeOut),sub); if (return_code!=GENA_SUCCESS) RemoveClientSubClientSID(&handle_info->ClientSubList,sub->sid); HandleUnlock(); return return_code;})#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -