/[svn]/liblscp/trunk/src/client.c
ViewVC logotype

Diff of /liblscp/trunk/src/client.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 171 by capela, Mon Jul 5 16:26:44 2004 UTC revision 177 by capela, Tue Jul 6 14:06:17 2004 UTC
# Line 40  static lscp_status_t    _lscp_client_evt Line 40  static lscp_status_t    _lscp_client_evt
40  static void _lscp_client_evt_proc ( void *pvClient )  static void _lscp_client_evt_proc ( void *pvClient )
41  {  {
42      lscp_client_t *pClient = (lscp_client_t *) pvClient;      lscp_client_t *pClient = (lscp_client_t *) pvClient;
43      char  achBuffer[LSCP_BUFSIZ];      
44      int   cchBuffer;      fd_set fds;                         // File descriptor list for select().
45        int    fd, fdmax;                   // Maximum file descriptor number.
46        struct timeval tv;                  // For specifying a timeout value.
47        int    iSelect;                     // Holds select return status.
48        int    iTimeout;
49        
50        char   achBuffer[LSCP_BUFSIZ];
51        int    cchBuffer;
52      const char *pszSeps = ":\r\n";      const char *pszSeps = ":\r\n";
53      char *pszToken;      char * pszToken;
54      char *pch;      char * pch;
55      int   cchToken;      int     cchToken;
56      lscp_event_t event;      lscp_event_t event;
57    
58  #ifdef DEBUG  #ifdef DEBUG
# Line 53  static void _lscp_client_evt_proc ( void Line 60  static void _lscp_client_evt_proc ( void
60  #endif  #endif
61    
62      while (pClient->evt.iState) {      while (pClient->evt.iState) {
63    
64            // Prepare for waiting on select...
65            fd = (int) pClient->evt.sock;
66            FD_ZERO(&fds);
67            FD_SET((unsigned int) fd, &fds);
68            fdmax = fd;
69    
70            // Use the timeout (x10) select feature ...
71            iTimeout = 10 * pClient->iTimeout;
72            if (iTimeout > 1000) {
73                tv.tv_sec = iTimeout / 1000;
74                iTimeout -= tv.tv_sec * 1000;
75            }
76            else tv.tv_sec = 0;
77            tv.tv_usec = iTimeout * 1000;
78    
79          // Wait for event...          // Wait for event...
80          cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);          iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);
81          if (cchBuffer > 0) {          if (iSelect > 0 && FD_ISSET(fd, &fds)) {
82              // Make sure received buffer it's null terminated.              // May recv now...
83              achBuffer[cchBuffer] = (char) 0;              cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);
84              // Parse for the notification event message...              if (cchBuffer > 0) {
85              pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".                  // Make sure received buffer it's null terminated.
86              if (strcasecmp(pszToken, "NOTIFY") == 0) {                  achBuffer[cchBuffer] = (char) 0;
87                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                  // Parse for the notification event message...
88                  event    = lscp_event_from_text(pszToken);                  pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
89                  // And pick the rest of data...                  if (strcasecmp(pszToken, "NOTIFY") == 0) {
90                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
91                  cchToken = (pszToken == NULL ? 0 : strlen(pszToken));                      event    = lscp_event_from_text(pszToken);
92                  // Double-check if we're really up to it...                      // And pick the rest of data...
93                  if (pClient->events & event) {                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
94                      // Invoke the client event callback...                      cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
95                      if ((*pClient->pfnCallback)(                      // Double-check if we're really up to it...
96                              pClient,                      if (pClient->events & event) {
97                              event,                          // Invoke the client event callback...
98                              pszToken,                          if ((*pClient->pfnCallback)(
99                              cchToken,                                  pClient,
100                              pClient->pvData) != LSCP_OK) {                                  event,
101                          pClient->evt.iState = 0;                                  pszToken,
102                                    cchToken,
103                                    pClient->pvData) != LSCP_OK) {
104                                pClient->evt.iState = 0;
105                            }
106                      }                      }
107                  }                  }
108                } else {
109                    lscp_socket_perror("_lscp_client_evt_proc: recv");
110                    pClient->evt.iState = 0;
111              }              }
112          } else {          }   // Check if select has in error.
113              lscp_socket_perror("_lscp_client_evt_proc: recv");          else if (iSelect < 0) {
114                lscp_socket_perror("_lscp_client_call: select");
115              pClient->evt.iState = 0;              pClient->evt.iState = 0;
116          }          }
117            
118            // Finally, always signal the event.
119            lscp_cond_signal(pClient->cond);
120      }      }
121    
122  #ifdef DEBUG  #ifdef DEBUG
# Line 162  static lscp_status_t _lscp_client_evt_re Line 196  static lscp_status_t _lscp_client_evt_re
196          return LSCP_FAILED;          return LSCP_FAILED;
197      }      }
198    
199        // Wait on response.
200        lscp_cond_wait(pClient->cond, pClient->mutex);
201        
202      // Update as naively as we can...      // Update as naively as we can...
203      if (iSubscribe)      if (iSubscribe)
204          pClient->events |=  event;          pClient->events |=  event;
# Line 313  lscp_client_t* lscp_client_create ( cons Line 350  lscp_client_t* lscp_client_create ( cons
350    
351      // Initialize the transaction mutex.      // Initialize the transaction mutex.
352      lscp_mutex_init(pClient->mutex);      lscp_mutex_init(pClient->mutex);
353        lscp_cond_init(pClient->cond);
354    
355      // Finally we've some success...      // Finally we've some success...
356      return pClient;      return pClient;
# Line 399  lscp_status_t lscp_client_destroy ( lscp Line 437  lscp_status_t lscp_client_destroy ( lscp
437      // Last but not least, free good ol'transaction mutex.      // Last but not least, free good ol'transaction mutex.
438      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
439      lscp_mutex_destroy(pClient->mutex);      lscp_mutex_destroy(pClient->mutex);
440        lscp_cond_destroy(pClient->cond);
441    
442      free(pClient);      free(pClient);
443    

Legend:
Removed from v.171  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC