00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "mmstools/mmstcpserverthread.h"
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <string.h>
00037 #include <mmstools/tools.h>
00038
00039 MMSTCPServerThread::MMSTCPServerThread(MMSServerInterface *interface, int s, string identity) : MMSThread(identity) {
00040 this->interface = interface;
00041 this->s = s;
00042 this->request_buffer = "";
00043 this->answer_buffer = "";
00044 }
00045
00046 bool MMSTCPServerThread::setSocket(int s) {
00047 this->s = s;
00048 return true;
00049 }
00050
00051 void MMSTCPServerThread::threadMain() {
00052 char mybuf[4096+1];
00053 int len, from;
00054
00055 DEBUGMSG("MMSTCPServerThread", "process TCP Request");
00056
00057
00058 if (!this->s) return;
00059 if (!this->interface) {
00060 close(this->s);
00061 this->s=-1;
00062 return;
00063 }
00064
00065
00066 this->request_buffer = "";
00067 do {
00068 if ((len = recv(this->s, mybuf, sizeof(mybuf)-1, 0))<0) {
00069 close(this->s);
00070 this->s=-1;
00071 return;
00072 }
00073 if (len>0) {
00074 mybuf[len]=0;
00075 this->request_buffer+= mybuf;
00076 }
00077 } while ((len>0)&&(mybuf[len-1]!=0));
00078
00079
00080 this->answer_buffer = "";
00081 if (!this->interface->processRequest(&this->request_buffer, &this->answer_buffer)) {
00082 close(this->s);
00083 this->s=-1;
00084 return;
00085 }
00086
00087
00088 from = 0;
00089 do {
00090 strcpy(mybuf, (this->answer_buffer.substr(from, sizeof(mybuf)-1)).c_str());
00091 if (!*mybuf) break;
00092 if ((len = send(this->s, mybuf, strlen(mybuf), MSG_NOSIGNAL))<0) {
00093 close(this->s);
00094 this->s=-1;
00095 return;
00096 }
00097 from+=len;
00098 } while (len>0);
00099 send(this->s, "\0", 1, 0);
00100
00101 close(this->s);
00102 this->s=-1;
00103 }