Právě prohlížíš: socket_fns.h
Zpět do složkyStáhnout
  1. bool readline(const int *con, char* string); //Reads until \n character is received
  2. void sendMessage(const char*msg, const int*con); //Sends input strin g with \r\n ending
  3. void byteMessage(unsigned char byte, const int*con); //Sends one byte message. Just shortcut function
  4. void ardMessage(const char* bytes,const char length, const int*con);
  5.  
  6. void sendMessage(const char*msg, const int*con) {
  7. unsigned char len;
  8. len=strlen(msg);
  9. //std::cout<<(int)len<<'\n';
  10. //if(len>sizeof(msg))
  11. // len=sizeof(msg);
  12. //std::cout<<"Message: "<<(int)len<<"-"<<msg<<'\n';
  13. write(*con, msg, len);
  14. write(*con,"\r\n",2);
  15. return;
  16. }
  17. void byteMessage(unsigned char byte, const int*con) {
  18. char j = 1;
  19. write(*con, &j, 1);
  20. write(*con, &byte,1);
  21. return;
  22. }
  23. void ardMessage(const char* bytes, const char length, const int*con) {
  24. write(*con, &length,1);
  25. write(*con, bytes,(int)length);
  26. return;
  27. }
  28. bool readline(const int *con, char* string) {
  29. char in[1];
  30. bzero(string,BUFF_SIZE);
  31. int pos = 0;
  32. while(pos<BUFF_SIZE-1) {
  33. if(read(*con,in,1)<=0)
  34. return false;
  35. if(in[0]=='\r')
  36. continue;
  37. if(in[0]=='\n')
  38. break;
  39. string[pos]=in[0];
  40. pos++;
  41. }
  42. //printf("Posledni znak: %i\n",string[pos-1]);
  43. //string[pos-1]='\0';
  44. return true;
  45. }
  46. void setTimeout(const int* con, int secs) {
  47. struct timeval timeout;
  48. timeout.tv_sec = secs;
  49. timeout.tv_usec = 0;
  50.  
  51. if (setsockopt (*con, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
  52. printf("ERROR: setsockopt failed\n");
  53.  
  54. if (setsockopt (*con, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
  55. printf("ERROR: setsockopt failed\n");
  56. return;
  57. }
  58. bool dataReady(const int* fd, const int us)
  59. {
  60. fd_set fds;
  61. FD_ZERO(&fds);
  62. FD_SET(*fd, &fds);
  63. timeval timeout;
  64. timeout.tv_sec = 0;
  65. timeout.tv_usec = us;
  66. return select((*fd)+1, &fds, NULL, NULL, &timeout) > 0;
  67. }
  68.  
  69. int data_ready(const int*fd)
  70. /* Returns number of bytes available or -1 when connection is down*/
  71. {
  72. int ret = -1;
  73. printf("Checking data -- ");
  74. fd_set fds;
  75. FD_ZERO(&fds);
  76. FD_SET(*fd, &fds);
  77. timeval timeout;
  78. timeout.tv_sec = 0;
  79. timeout.tv_usec = 0;
  80. ret = select((*fd)+1, NULL, &fds, NULL, &timeout); //Are there any bytes ready to be read?
  81. if (ret > 0)
  82. {
  83. ioctl(*fd, FIONREAD, &ret); //How many bytes are ready to be read?
  84. if (ret > 0)
  85. {
  86. printf("%d bytes ready!\n", ret);
  87. }
  88. else //If there are no bytes ready to be read, although there should be, the connection is down. Return -1 for error.
  89. {
  90. printf("disconnected!\n");
  91. ret = -1;
  92. }
  93. }
  94. else {
  95. printf("no bytes.\n");
  96. }
  97.  
  98. return ret;
  99. }
  100. bool is_alive(const int*con)
  101. {
  102. return data_ready(con) >= 0; //if data_ready() returned -1, the connection is down
  103. }
  104.  
  105.  
Parsed using GeSHi 1.0.8.11 at 0.018