/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/network/lscpserver.cpp

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

revision 1471 by schoenebeck, Mon Nov 5 13:56:26 2007 UTC revision 1481 by senoner, Wed Nov 14 23:42:15 2007 UTC
# Line 25  Line 25 
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26  #include "lscpevent.h"  #include "lscpevent.h"
27    
28    #if defined(WIN32)
29    #else
30  #include <fcntl.h>  #include <fcntl.h>
31    #endif
32    
33  #if ! HAVE_SQLITE3  #if ! HAVE_SQLITE3
34  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
# Line 129  LSCPServer::LSCPServer(Sampler* pSampler Line 132  LSCPServer::LSCPServer(Sampler* pSampler
132  }  }
133    
134  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
135    #if defined(WIN32)
136        if (hSocket >= 0) closesocket(hSocket);
137    #else
138      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
139    #endif
140  }  }
141    
142  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
# Line 230  int LSCPServer::WaitUntilInitialized(lon Line 237  int LSCPServer::WaitUntilInitialized(lon
237  }  }
238    
239  int LSCPServer::Main() {  int LSCPServer::Main() {
240            #if defined(WIN32)
241            WSADATA wsaData;
242            int iResult;
243            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
244            if (iResult != 0) {
245                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
246                    exit(EXIT_FAILURE);
247            }
248            #endif
249      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
250      if (hSocket < 0) {      if (hSocket < 0) {
251          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 243  int LSCPServer::Main() { Line 259  int LSCPServer::Main() {
259              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
260                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
261                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
262                        #if defined(WIN32)
263                        closesocket(hSocket);
264                        #else
265                      close(hSocket);                      close(hSocket);
266                        #endif
267                      //return -1;                      //return -1;
268                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
269                  }                  }
# Line 325  int LSCPServer::Main() { Line 345  int LSCPServer::Main() {
345                  continue; //Nothing try again                  continue; //Nothing try again
346          if (retval == -1) {          if (retval == -1) {
347                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
348                    #if defined(WIN32)
349                    closesocket(hSocket);
350                    #else
351                  close(hSocket);                  close(hSocket);
352                    #endif
353                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
354          }          }
355    
# Line 337  int LSCPServer::Main() { Line 361  int LSCPServer::Main() {
361                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
362                  }                  }
363    
364                    #if defined(WIN32)
365                    u_long nonblock_io = 1;
366                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
367                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
368                      exit(EXIT_FAILURE);
369                    }
370            #else          
371                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
372                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
373                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
374                  }                  }
375                    #endif
376    
377                  // Parser initialization                  // Parser initialization
378                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 399  void LSCPServer::CloseConnection( std::v Line 431  void LSCPServer::CloseConnection( std::v
431          NotifyMutex.Lock();          NotifyMutex.Lock();
432          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
433          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
434            #if defined(WIN32)
435            closesocket(socket);
436            #else
437          close(socket);          close(socket);
438            #endif
439          NotifyMutex.Unlock();          NotifyMutex.Unlock();
440  }  }
441    
# Line 478  bool LSCPServer::GetLSCPCommand( std::ve Line 514  bool LSCPServer::GetLSCPCommand( std::ve
514          char c;          char c;
515          int i = 0;          int i = 0;
516          while (true) {          while (true) {
517                    #if defined(WIN32)
518                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
519                    #else
520                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
521                    #endif
522                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection
523                          CloseConnection(iter);                          CloseConnection(iter);
524                          break;                          break;
# Line 493  bool LSCPServer::GetLSCPCommand( std::ve Line 533  bool LSCPServer::GetLSCPCommand( std::ve
533                          }                          }
534                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
535                  }                  }
536                    #if defined(WIN32)
537                    if (result == SOCKET_ERROR) {
538                        int wsa_lasterror = WSAGetLastError();
539                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
540                                    return false;
541                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));  
542                            CloseConnection(iter);
543                            break;
544                    }
545                    #else
546                  if (result == -1) {                  if (result == -1) {
547                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
548                                  return false;                                  return false;
# Line 531  bool LSCPServer::GetLSCPCommand( std::ve Line 581  bool LSCPServer::GetLSCPCommand( std::ve
581                          CloseConnection(iter);                          CloseConnection(iter);
582                          break;                          break;
583                  }                  }
584                    #endif
585          }          }
586          return false;          return false;
587  }  }

Legend:
Removed from v.1471  
changed lines
  Added in v.1481

  ViewVC Help
Powered by ViewVC