OSDN Git Service

DHA20100105分ソース反映しました(Ticket#367)
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / module / protocol / protocol_module_sessionless.cpp
1 /*
2  * @file  protocol_module_sessionless.cpp
3  * @brief protocol module of any protocol.
4  * @brief this module never keep session persistence.
5  *
6  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
7  * Copyright (C) 2009  NTT COMWARE Corporation.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  **********************************************************************/
25 #include <boost/xpressive/xpressive.hpp>
26 #include <vector>
27 #include <list>
28 #include <algorithm>
29 #include <iostream>
30 #include <boost/asio/ip/tcp.hpp>
31 #include <boost/format.hpp>
32 #include "protocol_module_sessionless.h"
33 #include "utility.h"
34
35 namespace l7vs
36 {
37
38     const std::string protocol_module_sessionless::MODULE_NAME = "sessionless";
39     const int protocol_module_sessionless::THREAD_DIVISION_UP_STREAM = 0;
40     const int protocol_module_sessionless::THREAD_DIVISION_DOWN_STREAM = 1;
41
42     const int protocol_module_sessionless::END_FLAG_OFF = 0;
43     const int protocol_module_sessionless::END_FLAG_ON = 1;
44
45     const int protocol_module_sessionless::ACCEPT_END_FLAG_OFF = 0;
46     const int protocol_module_sessionless::ACCEPT_END_FLAG_ON = 1;
47
48     const int protocol_module_sessionless::SORRY_FLAG_ON = 1;
49     const int protocol_module_sessionless::SORRY_FLAG_OFF = 0;
50
51     const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_OFF = 0;
52     const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_ON = 1;
53
54     const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_OFF = 0;
55     const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_ON = 1;
56
57     const int protocol_module_sessionless::EDIT_DIVISION_NO_EDIT = 0;
58     const int protocol_module_sessionless::EDIT_DIVISION_EDIT = 1;
59
60     const int protocol_module_sessionless::FORWARDED_FOR_OFF = 0;
61     const int protocol_module_sessionless::FORWARDED_FOR_ON = 1;
62     //! constractor
63     protocol_module_sessionless::protocol_module_sessionless() :
64             http_protocol_module_base(MODULE_NAME), forwarded_for(FORWARDED_FOR_OFF)
65     {
66         sorry_uri.assign('\0');
67         sorry_uri[0] = '/';
68     }
69     //! destractor
70     protocol_module_sessionless::~protocol_module_sessionless()
71     {
72     }
73     //! tcp protocol support check
74     //! @return tcp support is true
75     //! @return tcp not-support is false
76     bool protocol_module_sessionless::is_tcp()
77     {
78         /*-------- DEBUG LOG --------*/
79         if (unlikely(LOG_LV_DEBUG == getloglevel()))
80         {
81             putLogDebug(100000, "in/out_function : bool protocol_module_sessionless::is_tcp() : "
82                         "return_value = true.", __FILE__, __LINE__ );
83         }
84         /*------DEBUG LOG END------*/
85         return true;
86     }
87
88     //! udp protocol support check
89     //! @return udp support is true
90     //! @return udp not-support is false
91     bool protocol_module_sessionless::is_udp()
92     {
93         /*-------- DEBUG LOG --------*/
94         if (unlikely(LOG_LV_DEBUG == getloglevel()))
95         {
96             putLogDebug(100001, "in/out_function : bool protocol_module_sessionless::is_udp() : "
97                         "return_value = false.", __FILE__, __LINE__ );
98         }
99         /*------DEBUG LOG END------*/
100         return false;
101     }
102
103     //! replication interval interrrupt
104     //! timer thread call this function. from virtualservice.
105     void protocol_module_sessionless::replication_interrupt()
106     {
107         /*-------- DEBUG LOG --------*/
108         if (unlikely(LOG_LV_DEBUG == getloglevel()))
109         {
110             putLogDebug(100002, "in/out_function : void protocol_module_sessionless::"
111                         "replication_interrupt().", __FILE__, __LINE__ );
112         }
113         /*------DEBUG LOG END------*/
114     }
115     //! initialize function. called from module control. module loaded call
116     //! @param[in]    realserver list iterator begin function object type
117     //!    @param[in]    realserver list iterator end function object type
118     //! @param[in]    realserver list iterator next function object type
119     //! @param[in]    realserver list mutex lock function object type.
120     //! @param[in]    realserver list mutex unlock function object type
121     void protocol_module_sessionless::initialize(rs_list_itr_func_type    inlist_begin,
122             rs_list_itr_func_type    inlist_end,
123             rs_list_itr_next_func_type    inlist_next,
124             boost::function< void( void ) >    inlist_lock,
125             boost::function< void( void ) >    inlist_unlock)
126     {
127         /*-------- DEBUG LOG --------*/
128         if (unlikely(LOG_LV_DEBUG == getloglevel()))
129         {
130             putLogDebug(100003, "in_function : void protocol_module_sessionless::initialize("
131                         "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
132                         "rs_list_itr_next_func_type inlist_next, boost::function< void( void ) > "
133                         "inlist_lock, boost::function< void( void ) > inlist_unlock).", __FILE__, __LINE__ );
134         }
135         /*------DEBUG LOG END------*/
136
137         //RealServer list begin function
138         rs_list_begin = inlist_begin;
139         //RealServer list end function
140         rs_list_end = inlist_end;
141         //RealServer list next function
142         rs_list_next = inlist_next;
143         //RealServer list lock funtion
144         rs_list_lock = inlist_lock;
145         //RealServer list unlock funtion
146         rs_list_unlock = inlist_unlock;
147
148         /*-------- DEBUG LOG --------*/
149         if (unlikely(LOG_LV_DEBUG == getloglevel()))
150         {
151             putLogDebug(100004, "out_function : void protocol_module_sessionless::initialize("
152                         "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
153                         "rs_list_itr_next_func_type inlist_next, boost::function< void( void ) > "
154                         "inlist_lock, boost::function< void( void ) > inlist_unlock).", __FILE__, __LINE__ );
155         }
156         /*-------- DEBUG LOG --------*/
157     }
158
159     //! finalize called from module control. module unloaded call.
160     void protocol_module_sessionless::finalize()
161     {
162         /*-------- DEBUG LOG --------*/
163         if (unlikely(LOG_LV_DEBUG == getloglevel()))
164         {
165             putLogDebug(100005, "in_function : void protocol_module_sessionless::finalize().", __FILE__,
166                         __LINE__ );
167         }
168         /*------DEBUG LOG END------*/
169
170         //RealServer list functions initializtion
171         //RealServer list begin function
172         rs_list_begin.clear();
173         //RealServer list end function
174         rs_list_end.clear();
175         //RealServer list next function
176         rs_list_next.clear();
177         //RealServer list lock function
178         rs_list_lock.clear();
179         //RealServer list unlock function
180         rs_list_unlock.clear();
181
182         /*-------- DEBUG LOG --------*/
183         if (unlikely(LOG_LV_DEBUG == getloglevel()))
184         {
185             putLogDebug(100006, "function : void protocol_module_sessionless::finalize() : "
186                         "rs_list_begin.clear(), rs_list_end.clear(), rs_list_next.clear(), rs_list_lock.clear(), rs_list_unlock.clear() end.", __FILE__,
187                         __LINE__ );
188         }
189         /*------DEBUG LOG END------*/
190
191         //Replication functions initializtion
192         //component memory allcate function
193         replication_pay_memory.clear();
194         //component memory lock function
195         replication_area_lock.clear();
196         //component memory unlock function
197         replication_area_unlock.clear();
198
199         /*-------- DEBUG LOG --------*/
200         if (unlikely(LOG_LV_DEBUG == getloglevel()))
201         {
202             putLogDebug(100007, "function : void protocol_module_sessionless::finalize() : "
203                         "replication_pay_memory.clear(), replication_area_lock.clear(), replication_area_unlock.clear() end.", __FILE__,
204                         __LINE__ );
205         }
206         /*------DEBUG LOG END------*/
207
208         //ScheduleModule's functions initializtion
209         schedule_tcp.clear();
210
211         /*-------- DEBUG LOG --------*/
212         if (unlikely(LOG_LV_DEBUG == getloglevel()))
213         {
214             putLogDebug(100008, "function : void protocol_module_sessionless::finalize() : "
215                         "schedule_tcp.clear(), schedule_udp.clear() end.", __FILE__,
216                         __LINE__ );
217         }
218         /*------DEBUG LOG END------*/
219
220         //Module's option initializtion
221         //forwarded_for
222         forwarded_for = FORWARDED_FOR_OFF;
223         //sorry-uri
224         sorry_uri.assign('\0');
225         /*-------- DEBUG LOG --------*/
226         if (unlikely(LOG_LV_DEBUG == getloglevel()))
227         {
228             putLogDebug(100009, "function : void protocol_module_sessionless::finalize() : "
229                         "forwarded_for = 0, sorry_uri.assign('\\0') end.", __FILE__,
230                         __LINE__ );
231         }
232         /*------DEBUG LOG END------*/
233         /*-------- DEBUG LOG --------*/
234         if (unlikely(LOG_LV_DEBUG == getloglevel()))
235         {
236             putLogDebug(100010, "out_function : void protocol_module_sessionless::finalize().", __FILE__,
237                         __LINE__ );
238         }
239         /*------DEBUG LOG END------*/
240
241
242         //logger functions initializtion
243         //log level getting function
244         getloglevel.clear();
245         //logger(Fatal)
246         putLogFatal.clear();
247         //logger(Error)
248         putLogError.clear();
249         //logger(Warn)
250         putLogWarn.clear();
251         //logger(Info)
252         putLogInfo.clear();
253         //logger(Debug)
254         putLogDebug.clear();
255     }
256
257     //! sorry support check
258     //! @return true sorry mode is supported.
259     //! @return false sorry mode is unsupported.
260     bool protocol_module_sessionless::is_use_sorry()
261     {
262         /*-------- DEBUG LOG --------*/
263         if (unlikely(LOG_LV_DEBUG == getloglevel()))
264         {
265             putLogDebug(100011, "in/out_function : bool protocol_module_sessionless::is_use_sorry() : "
266                         "return_value = true.", __FILE__, __LINE__ );
267         }
268         /*------DEBUG LOG END------*/
269         return true;
270     }
271
272     //! realserver list update event
273     void protocol_module_sessionless::handle_rslist_update()
274     {
275         /*-------- DEBUG LOG --------*/
276         if (unlikely(LOG_LV_DEBUG == getloglevel()))
277         {
278             putLogDebug(100012, "in/out_function : void protocol_module_sessionless::handle_rslist_update().", __FILE__,
279                         __LINE__ );
280         }
281         /*------DEBUG LOG END------*/
282     }
283
284     //! module parameter check.used by l7vsadm
285     //! @param[in]    module paramter string list
286     //! @return    result.flag true is parameter is noproblem.
287     //! @return result.flag false is paramter is problem.
288     protocol_module_base::check_message_result protocol_module_sessionless::check_parameter(const std::vector<
289             std::string>& args)
290     {
291         /*-------- DEBUG LOG --------*/
292         if (unlikely(LOG_LV_DEBUG == getloglevel()))
293         {
294             boost::format formatter("in_function : protocol_module_base::check_message_result "
295                                     "protocol_module_sessionless::check_parameter("
296                                     "const std::vector<std::string>& args) : args = %s.");
297             std::string argsdump;
298             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
299             {
300                 argsdump += *it;
301                 argsdump += " ";
302             }
303             formatter % argsdump;
304             putLogDebug(100013, formatter.str(), __FILE__, __LINE__ );
305         }
306         /*------DEBUG LOG END------*/
307         using namespace boost::xpressive;
308         //set check result true
309         check_message_result check_result;
310         check_result.flag = true;
311         bool forward_checked = false;
312         bool sorryuri_checked = false;
313         sregex    sorry_uri_regex
314         =    +(    '/' >>
315              *(    alpha |
316                 digit |
317                 ( set = ';', ':', '@', '&', '=' ) |
318                 ( set = '$', '-', '_', '.', '+' ) |
319                 ( set = '!', '*', '\'', '\(', ')', ',' ) |
320                 '%' >> repeat<2>(xdigit)));
321         typedef std::vector<std::string>::const_iterator vec_str_it;
322
323         try
324         {
325             vec_str_it it = args.begin();
326             vec_str_it it_end = args.end();
327             //loop option strings
328             for (; it != it_end; ++it)
329             {
330                 //option string = "-F"
331                 if (*it == "-F" || *it == "--forwarded-for")
332                 {
333                     //set forward flag  ON
334                     forward_checked = true;
335                 }
336                 //option string = "-S"
337                 else if (*it == "-S" || *it == "--sorry-uri")
338                 {
339                     //set sorryURI flag OFF
340                     if (!sorryuri_checked)
341                     {
342                         //next item exist
343                         if (++it != it_end)
344                         {
345                             if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--"))
346                             {
347                                 //set check result flag false
348                                 check_result.flag = false;
349                                 //set check result message
350                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
351                                 putLogError(100000, check_result.message, __FILE__, __LINE__ );
352                                 //loop break;
353                                 break;
354                             }
355                             //next option string's length > 127
356                             if (it->size() > MAX_OPTION_SIZE - 1)
357                             {
358                                 std::ostringstream ostr;
359                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
360
361                                 //set check result flag false
362                                 check_result.flag = false;
363                                 //set check result message
364                                 check_result.message = ostr.str();
365
366                                 putLogError(100001, check_result.message, __FILE__, __LINE__ );
367                                 //loop break;
368                                 break;
369                             }
370                             //next option string's length <= 127
371                             else
372                             {
373                                 //regex check
374                                 if (regex_match(*it, sorry_uri_regex ))
375                                 {
376                                     //check OK
377                                     //set sorryURI flag ON
378                                     sorryuri_checked = true;
379                                 }
380                                 //check NG
381                                 else
382                                 {
383                                     std::ostringstream ostr;
384                                     ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
385
386                                     //set check result flag false
387                                     check_result.flag = false;
388                                     //set check result message
389                                     check_result.message = ostr.str();
390                                     putLogError(100002, check_result.message, __FILE__, __LINE__ );
391                                     //loop break
392                                     break;
393                                 }
394                             }
395                         }
396                         //next item is not exist
397                         else
398                         {
399                             //set check flag false
400                             check_result.flag = false;
401                             //set check result message
402                             check_result.message = "You have to set option value '-S/--sorry-uri'.";
403                             putLogError(100003, check_result.message, __FILE__,
404                                         __LINE__ );
405                             //loop break
406                             break;
407                         }
408                     }
409                     //sorryURI flag = ON
410                     else
411                     {
412                         //set check result flag false
413                         check_result.flag = false;
414                         //set check result message
415                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
416                         putLogError(100004, check_result.message, __FILE__,
417                                     __LINE__ );
418                         //loop break
419                         break;
420                     }
421                 }
422                 //other option string
423                 else
424                 {
425                     //set check result flag false
426                     check_result.flag = false;
427                     //set check result message
428                     check_result.message = "Option error.";
429                     putLogError(100005, check_result.message, __FILE__, __LINE__ );
430                     //loop break
431                     break;
432                 }
433             }
434         }
435         catch (const std::exception& ex)
436         {
437             check_result.flag = false;
438             std::cerr << "protocol_module_sessionless::check_parameter() : exception : error = " << ex.what() << "." << std::endl;
439             boost::format formatter("function : protocol_module_base::check_message_result "
440                                     "protocol_module_sessionless::check_parameter() exception : "
441                                     "error = %s.");
442             formatter % ex.what();
443             putLogError(100006, formatter.str(), __FILE__, __LINE__ );
444         }
445         catch (...)
446         {
447             check_result.flag = false;
448             std::cerr << "protocol_module_sessionless::check_parameter() : Unknown exception." << std::endl;
449             putLogError(100007, "function : protocol_module_base::check_message_result "
450                         "protocol_module_sessionless::check_parameter() : "
451                         "Unknown exception.", __FILE__, __LINE__ );
452         }
453
454         /*-------- DEBUG LOG --------*/
455         if (unlikely(LOG_LV_DEBUG == getloglevel()))
456         {
457             boost::format formatter("out_function : protocol_module_base::check_message_result "
458                                     "protocol_module_sessionless::check_parameter("
459                                     "const std::vector<std::string>& args) : return_value = ("
460                                     "check_message_result.flag = %d, check_message_result.message = %s).");
461             formatter % check_result.flag % check_result.message;
462             putLogDebug(100014, formatter.str(), __FILE__, __LINE__ );
463         }
464         /*------DEBUG LOG END------*/
465         return check_result;
466     }
467
468     //! parameter set
469     //! @param[in] module paramter string list
470     //! @return    result.flag true is parameter is noproblem.
471     //! @return result.flag false is paramter is problem.
472     protocol_module_base::check_message_result protocol_module_sessionless::set_parameter(const std::vector<
473             std::string>& args)
474     {
475         /*-------- DEBUG LOG --------*/
476         if (unlikely(LOG_LV_DEBUG == getloglevel()))
477         {
478             boost::format formatter("in_function : protocol_module_base::check_message_result "
479                                     "protocol_module_sessionless::set_parameter("
480                                     "const std::vector<std::string>& args) : args = %s.");
481             std::string argsdump;
482             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
483             {
484                 argsdump += *it;
485                 argsdump += " ";
486             }
487             formatter % argsdump;
488             putLogDebug(100015, formatter.str(), __FILE__, __LINE__ );
489         }
490         /*------DEBUG LOG END------*/
491         using namespace boost::xpressive;
492         //set check result flag true
493         check_message_result check_result;
494         check_result.flag = true;
495         bool forward_checked = false;
496         bool sorryuri_checked = false;
497         sregex    sorry_uri_regex
498         =    +(    '/' >>
499              *(    alpha |
500                 digit |
501                 ( set = ';', ':', '@', '&', '=' ) |
502                 ( set = '$', '-', '_', '.', '+' ) |
503                 ( set = '!', '*', '\'', '\(', ')', ',' ) |
504                 '%' >> repeat<2>(xdigit)));
505
506         typedef std::vector<std::string>::const_iterator vec_str_it;
507
508         //set forwarded flag true
509         forwarded_for = 1;
510
511         try
512         {
513             vec_str_it it = args.begin();
514             vec_str_it it_end = args.end();
515             for (; it != it_end; ++it)
516             {
517                 //option string = "-F"
518                 if (*it == "-F" || *it == "--forwarded-for")
519                 {
520                     //set forwarded flag ON
521                     forward_checked = true;
522                 }
523                 //option string  = "-S"
524                 else if (*it == "-S" || *it == "--sorry-uri")
525                 {
526                     //sorryURI flag = OFF
527                     if (!sorryuri_checked)
528                     {
529                         //next item exist
530                         if (++it != it_end)
531                         {
532                             if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--"))
533                             {
534                                 //set check result flag false
535                                 check_result.flag = false;
536                                 //set check result message
537                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
538                                 putLogError(100008, check_result.message, __FILE__,
539                                             __LINE__ );
540                                 //loop break
541                                 break;
542                             }
543                             //next option string's length > 127
544                             if (it->size() > MAX_OPTION_SIZE - 1)
545                             {
546                                 std::ostringstream ostr;
547                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
548
549                                 //set check result flag false
550                                 check_result.flag = false;
551                                 //set check result message
552                                 check_result.message = ostr.str();
553                                 putLogError(100009, check_result.message, __FILE__,
554                                             __LINE__ );
555                                 //loop break
556                                 break;
557                             }
558                             //next option string's length <= 127
559                             else
560                             {
561                                 //regex check
562                                 //check OK
563                                 if (regex_match(*it, sorry_uri_regex))
564                                 {
565                                     sorryuri_checked = true;
566                                     strcpy(sorry_uri.data(), it->c_str());
567                                 }
568                                 //check NG
569                                 else
570                                 {
571                                     std::ostringstream ostr;
572                                     ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
573
574                                     //set check result flag false
575                                     check_result.flag = false;
576                                     //set check result message
577                                     check_result.message = ostr.str();
578                                     putLogError(100010, check_result.message, __FILE__,
579                                                 __LINE__ );
580
581                                     break;
582                                 }
583                             }
584                         }
585                         //next item not exist
586                         else
587                         {
588                             //set check result flag false
589                             check_result.flag = false;
590                             //set check result message
591                             check_result.message = "You have to set option value '-S/--sorry-uri'.";
592                             putLogError(100011, check_result.message, __FILE__,
593                                         __LINE__ );
594
595                             break;
596                         }
597                     }
598                     //sorryURI flag = ON
599                     else
600                     {
601                         //set check result flag false
602                         check_result.flag = false;
603                         //set check result message
604                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
605                         putLogError(100012, check_result.message, __FILE__,
606                                     __LINE__ );
607
608                         break;
609                     }
610                 }
611                 //others
612                 else
613                 {
614                     //set check result flag false
615                     check_result.flag = false;
616                     //set check result message
617                     check_result.message = "Option error.";
618
619                     putLogError(100013, check_result.message, __FILE__,
620                                 __LINE__ );
621
622                     break;
623                 }
624             }
625
626             if (check_result.flag == true)
627             {
628                 //forward flag = OFF
629                 if (!forward_checked)
630                 {
631                     forwarded_for = 0;
632                 }
633             }
634
635         }
636         catch (const std::exception& ex)
637         {
638             check_result.flag = false;
639             std::cerr << "protocol_module_sessionless::set_parameter() : exception : error = " << ex.what() << "." << std::endl;
640             boost::format formatter("function : protocol_module_base::check_message_result "
641                                     "protocol_module_sessionless::set_parameter() : exception : "
642                                     "error = %s.");
643             formatter % ex.what();
644             putLogError(100014, formatter.str(), __FILE__, __LINE__ );
645         }
646         catch (...)
647         {
648             check_result.flag = false;
649             std::cerr << "protocol_module_sessionless::set_parameter() : Unknown exception." << std::endl;
650             putLogError(100015, "function : protocol_module_base::check_message_result "
651                         "protocol_module_sessionless::set_parameter() : "
652                         "Unknown exception.", __FILE__, __LINE__ );
653         }
654
655         /*-------- DEBUG LOG --------*/
656         if (unlikely(LOG_LV_DEBUG == getloglevel()))
657         {
658             boost::format formatter("out_function : protocol_module_base::check_message_result "
659                                     "protocol_module_sessionless::set_parameter("
660                                     "const std::vector<std::string>& args) : return_value = ("
661                                     "check_message_result.flag = %d, check_message_result.message = %s).");
662             formatter % check_result.flag % check_result.message;
663             putLogDebug(100016, formatter.str(), __FILE__, __LINE__ );
664         }
665         /*-------- DEBUG LOG END--------*/
666
667         return check_result;
668     }
669
670     //! parameter add
671     //! @param[in] module paramter string list
672     //! @return    result.flag true is parameter is noproblem.
673     //! @return result.flag false is paramter is problem.
674     protocol_module_base::check_message_result protocol_module_sessionless::add_parameter(const std::vector<
675             std::string>& args)
676     {
677         /*-------- DEBUG LOG --------*/
678         if (unlikely(LOG_LV_DEBUG == getloglevel()))
679         {
680             boost::format formatter("in_function : protocol_module_base::check_message_result "
681                                     "protocol_module_sessionless::add_parameter("
682                                     "const std::vector<std::string>& args) : args = %s.");
683             std::string argsdump;
684             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
685             {
686                 argsdump += *it;
687                 argsdump += " ";
688             }
689             formatter % argsdump;
690             putLogDebug(100017, formatter.str(), __FILE__, __LINE__ );
691         }
692         /*------DEBUG LOG END------*/
693         check_message_result check_result;
694         //set check result flag true
695         check_result.flag = true;
696
697         //param list is not empty
698         if (!args.empty())
699         {
700             //set check result flag false
701             check_result.flag = false;
702             //set check result message
703             check_result.message = "Cannot add option.";
704             putLogError(100016, check_result.message, __FILE__, __LINE__ );
705         }
706
707         /*-------- DEBUG LOG --------*/
708         if (unlikely(LOG_LV_DEBUG == getloglevel()))
709         {
710             boost::format formatter("out_function : protocol_module_base::check_message_result "
711                                     "protocol_module_sessionless::add_parameter("
712                                     "const std::vector<std::string>& args) : return_value = ("
713                                     "check_message_result.flag = %d, check_message_result.message = %s).");
714             formatter % check_result.flag % check_result.message;
715             putLogDebug(100018, formatter.str(), __FILE__, __LINE__ );
716         }
717         /*-------- DEBUG LOG --------*/
718
719         return check_result;
720     }
721
722     //! get option info
723     //! @param[out] module paramter string
724     void protocol_module_sessionless::get_option_info(std::string& option)
725     {
726         /*-------- DEBUG LOG --------*/
727         if (unlikely(LOG_LV_DEBUG == getloglevel()))
728         {
729             putLogDebug(100019, "in_function : void protocol_module_sessionless::get_option_info("
730                         "std::string& option).", __FILE__, __LINE__);
731         }
732         /*------DEBUG LOG END------*/
733
734         boost::format option_formatter("%s--sorry-uri '%s'");
735         option_formatter % (forwarded_for ? "--forwarded-for " : "") % sorry_uri.c_array();
736         option.assign(option_formatter.str());
737
738         /*-------- DEBUG LOG --------*/
739         if (unlikely(LOG_LV_DEBUG == getloglevel()))
740         {
741             boost::format formatter("out_function : void protocol_module_sessionless::get_option_info("
742                                     "std::string& option) : option = %s.");
743             formatter % option;
744             putLogDebug(100020, formatter.str(), __FILE__, __LINE__);
745         }
746         /*------DEBUG LOG END------*/
747     }
748
749     //! TCP/IP scheduled function registation.
750     //! @param[in] schedule module TCP/IP scheduled function object type
751     void protocol_module_sessionless::register_schedule(tcp_schedule_func_type inschedule)
752     {
753         /*-------- DEBUG LOG --------*/
754         if (unlikely(LOG_LV_DEBUG == getloglevel()))
755         {
756             putLogDebug(100021, "in_function : void protocol_module_sessionless::register_schedule("
757                         "tcp_schedule_func_type inschedule).", __FILE__, __LINE__ );
758         }
759         /*------DEBUG LOG END------*/
760         schedule_tcp = inschedule;
761         /*-------- DEBUG LOG --------*/
762         if (unlikely(LOG_LV_DEBUG == getloglevel()))
763         {
764             putLogDebug(100022, "out_function : void protocol_module_sessionless::register_schedule("
765                         "tcp_schedule_func_type inschedule).", __FILE__, __LINE__ );
766         }
767         /*------DEBUG LOG END------*/
768     }
769
770     //! UDP scheduled function registation
771     //! @param[in] schedule module UDP scheduled funtion object type
772     void protocol_module_sessionless::register_schedule(udp_schedule_func_type inschedule)
773     {
774         /*-------- DEBUG LOG --------*/
775         if (unlikely(LOG_LV_DEBUG == getloglevel()))
776         {
777             putLogDebug(100023,
778                         "in/out_function : void protocol_module_sessionless::register_schedule(udp_schedule_func_type inschedule).",
779                         __FILE__, __LINE__ );
780         }
781         /*------DEBUG LOG END------*/
782     }
783
784     //! called from session initialzie use in upstream_thread
785     //! @param[in]    upstream thread id.
786     //! @param[in]    downstream thread id
787     //! @return        session use EVENT mode.
788     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_session_initialize(
789         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id,
790         const boost::asio::ip::tcp::endpoint& client_endpoint_tcp,
791         const boost::asio::ip::udp::endpoint& client_endpoint_udp)
792     {
793         /*-------- DEBUG LOG --------*/
794         if (unlikely(LOG_LV_DEBUG == getloglevel()))
795         {
796             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
797                                     "handle_session_initialize(const boost::thread::id up_thread_id, "
798                                     "const boost::thread::id down_thread_id, const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
799                                     "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : "
800                                     "up_thread_id = %d, down_thread_id = %d, client_endpoint_tcp = [%s]:%d.");
801             formatter % up_thread_id % down_thread_id % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port() ;
802             putLogDebug(100024, formatter.str(), __FILE__, __LINE__ );
803         }
804         /*------DEBUG LOG END------*/
805         EVENT_TAG status = FINALIZE;
806
807         //session thread initialization
808         try
809         {
810             thread_data_ptr p_up(new session_thread_data_sessionless);
811             /*-------- DEBUG LOG --------*/
812             if (unlikely(LOG_LV_DEBUG == getloglevel()))
813             {
814                 boost::format formatter("new : address = &(%d), size = %lu.");
815                 formatter % static_cast<void*>(p_up.get()) % sizeof(session_thread_data_sessionless);
816                 putLogDebug(100025, formatter.str(), __FILE__, __LINE__ );
817             }
818             /*------DEBUG LOG END------*/
819             p_up->thread_id = up_thread_id;
820             p_up->thread_division = THREAD_DIVISION_UP_STREAM;
821             p_up->pair_thread_id = down_thread_id;
822             p_up->accept_end_flag = ACCEPT_END_FLAG_OFF;
823             p_up->end_flag = END_FLAG_OFF;
824             p_up->sorry_flag = SORRY_FLAG_OFF;
825             p_up->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
826             p_up->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
827             p_up->last_status = INITIALIZE;
828             p_up->client_endpoint_tcp = client_endpoint_tcp;
829
830             recive_data recv_data;
831             p_up->recive_data_map[client_endpoint_tcp] = recv_data;
832
833             /*-------- DEBUG LOG --------*/
834             if (unlikely(LOG_LV_DEBUG == getloglevel()))
835             {
836                 // data dump
837                 boost::format
838                 formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
839                           "handle_session_initialize() : session_thread_data_sessionless(up_thread_id) : "
840                           "thread_id = %d, thread_division = %d, "
841                           "pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
842                           "sorry_flag = %d, sorryserver_switch_flag = %d, realserver_switch_flag = %d, last_status = %d, client_endpoint_tcp = [%s]:%d.");
843                 formatter % p_up->thread_id % p_up->thread_division % p_up->pair_thread_id % p_up->accept_end_flag
844                 % p_up->end_flag % p_up->sorry_flag % p_up->sorryserver_switch_flag % p_up->realserver_switch_flag
845                 % p_up->last_status % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port();
846                 putLogDebug(100026, formatter.str(), __FILE__, __LINE__ );
847             }
848             /*------DEBUG LOG END------*/
849
850             thread_data_ptr p_down(new session_thread_data_sessionless);
851             /*-------- DEBUG LOG --------*/
852             if (unlikely(LOG_LV_DEBUG == getloglevel()))
853             {
854                 boost::format formatter("new : address = &(%d), size = %lu.");
855                 formatter % static_cast<void*>(p_down.get()) % sizeof(session_thread_data_sessionless);
856                 putLogDebug(100027, formatter.str(), __FILE__, __LINE__ );
857             }
858             /*------DEBUG LOG END------*/
859             p_down->thread_id = down_thread_id;
860             p_down->thread_division = THREAD_DIVISION_DOWN_STREAM;
861             p_down->pair_thread_id = up_thread_id;
862             p_down->accept_end_flag = ACCEPT_END_FLAG_OFF;
863             p_down->end_flag = END_FLAG_OFF;
864             p_down->sorry_flag = SORRY_FLAG_OFF;
865             p_down->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
866             p_down->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
867             p_down->last_status = INITIALIZE;
868             p_down->client_endpoint_tcp = client_endpoint_tcp;
869             /*-------- DEBUG LOG --------*/
870             if (unlikely(LOG_LV_DEBUG == getloglevel()))
871             {
872                 // data dump
873                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
874                                         "handle_session_initialize() : session_thread_data_sessionless(down_thread_id) : "
875                                         "thread_id = %d, thread_division = %d, pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
876                                         "sorry_flag = %d, sorryserver_switch_flag = %d, realserver_switch_flag = %d, "
877                                         "last_status = %d, client_endpoint_tcp = [%s]:%d.");
878                 formatter % p_down->thread_id % p_down->thread_division % p_down->pair_thread_id % p_down->accept_end_flag
879                 % p_down->end_flag % p_down->sorry_flag % p_down->sorryserver_switch_flag
880                 % p_down->realserver_switch_flag % p_down->last_status % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port();
881                 putLogDebug(100028, formatter.str(), __FILE__, __LINE__ );
882             }
883             /*------DEBUG LOG END------*/
884
885             boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
886
887             session_thread_data_map[up_thread_id] = p_up;
888             session_thread_data_map[down_thread_id] = p_down;
889
890             status = ACCEPT;
891         }
892         catch (const std::bad_alloc&)
893         {
894             std::cerr << "protocol_module_sessionless::handle_session_initialize() : exception : Could not allocate memory." << std::endl;
895             boost::format formatter("Could not allocate memory. thread id : %d.");
896             formatter % boost::this_thread::get_id();
897             putLogError(100017, formatter.str(), __FILE__, __LINE__ );
898             status = FINALIZE;
899         }
900         catch (const std::exception& ex)
901         {
902             std::cerr << "protocol_module_sessionless::handle_session_initialize() : exception : error = " << ex.what() << "." << std::endl;
903             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
904                                     "handle_session_initialize() : exception : error = %s. thread id : %d.");
905             formatter % ex.what() % boost::this_thread::get_id();
906             putLogError(100018, formatter.str(), __FILE__, __LINE__ );
907             status = FINALIZE;
908         }
909         catch (...)
910         {
911             std::cerr << "protocol_module_sessionless::handle_session_initialize() : Unknown exception." << std::endl;
912             boost::format formatter("function : protocol_module_base::check_message_result "
913                                     "protocol_module_sessionless::handle_session_initialize() : "
914                                     "Unknown exception. thread id : %d.");
915             formatter % boost::this_thread::get_id();
916             putLogError(100019, formatter.str(), __FILE__, __LINE__ );
917
918             status = FINALIZE;
919         }
920
921         /*-------- DEBUG LOG --------*/
922         if (unlikely(LOG_LV_DEBUG == getloglevel()))
923         {
924             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
925                                     "handle_session_initialize(const boost::thread::id up_thread_id, "
926                                     "const boost::thread::id down_thread_id, const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
927                                     "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : return_value = %d. thread id : %d.");
928             formatter % status % boost::this_thread::get_id();
929             putLogDebug(100029, formatter.str(), __FILE__, __LINE__ );
930         }
931         /*------DEBUG LOG END------*/
932
933         return status;
934     }
935     //! called from session finalize use in upstream thread.
936     //! @param[in]    upstream thread id.
937     //! @param[in]    downstream thread id
938     //! @return        session use EVENT mode.
939     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_session_finalize(
940         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id)
941     {
942
943         /*-------- DEBUG LOG --------*/
944         if (unlikely(LOG_LV_DEBUG == getloglevel()))
945         {
946             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
947                                     "handle_session_finalize(const boost::thread::id up_thread_id, "
948                                     "const boost::thread::id down_thread_id) : "
949                                     "up_thread_id = %d, down_thread_id = %d.");
950             formatter % up_thread_id % down_thread_id;
951             putLogDebug(100030, formatter.str(), __FILE__, __LINE__ );
952         }
953         /*------DEBUG LOG END------*/
954         EVENT_TAG status = STOP;
955         thread_data_ptr p_up;
956         thread_data_ptr p_down;
957         session_thread_data_map_it session_thread_data_it;
958         recive_data_map_it recive_data_it;
959
960         //session thread free
961         try
962         {
963             boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
964
965             session_thread_data_it = session_thread_data_map.find(up_thread_id);
966             if (session_thread_data_it != session_thread_data_map.end())
967             {
968                 p_up = session_thread_data_it->second;
969                 /*-------- DEBUG LOG --------*/
970                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
971                 {
972                     boost::format formatter("delete : address = &(%d).");
973                     formatter % static_cast<void*>(p_up.get());
974                     putLogDebug(100031, formatter.str(), __FILE__, __LINE__ );
975                 }
976                 /*------DEBUG LOG END------*/
977                 session_thread_data_map.erase(up_thread_id);
978             }
979
980             session_thread_data_it = session_thread_data_map.find(down_thread_id);
981             if (session_thread_data_it != session_thread_data_map.end())
982             {
983                 /*-------- DEBUG LOG --------*/
984                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
985                 {
986                     boost::format formatter("delete : address = &(%d).");
987                     formatter % static_cast<void*>(p_down.get());
988                     putLogDebug(100032, formatter.str(), __FILE__,
989                                 __LINE__ );
990                 }
991                 /*------DEBUG LOG END------*/
992                 session_thread_data_map.erase(down_thread_id);
993             }
994
995             status = STOP;
996         }
997         catch (const std::exception& ex)
998         {
999             std::cerr << "protocol_module_sessionless::handle_session_finalize() : exception : error = " << ex.what() << "." << std::endl;
1000             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1001                                     "handle_session_finalize() : exception : error = %s. thread id : %d.");
1002             formatter % ex.what() % boost::this_thread::get_id();
1003             putLogError(100020, formatter.str(), __FILE__, __LINE__ );
1004             status = STOP;
1005         }
1006         catch (...)
1007         {
1008             std::cerr << "protocol_module_sessionless::handle_session_finalize() : Unknown exception." << std::endl;
1009             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1010                                     "handle_session_finalize() : "
1011                                     "Unknown exception. thread id : %d.");
1012             formatter % boost::this_thread::get_id();
1013             putLogError(100021, formatter.str(), __FILE__, __LINE__ );
1014             status = STOP;
1015         }
1016
1017         /*-------- DEBUG LOG --------*/
1018         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1019         {
1020             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1021                                     "handle_session_finalize(const boost::thread::id up_thread_id, "
1022                                     "const boost::thread::id down_thread_id) : return_value = %d. thread id : %d.");
1023             formatter % status % boost::this_thread::get_id();
1024             putLogDebug(100033, formatter.str(), __FILE__, __LINE__ );
1025         }
1026         /*------DEBUG LOG END------*/
1027         return status;
1028     }
1029
1030     //! called from after session accept.in client socket use in upstream thread.
1031     //! @param[in]    upstream thread id.
1032     //! @return        session use EVENT mode.
1033     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_accept(const boost::thread::id thread_id)
1034     {
1035
1036         /*-------- DEBUG LOG --------*/
1037         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1038         {
1039             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1040                                     "handle_accept(const boost::thread::id thread_id) : thread_id = %d.");
1041             formatter % thread_id;
1042             putLogDebug(100034, formatter.str(), __FILE__, __LINE__ );
1043         }
1044         /*------DEBUG LOG END------*/
1045
1046         EVENT_TAG status = FINALIZE;
1047         thread_data_ptr session_data;
1048         session_thread_data_map_it session_thread_it;
1049
1050         try
1051         {
1052             boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
1053
1054             session_thread_it = session_thread_data_map.find(thread_id);
1055             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
1056             {
1057                 boost::format formatter("Invalid thread id. thread id : %d.");
1058                 formatter % boost::this_thread::get_id();
1059                 putLogError(100022, formatter.str(), __FILE__, __LINE__ );
1060                 throw -1;
1061             }
1062
1063             session_data = session_thread_it->second;
1064
1065             //set accept end flag ON
1066             session_data->accept_end_flag = ACCEPT_END_FLAG_ON;
1067             /*-------- DEBUG LOG --------*/
1068             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1069             {
1070                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1071                                         "handle_accept(const boost::thread::id thread_id) : ACCEPT_END_FLAG_ON. thread id : %d.");
1072                 formatter % boost::this_thread::get_id();
1073                 putLogDebug(100035, formatter.str(), __FILE__, __LINE__ );
1074             }
1075             /*------DEBUG LOG END------*/
1076
1077             //sorry flag on
1078             if (session_data->sorry_flag == SORRY_FLAG_ON)
1079             {
1080                 //set return status
1081                 status = SORRYSERVER_SELECT;
1082             }
1083             //sorry flag on
1084             else
1085             {
1086                 //set return status
1087                 status = REALSERVER_SELECT;
1088             }
1089         }
1090         catch (int e)
1091         {
1092             /*-------- DEBUG LOG --------*/
1093             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1094             {
1095                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1096                                         "handle_accept() : catch exception e = %d. thread id : %d.");
1097                 formatter % e % boost::this_thread::get_id();
1098                 putLogDebug(100036, formatter.str(), __FILE__, __LINE__ );
1099             }
1100             status = FINALIZE;
1101             /*------DEBUG LOG END------*/
1102         }
1103         catch (const std::exception& ex)
1104         {
1105             std::cerr << "protocol_module_sessionless::handle_accept() : exception : error = " << ex.what() << "." << std::endl;
1106             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1107                                     "handle_accept() : exception : error = %s. thread id : %d.");
1108             formatter % ex.what() % boost::this_thread::get_id();
1109             putLogError(100023, formatter.str(), __FILE__, __LINE__ );
1110
1111             status = FINALIZE;
1112         }
1113         catch (...)
1114         {
1115             std::cerr << "protocol_module_sessionless::handle_accept() : Unknown exception." << std::endl;
1116             boost::format formatter("function : protocol_module_base::EVENT_TAG "
1117                                     "protocol_module_sessionless::handle_accept() : "
1118                                     "Unknown exception. thread id : %d.");
1119             formatter % boost::this_thread::get_id();
1120             putLogError(100024, formatter.str(), __FILE__, __LINE__ );
1121             status = FINALIZE;
1122         }
1123
1124         /*-------- DEBUG LOG --------*/
1125         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1126         {
1127             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1128                                     "handle_accept(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
1129             formatter % status % boost::this_thread::get_id();
1130             putLogDebug(100037, formatter.str(), __FILE__, __LINE__ );
1131         }
1132         /*------DEBUG LOG END------*/
1133         return status;
1134     }
1135
1136     //! called from after session recv in client socket. use in upstream thread.
1137     //! @param[in]    upstream thread id
1138     //! @param[in]    recive buffer refarence.
1139     //! @param[in]    recive length
1140     //! @return        session use EVENT mode.
1141     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_recv(const boost::thread::id thread_id,
1142             const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
1143     {
1144         /*-------- DEBUG LOG --------*/
1145         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1146         {
1147             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
1148             std::string buffer;
1149             dump_memory(recvbuffer.data(), buffer_size, buffer);
1150             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1151                                     "handle_client_recv(const boost::thread::id thread_id, "
1152                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1153                                     "const size_t recvlen) : thread_id = %d, recvbuffer = %s, recvlen = %d.");
1154             formatter % thread_id % buffer % recvlen;
1155             putLogDebug(100038, formatter.str(), __FILE__, __LINE__ );
1156         }
1157         /*------DEBUG LOG END------*/
1158
1159         EVENT_TAG status = FINALIZE;
1160         size_t data_remain_start = 0;
1161         size_t data_remain_size = 0;
1162         size_t request_data_remain_size = 0;
1163         size_t header_offset = 0;
1164         size_t header_offset_len = 0;
1165         size_t content_length_header_offset = 0;
1166         size_t content_length_header_len = 0;
1167         size_t content_len_value = 0;
1168         size_t pos = 0;
1169         size_t buffer_size = 0;
1170         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
1171         const size_t cr_lf_len = strlen("\r\n");
1172         std::string str_value;
1173         const std::string http_header = "";
1174         const std::string content_header = "Content-Length";
1175         thread_data_ptr session_data;
1176         char* buffer1 = NULL;
1177         char* buffer2 = NULL;
1178         bool bret = false;
1179         CHECK_RESULT_TAG check_result;
1180         session_thread_data_map_it session_thread_it;
1181         recive_data_map_it recive_data_it;
1182
1183         //parameter check
1184         if (recvlen > recvbuffer.size())
1185         {
1186             std::cerr << "protocol_module_sessionless::handle_client_recv() : Data size bigger than buffer size." << std::endl;
1187             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
1188             formatter % boost::this_thread::get_id();
1189             putLogError(100025, formatter.str(), __FILE__, __LINE__ );
1190             /*-------- DEBUG LOG --------*/
1191             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1192             {
1193                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1194                                         "handle_client_recv(const boost::thread::id thread_id, "
1195                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1196                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
1197                 formatter % FINALIZE % boost::this_thread::get_id();
1198                 putLogDebug(100039, formatter.str(), __FILE__, __LINE__ );
1199             }
1200             /*------DEBUG LOG END------*/
1201             return FINALIZE;
1202         }
1203
1204         try
1205         {
1206             {
1207                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
1208
1209                 session_thread_it = session_thread_data_map.find(thread_id);
1210                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
1211                 {
1212                     boost::format formatter("Invalid thread id. thread id : %d.");
1213                     formatter % boost::this_thread::get_id();
1214                     putLogError(100026, formatter.str(), __FILE__, __LINE__ );
1215                     throw -1;
1216                 }
1217
1218                 session_data = session_thread_it->second;
1219             }
1220
1221             //end flag on
1222             if (session_data->end_flag == END_FLAG_ON)
1223             {
1224                 status =  CLIENT_RECV;
1225             }
1226             //end flag off
1227             else
1228             {
1229                 recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
1230                 if (unlikely(recive_data_it == session_data->recive_data_map.end()))
1231                 {
1232                     boost::format formatter("Invalid endpoint. thread id : %d.");
1233                     formatter % boost::this_thread::get_id();
1234                     putLogError(100027, formatter.str(), __FILE__, __LINE__ );
1235                     throw -1;
1236                 }
1237
1238                 recive_data& recv_data = recive_data_it->second;
1239
1240                 send_status_it it = recv_data.send_status_list.begin();
1241                 send_status_it it_end = recv_data.send_status_list.end();
1242
1243                 //status list check
1244                 it = std::find_if(it, it_end, data_send_ok());
1245                 if (unlikely(it != it_end))
1246                 {
1247                     boost::format formatter("Sending data is not correct. thread id : %d.");
1248                     formatter % boost::this_thread::get_id();
1249                     putLogError(100028, formatter.str(), __FILE__, __LINE__ );
1250                     throw -1;
1251                 }
1252
1253                 //status list check
1254                 it = recv_data.send_status_list.begin();
1255                 it = std::adjacent_find(it, it_end, data_send_repeated());
1256                 if (unlikely(it != it_end))
1257                 {
1258                     boost::format formatter("Sending data is not correct. thread id : %d.");
1259                     formatter % boost::this_thread::get_id();
1260                     putLogError(100029, formatter.str(), __FILE__, __LINE__ );
1261                     throw -1;
1262                 }
1263
1264                 /*-------- DEBUG LOG --------*/
1265                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1266                 {
1267                     std::string datadump;
1268                     boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1269                                             "send_rest_size = %d, send_possible_size = %d, "
1270                                             "send_offset = %d, unsend_size = %d, edit_division = %d.");
1271                     int i = 0;
1272                     for (it = recv_data.send_status_list.begin();
1273                             it != recv_data.send_status_list.end();
1274                             ++it, ++i)
1275                     {
1276                         formatter % i % it->status % it->send_end_size
1277                         % it->send_rest_size % it->send_possible_size
1278                         % it->send_offset % it->unsend_size % it->edit_division;
1279                         datadump += formatter.str();
1280                     }
1281
1282                     formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1283                                     "handle_client_recv() : send status list dump : send status list size = %d.%s");
1284
1285                     formatter % recv_data.send_status_list.size() % datadump;
1286                     putLogDebug(100040, formatter.str(), __FILE__, __LINE__ );
1287                 }
1288                 /*------DEBUG LOG END------*/
1289
1290                 it = recv_data.send_status_list.begin();
1291                 //get original status info
1292                 while (it != it_end)
1293                 {
1294                     //item status is SEND_END
1295                     if (it->status == SEND_END)
1296                     {
1297                         //erase from list
1298                         recv_data.send_status_list.erase(it++);
1299                         continue;
1300                     }
1301                     //item status is SEND_CONTINUE
1302                     else if (it->status == SEND_CONTINUE)
1303                     {
1304                         it->send_offset += it->send_end_size;
1305                         data_remain_start = it->send_offset;
1306                         break;
1307                     }
1308                     //item status is SEND_NG
1309                     else
1310                     {
1311                         data_remain_start = it->send_offset;
1312                         data_remain_size = it->unsend_size;
1313                         break;
1314                     }
1315
1316                     ++it;
1317                 }
1318                 /*-------- DEBUG LOG --------*/
1319                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1320                 {
1321                     std::string datadump;
1322                     boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1323                                             "send_rest_size = %d, send_possible_size = %d, "
1324                                             "send_offset = %d, unsend_size = %d, edit_division = %d.");
1325                     int i = 0;
1326                     for (it = recv_data.send_status_list.begin();
1327                             it != recv_data.send_status_list.end();
1328                             ++it, ++i)
1329                     {
1330                         formatter % i % it->status % it->send_end_size
1331                         % it->send_rest_size % it->send_possible_size
1332                         % it->send_offset % it->unsend_size % it->edit_division;
1333                         datadump += formatter.str();
1334                     }
1335
1336                     formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1337                                     "handle_client_recv() : send status list dump : send status list size = %d.%s");
1338
1339                     formatter % recv_data.send_status_list.size() % datadump;
1340                     putLogDebug(100041, formatter.str(), __FILE__, __LINE__ );
1341                 }
1342                 /*------DEBUG LOG END------*/
1343
1344                 //recive buffer process
1345                 //buffer rest size < request size
1346                 if (recv_data.recive_buffer_rest_size < recvlen)
1347                 {
1348                     //buffer max size < remain size + request size
1349                     //buffer is need reallocate
1350                     if (recv_data.recive_buffer_max_size < data_remain_size + recvlen)
1351                     {
1352                         //the buffer's size that will be allocated is exceed the upper limit value
1353                         if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen)
1354                         {
1355                             std::cerr << "protocol_module_sessionless::handle_client_recv() : the buffer's size that will be allacted is exceed the upper limit value." << std::endl;
1356                             boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
1357                             formatter % boost::this_thread::get_id();
1358                             putLogError(100030, formatter.str(), __FILE__, __LINE__ );
1359                             /*-------- DEBUG LOG --------*/
1360                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1361                             {
1362                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1363                                                         "handle_client_recv(const boost::thread::id thread_id, "
1364                                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1365                                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
1366                                 formatter % FINALIZE % boost::this_thread::get_id();
1367                                 putLogDebug(100042, formatter.str(), __FILE__, __LINE__ );
1368                             }
1369                             /*------DEBUG LOG END------*/
1370                             return FINALIZE;
1371                         }
1372
1373                         buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
1374                         //recive_buffer1's memory allocate and initialization
1375                         buffer1 = new char[buffer_size];
1376                         /*-------- DEBUG LOG --------*/
1377                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1378                         {
1379                             boost::format formatter("new : address = &(%d), size = %lu.");
1380                             formatter % static_cast<void*>(buffer1) % buffer_size;
1381                             putLogDebug(100043, formatter.str(), __FILE__, __LINE__ );
1382                         }
1383                         /*------DEBUG LOG END------*/
1384                         memset(buffer1, 0, buffer_size);
1385                         //recive_buffer2's memory allocate and initialization
1386                         buffer2 = new char[buffer_size];
1387                         /*-------- DEBUG LOG --------*/
1388                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1389                         {
1390                             boost::format formatter("new : address = &(%d), size = %lu.");
1391                             formatter % static_cast<void*>(buffer2) % buffer_size;
1392                             putLogDebug(100044, formatter.str(), __FILE__, __LINE__ );
1393                         }
1394                         /*------DEBUG LOG END------*/
1395                         memset(buffer2, 0, buffer_size);
1396
1397                         /*-------- DEBUG LOG --------*/
1398                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1399                         {
1400                             std::string datadump;
1401                             dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
1402                             boost::format formatter(
1403                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1404                                 "handle_client_recv() : before memcpy (data dump) : "
1405                                 "data begin = %d, data_size = %d, data = %s");
1406                             formatter % data_remain_start % data_remain_size % datadump;
1407                             putLogDebug(100045, formatter.str(), __FILE__, __LINE__ );
1408                         }
1409                         /*------DEBUG LOG END------*/
1410                         //copy data from old buffer to new buffer
1411                         memcpy(buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
1412                         /*-------- DEBUG LOG --------*/
1413                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1414                         {
1415                             std::string datadump;
1416                             dump_memory(buffer1, data_remain_size, datadump);
1417                             boost::format formatter(
1418                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1419                                 "handle_client_recv() : after memcpy (data dump) : "
1420                                 "data begin = 0, data_size = %d, data = %s");
1421                             formatter % data_remain_size % datadump;
1422                             putLogDebug(100046, formatter.str(), __FILE__, __LINE__ );
1423                         }
1424                         /*------DEBUG LOG END------*/
1425
1426                         /*-------- DEBUG LOG --------*/
1427                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1428                         {
1429                             std::string datadump;
1430                             dump_memory(recvbuffer.data(), recvlen, datadump);
1431                             boost::format formatter(
1432                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1433                                 "handle_client_recv() : before memcpy (data dump) : "
1434                                 "data begin = 0, data_size = %d, data = %s");
1435                             formatter % recvlen % datadump;
1436                             putLogDebug(100047, formatter.str(), __FILE__, __LINE__ );
1437                         }
1438                         /*------DEBUG LOG END------*/
1439                         memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
1440                         /*-------- DEBUG LOG --------*/
1441                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1442                         {
1443                             std::string datadump;
1444                             dump_memory(buffer1 + data_remain_size, recvlen, datadump);
1445                             boost::format formatter(
1446                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1447                                 "handle_client_recv() : after memcpy (data dump) : "
1448                                 "data begin = %d, data_size = %d, data = %s");
1449                             formatter % data_remain_size % recvlen % datadump;
1450                             putLogDebug(100048, formatter.str(), __FILE__, __LINE__ );
1451                         }
1452                         /*------DEBUG LOG END------*/
1453                         //free old buffer1 and old buffer2
1454                         if (recv_data.recive_buffer1 != NULL)
1455                         {
1456                             /*-------- DEBUG LOG --------*/
1457                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1458                             {
1459                                 boost::format formatter("delete : address = &(%d).");
1460                                 formatter % static_cast<void*>(recv_data.recive_buffer1);
1461                                 putLogDebug(100049, formatter.str(), __FILE__,
1462                                             __LINE__ );
1463                             }
1464                             /*------DEBUG LOG END------*/
1465                             delete[] recv_data.recive_buffer1;
1466                             recv_data.recive_buffer1 = NULL;
1467                         }
1468
1469                         if (recv_data.recive_buffer2 != NULL)
1470                         {
1471                             /*-------- DEBUG LOG --------*/
1472                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1473                             {
1474                                 boost::format formatter("delete : address = &(%d).");
1475                                 formatter % static_cast<void*>(recv_data.recive_buffer2);
1476                                 putLogDebug(100050, formatter.str(), __FILE__,
1477                                             __LINE__ );
1478                             }
1479                             /*------DEBUG LOG END------*/
1480                             delete[] recv_data.recive_buffer2;
1481                             recv_data.recive_buffer2 = NULL;
1482                         }
1483
1484                         //set new buffer pointer
1485                         recv_data.recive_buffer1 = buffer1;
1486                         recv_data.recive_buffer2 = buffer2;
1487                         recv_data.recive_buffer = recv_data.recive_buffer1;
1488                         //set new buffer's max size
1489                         recv_data.recive_buffer_max_size = buffer_size;
1490                     }
1491                     //buffer's max size >= remain data size + requst size
1492                     //buffer isn't need reallocate, but switch
1493                     else
1494                     {
1495                         //pointer valid check
1496                         if (unlikely(recv_data.recive_buffer1 == NULL || recv_data.recive_buffer2 == NULL))
1497                         {
1498                             boost::format formatter("Invalid pointer. thread id : %d.");
1499                             formatter % boost::this_thread::get_id();
1500                             putLogError(100031, formatter.str(), __FILE__, __LINE__ );
1501                             throw -1;
1502                         }
1503                         //using buffer is buffer1
1504                         if (recv_data.recive_buffer == recv_data.recive_buffer1)
1505                         {
1506                             //buffer2 initialization
1507                             memset(recv_data.recive_buffer2, 0, recv_data.recive_buffer_max_size);
1508                             /*-------- DEBUG LOG --------*/
1509                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1510                             {
1511                                 std::string datadump;
1512                                 dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
1513                                 boost::format formatter(
1514                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1515                                     "handle_client_recv() : before memcpy (data dump) : "
1516                                     "data begin = %d, data_size = %d, data = %s");
1517                                 formatter % data_remain_start % data_remain_size  % datadump;
1518                                 putLogDebug(100051, formatter.str(), __FILE__, __LINE__ );
1519                             }
1520                             /*------DEBUG LOG END------*/
1521                             //copy data from buffer1 to buffer2
1522                             memcpy(recv_data.recive_buffer2, recv_data.recive_buffer + data_remain_start, data_remain_size);
1523                             /*-------- DEBUG LOG --------*/
1524                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1525                             {
1526                                 std::string datadump;
1527                                 dump_memory(recv_data.recive_buffer2, recvlen, datadump);
1528                                 boost::format formatter(
1529                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1530                                     "handle_client_recv() : after memcpy (data dump) : "
1531                                     "data begin = 0, data_size = %d, data = %s");
1532                                 formatter % recvlen % datadump;
1533                                 putLogDebug(100052, formatter.str(), __FILE__, __LINE__ );
1534                             }
1535                             /*------DEBUG LOG END------*/
1536                             /*-------- DEBUG LOG --------*/
1537                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1538                             {
1539                                 std::string datadump;
1540                                 dump_memory(recvbuffer.data(), recvlen, datadump);
1541                                 boost::format formatter(
1542                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1543                                     "handle_client_recv() : before memcpy (data dump) : "
1544                                     "data begin = 0, data_size = %d, data = %s");
1545                                 formatter % recvlen % datadump;
1546                                 putLogDebug(100053, formatter.str(), __FILE__, __LINE__ );
1547                             }
1548                             /*------DEBUG LOG END------*/
1549                             memcpy(recv_data.recive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
1550                             /*-------- DEBUG LOG --------*/
1551                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1552                             {
1553                                 std::string datadump;
1554                                 dump_memory(recv_data.recive_buffer2 + data_remain_size, recvlen, datadump);
1555                                 boost::format formatter(
1556                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1557                                     "handle_client_recv() : after memcpy (data dump) : "
1558                                     "data begin = %d, data_size = %d, data = %s");
1559                                 formatter % data_remain_size % recvlen % datadump;
1560                                 putLogDebug(100054, formatter.str(), __FILE__, __LINE__ );
1561                             }
1562                             /*------DEBUG LOG END------*/
1563                             //set buffer2 as using buffer
1564                             recv_data.recive_buffer = recv_data.recive_buffer2;
1565                         }
1566                         //using buffer is buffer2
1567                         else
1568                         {
1569                             //buffer1 initializtion
1570                             memset(recv_data.recive_buffer1, 0, recv_data.recive_buffer_max_size);
1571                             /*-------- DEBUG LOG --------*/
1572                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1573                             {
1574                                 std::string datadump;
1575                                 dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
1576                                 boost::format formatter(
1577                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1578                                     "handle_client_recv() : before memcpy (data dump) : "
1579                                     "data begin = %d, data_size = %d, data = %s");
1580                                 formatter % data_remain_start % data_remain_size % datadump;
1581                                 putLogDebug(100055, formatter.str(), __FILE__, __LINE__ );
1582                             }
1583                             /*------DEBUG LOG END------*/
1584                             //copy data from buffer2 to buffer1
1585                             memcpy(recv_data.recive_buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
1586                             /*-------- DEBUG LOG --------*/
1587                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1588                             {
1589                                 std::string datadump;
1590                                 dump_memory(recv_data.recive_buffer1, data_remain_size, datadump);
1591                                 boost::format formatter(
1592                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1593                                     "handle_client_recv() : after memcpy (data dump) : "
1594                                     "data begin = 0, data_size = %d, data = %s");
1595                                 formatter % data_remain_size % datadump;
1596                                 putLogDebug(100056, formatter.str(), __FILE__, __LINE__ );
1597                             }
1598                             /*------DEBUG LOG END------*/
1599                             /*-------- DEBUG LOG --------*/
1600                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1601                             {
1602                                 std::string datadump;
1603                                 dump_memory(recvbuffer.data(), recvlen, datadump);
1604                                 boost::format formatter(
1605                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1606                                     "handle_client_recv() : before memcpy (data dump) : "
1607                                     "data begin = 0, data_size = %d, data = %s");
1608                                 formatter % recvlen % datadump;
1609                                 putLogDebug(100057, formatter.str(), __FILE__, __LINE__ );
1610                             }
1611                             /*------DEBUG LOG END------*/
1612                             memcpy(recv_data.recive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
1613                             /*-------- DEBUG LOG --------*/
1614                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1615                             {
1616                                 std::string datadump;
1617                                 dump_memory(recv_data.recive_buffer1 + data_remain_size, recvlen, datadump);
1618                                 boost::format formatter(
1619                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1620                                     "handle_client_recv() : after memcpy (data dump) : "
1621                                     "data begin = %d, data_size = %d, data = %s");
1622                                 formatter % data_remain_size % recvlen % datadump;
1623                                 putLogDebug(100058, formatter.str(), __FILE__, __LINE__ );
1624                             }
1625                             /*------DEBUG LOG END------*/
1626                             //set buffer1 as using buffer
1627                             recv_data.recive_buffer = recv_data.recive_buffer1;
1628                         }
1629                     }
1630
1631                     //set buffer's rest size
1632                     recv_data.recive_buffer_rest_size = recv_data.recive_buffer_max_size - data_remain_size - recvlen;
1633
1634                     //remain_size recalc
1635                     data_remain_size += recvlen;
1636
1637                     send_status_it it_begin = recv_data.send_status_list.begin();
1638                     send_status_it it_end = recv_data.send_status_list.end();
1639
1640                     //offset recalc
1641                     for (; it_begin != it_end; ++it_begin)
1642                     {
1643                         it_begin->send_offset -= data_remain_start;
1644                     }
1645                 }
1646                 //buffer's rest size >= request size
1647                 else
1648                 {
1649                     //pointer valid check
1650                     if (unlikely(recv_data.recive_buffer == NULL))
1651                     {
1652                         boost::format formatter("Invalid pointer. thread id : %d.");
1653                         formatter % boost::this_thread::get_id();
1654                         putLogError(100032, formatter.str(), __FILE__, __LINE__ );
1655                         throw -1;
1656                     }
1657                     /*-------- DEBUG LOG --------*/
1658                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
1659                     {
1660                         std::string datadump;
1661                         dump_memory(recvbuffer.data(), recvlen, datadump);
1662                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1663                                                 "handle_client_recv() : before memcpy (data dump) : "
1664                                                 "data begin = 0, data_size = %d, data = %s");
1665                         formatter % recvlen % datadump;
1666                         putLogDebug(100059, formatter.str(), __FILE__, __LINE__ );
1667                     }
1668                     /*------DEBUG LOG END------*/
1669                     //copy data from parameter to using buffer
1670                     memcpy(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size,
1671                            recvbuffer.data(), recvlen);
1672                     /*-------- DEBUG LOG --------*/
1673                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
1674                     {
1675                         std::string datadump;
1676                         dump_memory(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size,
1677                                     recvlen, datadump);
1678                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1679                                                 "handle_client_recv() : after memcpy (data dump) : "
1680                                                 "data begin = %d, data_size = %d, data = %s");
1681                         formatter % (recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size )
1682                         % recvlen % datadump;
1683                         putLogDebug(100060, formatter.str(), __FILE__, __LINE__ );
1684                     }
1685                     /*------DEBUG LOG END------*/
1686                     //buffer's rest size recalc
1687                     recv_data.recive_buffer_rest_size -= recvlen;
1688                     //remain data size recalc
1689                     data_remain_size += recvlen;
1690                 }
1691
1692                 it = recv_data.send_status_list.begin();
1693                 it_end = recv_data.send_status_list.end();
1694
1695                 //set request rest size
1696                 request_data_remain_size = recvlen;
1697
1698                 //original status process
1699                 for (; it != it_end; ++it)
1700                 {
1701                     //status is SEND_CONTINUE
1702                     if (it->status == SEND_CONTINUE)
1703                     {
1704                         //send rest size > request size
1705                         if (it->send_rest_size > request_data_remain_size)
1706                         {
1707                             //send possible size recalc
1708                             it->send_possible_size = request_data_remain_size;
1709                             //send rest size recalc
1710                             it->send_rest_size -= request_data_remain_size;
1711                             //send end size recalc
1712                             it->send_end_size = 0;
1713                             //request size recalc
1714                             request_data_remain_size = 0;
1715                         }
1716                         //send rest size <= request size
1717                         else
1718                         {
1719                             //send possible size recalc
1720                             it->send_possible_size = it->send_rest_size;
1721                             //send rest size recalc
1722                             request_data_remain_size -= it->send_rest_size;
1723                             //send end size recalc
1724                             it->send_end_size = 0;
1725                             //request size recalc
1726                             it->send_rest_size = 0;
1727                         }
1728                         //set edit_division flag off
1729                         it->edit_division = EDIT_DIVISION_NO_EDIT;
1730                         //set status SEND_OK
1731                         it->status = SEND_OK;
1732                     }
1733                     //status is SEND_NG
1734                     else if (it->status == SEND_NG)
1735                     {
1736                         //check http method
1737                         check_result = check_http_method(recv_data.recive_buffer + it->send_offset, data_remain_size);
1738                         /*-------- DEBUG LOG --------*/
1739                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1740                         {
1741                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1742                                                     "handle_client_recv() : call check_http_method : "
1743                                                     "return_value = %d. thread id : %d.");
1744                             formatter % check_result % boost::this_thread::get_id();
1745                             putLogDebug(100061, formatter.str(), __FILE__, __LINE__ );
1746                         }
1747                         /*------DEBUG LOG END------*/
1748                         //check http method result is OK
1749                         if (check_result == CHECK_OK)
1750                         {
1751                             //check http version
1752                             check_result = check_http_version(recv_data.recive_buffer + it->send_offset, data_remain_size);
1753                             /*-------- DEBUG LOG --------*/
1754                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1755                             {
1756                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1757                                                         "handle_client_recv() : call check_http_version : "
1758                                                         "return_value = %d. thread id : %d.");
1759                                 formatter % check_result % boost::this_thread::get_id();
1760                                 putLogDebug(100062, formatter.str(), __FILE__, __LINE__ );
1761                             }
1762                             /*------DEBUG LOG END------*/
1763                         }
1764                         //check method and version result is OK
1765                         if (check_result == CHECK_OK)
1766                         {
1767                             //search http header
1768                             bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size, http_header,
1769                                                     header_offset, header_offset_len);
1770                             /*-------- DEBUG LOG --------*/
1771                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1772                             {
1773                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1774                                                         "handle_client_recv() : call find_http_header : "
1775                                                         "return_value = %d. thread id : %d.");
1776                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
1777                                 putLogDebug(100063, formatter.str(), __FILE__, __LINE__ );
1778                             }
1779                             /*------DEBUG LOG END------*/
1780                             //search http header result is OK
1781                             if (bret)
1782                             {
1783                                 //search Content_Length header
1784                                 bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size,
1785                                                         content_header, content_length_header_offset, content_length_header_len);
1786                                 /*-------- DEBUG LOG --------*/
1787                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1788                                 {
1789                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1790                                                             "handle_client_recv() : call find_http_header : "
1791                                                             "return_value = %d. thread id : %d.");
1792                                     formatter % static_cast<int>(bret) % boost::this_thread::get_id();
1793                                     putLogDebug(100064, formatter.str(), __FILE__, __LINE__ );
1794                                 }
1795                                 /*------DEBUG LOG END------*/
1796                                 //search Content_Length result is OK
1797                                 if (bret)
1798                                 {
1799                                     //Get Content_Length header's numeric value
1800                                     for (pos = 0; recv_data.recive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
1801                                             < content_length_header_len; ++pos)
1802                                         ;
1803                                     if (pos == content_length_header_len)
1804                                     {
1805                                         throw std::string("Content_Length field's value is invalid.");
1806                                     }
1807
1808                                     ++pos;
1809
1810                                     str_value.assign(recv_data.recive_buffer + it->send_offset + content_length_header_offset + pos,
1811                                                      content_length_header_len - pos);
1812
1813                                     size_t pos_end = str_value.find_last_of('\r');
1814                                     if (pos_end != std::string::npos)
1815                                     {
1816                                         str_value = str_value.erase(pos_end);
1817                                     }
1818
1819                                     for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
1820
1821                                     str_value = str_value.substr(pos);
1822
1823                                     try
1824                                     {
1825                                         content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
1826                                     }
1827                                     catch (const boost::bad_lexical_cast& ex)
1828                                     {
1829                                         throw std::string("Content_Length field's value is invalid.");
1830                                     }
1831
1832                                     //send_rest_size recalc
1833                                     //set whole http header's length + Content_Length's value
1834                                     it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
1835                                 }
1836                                 //search Content_Length result is NG
1837                                 else
1838                                 {
1839                                     //send_rest_size recalc
1840                                     //set whole http header's length
1841                                     if (header_offset_len == 0)
1842                                     {
1843                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
1844                                     } else
1845                                     {
1846                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
1847                                     }
1848                                 }
1849                                 //set edit_division flag on
1850                                 it->edit_division = EDIT_DIVISION_EDIT;
1851                             }
1852                             //search http header result is NG
1853                             else
1854                             {
1855                                 //unsend_size recalc
1856                                 it->unsend_size += request_data_remain_size;
1857                                 //request data rest size recalc
1858                                 request_data_remain_size = 0;
1859                                 break;
1860                             }
1861                         }
1862                         //check method and version result is NG
1863                         else if (check_result == CHECK_NG)
1864                         {
1865                             //set edit_division flag off
1866                             it->edit_division = EDIT_DIVISION_NO_EDIT;
1867                             //send_rest_size recalc
1868                             it->send_rest_size = it->unsend_size + request_data_remain_size;
1869                         }
1870                         //check method and version result is CHECK_INPOSSIBLE
1871                         else
1872                         {
1873                             //unsend_size recalc
1874                             it->unsend_size += request_data_remain_size;
1875                             //request data rest size recalc
1876                             request_data_remain_size = 0;
1877                             break;
1878                         }
1879
1880                         //recalc fields value according to send_rest_size and request rest size
1881                         if (it->send_rest_size > it->unsend_size + request_data_remain_size)
1882                         {
1883                             it->send_possible_size = it->unsend_size + request_data_remain_size;
1884                             it->send_rest_size -= (it->unsend_size + request_data_remain_size);
1885                             it->send_end_size = 0;
1886                             it->unsend_size = 0;
1887                             request_data_remain_size = 0;
1888                         }
1889                         else
1890                         {
1891                             it->send_possible_size = it->send_rest_size;
1892                             request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
1893                             it->send_end_size = 0;
1894                             it->unsend_size = 0;
1895                             it->send_rest_size = 0;
1896                         }
1897
1898                         //change status from SEND_NG to SEND_OK
1899                         it->status = SEND_OK;
1900                     }
1901                     //no request rest data to process
1902                     if (request_data_remain_size <= 0)
1903                     {
1904                         break;
1905                     }
1906                 }
1907                 /*-------- DEBUG LOG --------*/
1908                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1909                 {
1910                     std::string datadump;
1911                     boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1912                                             "send_rest_size = %d, send_possible_size = %d, "
1913                                             "send_offset = %d, unsend_size = %d, edit_division = %d.");
1914                     int i = 0;
1915                     for (it = recv_data.send_status_list.begin();
1916                             it != recv_data.send_status_list.end();
1917                             ++it, ++i)
1918                     {
1919                         formatter % i % it->status % it->send_end_size
1920                         % it->send_rest_size % it->send_possible_size
1921                         % it->send_offset % it->unsend_size % it->edit_division;
1922                         datadump += formatter.str();
1923                     }
1924
1925                     formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1926                                     "handle_client_recv() : send status list dump : send status list size = %d.%s");
1927
1928                     formatter % recv_data.send_status_list.size() % datadump;
1929                     putLogDebug(100065, formatter.str(), __FILE__, __LINE__ );
1930                 }
1931                 /*------DEBUG LOG END------*/
1932                 //there are still rest data need to process
1933                 //new status created and add to status list
1934                 while (request_data_remain_size > 0)
1935                 {
1936                     //new status created
1937                     send_status new_send_state;
1938
1939                     new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
1940                     new_send_state.send_end_size = 0;
1941                     new_send_state.send_offset = 0;
1942                     new_send_state.send_possible_size = 0;
1943                     new_send_state.unsend_size = 0;
1944                     new_send_state.send_rest_size = 0;
1945                     //status initialize to SEND_NG
1946                     new_send_state.status = SEND_NG;
1947                     //add new status to status_list
1948                     recv_data.send_status_list.push_back(new_send_state);
1949                     std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
1950                     //cacl offset
1951                     new_send_it->send_offset = recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size
1952                                                - request_data_remain_size;
1953
1954                     //check http method
1955                     check_result = check_http_method(recv_data.recive_buffer + new_send_it->send_offset,
1956                                                      request_data_remain_size);
1957                     /*-------- DEBUG LOG --------*/
1958                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
1959                     {
1960                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1961                                                 "handle_client_recv() : call check_http_method : "
1962                                                 "return_value = %d. thread id : %d.");
1963                         formatter % check_result % boost::this_thread::get_id();
1964                         putLogDebug(100066, formatter.str(), __FILE__, __LINE__ );
1965                     }
1966                     /*------DEBUG LOG END------*/
1967                     //check http method result is OK
1968                     if (check_result == CHECK_OK)
1969                     {
1970                         //check http version
1971                         check_result = check_http_version(recv_data.recive_buffer + new_send_it->send_offset,
1972                                                           request_data_remain_size);
1973                         /*-------- DEBUG LOG --------*/
1974                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1975                         {
1976                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1977                                                     "handle_client_recv() : call check_http_version : "
1978                                                     "return_value = %d. thread id : %d.");
1979                             formatter % check_result % boost::this_thread::get_id();
1980                             putLogDebug(100067, formatter.str(), __FILE__, __LINE__ );
1981                         }
1982                         /*------DEBUG LOG END------*/
1983                     }
1984                     //check http method and version result is OK
1985                     if (check_result == CHECK_OK)
1986                     {
1987                         //search whole http header, get whole http header's offset and length
1988                         bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset, request_data_remain_size,
1989                                                 http_header, header_offset, header_offset_len);
1990                         /*-------- DEBUG LOG --------*/
1991                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1992                         {
1993                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1994                                                     "handle_client_recv() : call find_http_header : "
1995                                                     "return_value = %d. thread id : %d.");
1996                             formatter % check_result % boost::this_thread::get_id();
1997                             putLogDebug(100068, formatter.str(), __FILE__, __LINE__ );
1998                         }
1999                         /*------DEBUG LOG END------*/
2000                         //searched whole http header
2001                         if (bret)
2002                         {
2003                             //search ContentLength http header, get ContentLength header's offset and length
2004                             bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset,
2005                                                     request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
2006                             /*-------- DEBUG LOG --------*/
2007                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2008                             {
2009                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2010                                                         "handle_client_recv() : call find_http_header : "
2011                                                         "return_value = %d. thread id : %d.");
2012                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
2013                                 putLogDebug(100069, formatter.str(), __FILE__, __LINE__ );
2014                             }
2015                             /*------DEBUG LOG END------*/
2016
2017                             //searched ContentLength http header
2018                             if (bret)
2019                             {
2020                                 //Get Content_Length header's numeric value
2021                                 for (pos = 0;
2022                                         recv_data.recive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
2023                                         && pos < content_length_header_len;
2024                                         ++pos);
2025                                 if (pos == content_length_header_len)
2026                                 {
2027                                     throw std::string("Content_Length field's value is invalid.");
2028                                 }
2029
2030                                 ++pos;
2031
2032                                 str_value.assign(recv_data.recive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
2033                                                  content_length_header_len - pos);
2034
2035                                 size_t pos_end = str_value.find_last_of('\r');
2036                                 if (pos_end != std::string::npos)
2037                                 {
2038                                     str_value = str_value.erase(pos_end);
2039                                 }
2040
2041                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
2042
2043                                 str_value = str_value.substr(pos);
2044                                 try
2045                                 {
2046                                     content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
2047                                 }
2048                                 catch (const boost::bad_lexical_cast& ex)
2049                                 {
2050                                     throw std::string("Content_Length field's value is invalid.");
2051                                 }
2052                                 //send_rest_size recalc
2053                                 //set whole http header's  + whole http header's length + Content_Length's value
2054                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
2055                             }
2056                             //not searched ContentLength http header
2057                             else
2058                             {
2059                                 //send_rest_size recalc
2060                                 //set whole http header's  + whole http header's length
2061                                 if (header_offset_len == 0)
2062                                 {
2063                                     new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
2064                                 }
2065                                 else
2066                                 {
2067                                     new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
2068                                 }
2069
2070                             }
2071                             //set edit_division flag on
2072                             new_send_it->edit_division = EDIT_DIVISION_EDIT;
2073                         }
2074                         //not searched whole http header
2075                         else
2076                         {
2077                             new_send_it->unsend_size = request_data_remain_size;
2078                             request_data_remain_size = 0;
2079                             break;
2080                         }
2081                     }
2082                     //check http method or version result is NG
2083                     else if (check_result == CHECK_NG)
2084                     {
2085                         new_send_it->edit_division = EDIT_DIVISION_NO_EDIT;
2086                         new_send_it->send_rest_size = request_data_remain_size;
2087                     }
2088
2089                     //check http method or version result is impossible
2090                     else
2091                     {
2092                         new_send_it->unsend_size = request_data_remain_size;
2093                         request_data_remain_size = 0;
2094                         break;
2095                     }
2096
2097                     //recalc fields value according to send_rest_size and request rest size
2098                     if (new_send_it->send_rest_size > request_data_remain_size)
2099                     {
2100                         new_send_it->send_possible_size = request_data_remain_size;
2101                         new_send_it->send_rest_size -= request_data_remain_size;
2102                         new_send_it->send_end_size = 0;
2103                         request_data_remain_size = 0;
2104                     }
2105                     else
2106                     {
2107                         new_send_it->send_possible_size = new_send_it->send_rest_size;
2108                         request_data_remain_size -= new_send_it->send_rest_size;
2109                         new_send_it->send_end_size = 0;
2110                         new_send_it->send_rest_size = 0;
2111                     }
2112
2113                     //change status from SEND_NG to SEND_OK
2114                     new_send_it->status = SEND_OK;
2115                 }
2116
2117                 /*-------- DEBUG LOG --------*/
2118                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2119                 {
2120                     std::string datadump;
2121                     boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
2122                                             "send_rest_size = %d, send_possible_size = %d, "
2123                                             "send_offset = %d, unsend_size = %d, edit_division = %d.");
2124                     int i = 0;
2125                     for (it = recv_data.send_status_list.begin();
2126                             it != recv_data.send_status_list.end();
2127                             ++it, ++i)
2128                     {
2129                         formatter % i % it->status % it->send_end_size
2130                         % it->send_rest_size % it->send_possible_size
2131                         % it->send_offset % it->unsend_size % it->edit_division;
2132                         datadump += formatter.str();
2133                     }
2134
2135                     formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2136                                     "handle_client_recv() : send status list dump : send status list size = %d.%s");
2137
2138                     formatter % recv_data.send_status_list.size() % datadump;
2139                     putLogDebug(100070, formatter.str(), __FILE__, __LINE__ );
2140                 }
2141                 /*------DEBUG LOG END------*/
2142                 //search for send_possible item in status list
2143                 send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
2144                                                  data_send_possible());
2145                 //the data that can be sent possible is exist
2146                 if (it_find != recv_data.send_status_list.end())
2147                 {
2148                     //sorry flag is on
2149                     if (session_data->sorry_flag == SORRY_FLAG_ON)
2150                     {
2151                         status = SORRYSERVER_CONNECT;
2152                     }
2153                     //sorry flag is off
2154                     else
2155                     {
2156                         status = REALSERVER_CONNECT;
2157                     }
2158                 }
2159                 //the data that can be sent possible is not exist
2160                 else
2161                 {
2162                     status = CLIENT_RECV;
2163                 }
2164             }
2165         }
2166         catch (int e)
2167         {
2168             /*-------- DEBUG LOG --------*/
2169             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2170             {
2171                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2172                                         "handle_client_recv() : catch exception e = %d. thread id : %d.");
2173                 formatter % e % boost::this_thread::get_id();
2174                 putLogDebug(100071, formatter.str(), __FILE__, __LINE__ );
2175             }
2176             /*------DEBUG LOG END------*/
2177             status = FINALIZE;
2178         }
2179         catch (const std::string& ex)
2180         {
2181             std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : " << ex << std::endl;
2182             boost::format formatter("protocol_module_sessionless::handle_client_recv() : exception :  %s. thread id : %d.");
2183             formatter % ex.c_str() % boost::this_thread::get_id();
2184             putLogError(100033, formatter.str(), __FILE__, __LINE__ );
2185             status = FINALIZE;
2186         }
2187         catch (const std::bad_alloc&)
2188         {
2189             std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : Could not allocate memory." << std::endl;
2190             boost::format formatter("Could not allocate memory. thread id : %d.");
2191             formatter % boost::this_thread::get_id();
2192             putLogError(100034, formatter.str(), __FILE__, __LINE__ );
2193             status = FINALIZE;
2194         }
2195         catch (const std::exception& ex)
2196         {
2197             std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : error = " << ex.what() << "." << std::endl;
2198             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2199                                     "handle_client_recv() : exception : error = %s. thread id : %d.");
2200             formatter % ex.what() % boost::this_thread::get_id();
2201             putLogError(100035, formatter.str(), __FILE__, __LINE__ );
2202
2203             status = FINALIZE;
2204         }
2205         catch (...)
2206         {
2207             std::cerr << "protocol_module_sessionless::handle_client_recv() : Unknown exception." << std::endl;
2208             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2209                                     "protocol_module_sessionless::handle_client_recv() : "
2210                                     "Unknown exception. thread id : %d.");
2211             formatter % boost::this_thread::get_id();
2212             putLogError(100036, formatter.str(), __FILE__, __LINE__ );
2213             status = FINALIZE;
2214         }
2215
2216         /*-------- DEBUG LOG --------*/
2217         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2218         {
2219             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2220                                     "handle_client_recv(const boost::thread::id thread_id, "
2221                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
2222                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
2223             formatter % status % boost::this_thread::get_id();
2224             putLogDebug(100072, formatter.str(), __FILE__, __LINE__ );
2225         }
2226         /*------DEBUG LOG END------*/
2227
2228         return status;
2229     }
2230
2231     //! called from after realserver select.use in upstream thread.
2232     //! @param[in]    upstream thread id
2233     //! @param[out]    realserver TCP endpoint
2234     //! @return        session use EVENT mode.
2235     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_select(
2236         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint & rs_endpoint)
2237     {
2238         /*-------- DEBUG LOG --------*/
2239         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2240         {
2241             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2242                                     "handle_realserver_select(const boost::thread::id thread_id, "
2243                                     "boost::asio::ip::tcp::endpoint & rs_endpoint) : "
2244                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
2245             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
2246             putLogDebug(100073, formatter.str(), __FILE__, __LINE__ );
2247         }
2248         /*------DEBUG LOG END------*/
2249         EVENT_TAG status = FINALIZE;
2250         boost::asio::ip::tcp::endpoint tmp_endpoint;
2251         thread_data_ptr session_data;
2252         session_thread_data_map_it session_thread_it;
2253         session_thread_data_map_it session_thread_it_end;
2254         recive_data_map_it recive_data_it;
2255
2256         if (schedule_tcp.empty())
2257         {
2258             std::cerr << "protocol_module_sessionless::handle_realserver_select() : Schedule_tcp function is empty." << std::endl;
2259             boost::format formatter("Schedule_tcp function is empty. thread id : %d.");
2260             formatter % boost::this_thread::get_id();
2261             putLogError(100037, formatter.str(), __FILE__, __LINE__ );
2262             /*-------- DEBUG LOG --------*/
2263             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2264             {
2265                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2266                                         "handle_realserver_select(const boost::thread::id thread_id, "
2267                                         "boost::asio::ip::tcp::endpoint & rs_endpoint)"
2268                                         " : return_value = %d. thread id : %d.");
2269                 formatter % FINALIZE % boost::this_thread::get_id();
2270                 putLogDebug(100074, formatter.str(), __FILE__, __LINE__ );
2271             }
2272             /*------DEBUG LOG END------*/
2273             return FINALIZE;
2274         }
2275
2276         try
2277         {
2278             {
2279                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
2280
2281                 session_thread_it = session_thread_data_map.find(thread_id);
2282                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2283                 {
2284                     boost::format formatter("Invalid thread id. thread id : %d.");
2285                     formatter % boost::this_thread::get_id();
2286                     putLogError(100038, formatter.str(), __FILE__, __LINE__ );
2287                     throw -1;
2288                 }
2289
2290                 session_data = session_thread_it->second;
2291             }
2292
2293             //call schedule_module's schedule function, get realserver endpoint
2294             {
2295                 rs_list_scoped_lock scoped_lock(rs_list_lock, rs_list_unlock);
2296                 schedule_tcp(thread_id, rs_list_begin, rs_list_end, rs_list_next, rs_endpoint);
2297             }
2298
2299             /*-------- DEBUG LOG --------*/
2300             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2301             {
2302                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2303                                         "handle_realserver_select() : call schedule_tcp : "
2304                                         "rs_endpoint = [%s]:%d. thread id : %d.");
2305                 formatter % rs_endpoint.address().to_string() % rs_endpoint.port() % boost::this_thread::get_id();
2306                 putLogDebug(100075, formatter.str(), __FILE__, __LINE__ );
2307             }
2308             /*------DEBUG LOG END------*/
2309
2310             //endpoint decide
2311             if (rs_endpoint != tmp_endpoint)
2312             {
2313                 //save rs endpoint
2314                 session_data->target_endpoint = rs_endpoint;
2315
2316                 recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
2317                 if (unlikely(recive_data_it == session_data->recive_data_map.end()))
2318                 {
2319                     boost::format formatter("Invalid endpoint. thread id : %d.");
2320                     formatter % boost::this_thread::get_id();
2321                     putLogError(100039, formatter.str(), __FILE__, __LINE__ );
2322                     throw -1;
2323                 }
2324                 recive_data& recv_data = recive_data_it->second;
2325
2326                 send_status_it it = recv_data.send_status_list.begin();
2327                 send_status_it it_end = recv_data.send_status_list.end();
2328
2329                 it = find_if(it, it_end, data_send_possible());
2330                 if (it != it_end)
2331                 {
2332                     status = REALSERVER_CONNECT;
2333                 }
2334                 else
2335                 {
2336                     status = CLIENT_RECV;
2337                 }
2338             }
2339             else
2340             {
2341                 //set end flag on
2342                 session_data->sorry_flag = SORRY_FLAG_ON;
2343                 /*-------- DEBUG LOG --------*/
2344                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2345                 {
2346                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2347                                             "handle_realserver_select() : SORRY_FLAG_ON. thread id : %d.");
2348                     formatter % boost::this_thread::get_id();
2349                     putLogDebug(100076, formatter.str(), __FILE__, __LINE__ );
2350                 }
2351                 /*------DEBUG LOG END------*/
2352                 status = SORRYSERVER_SELECT;
2353             }
2354         }
2355         catch (int e)
2356         {
2357             /*-------- DEBUG LOG --------*/
2358             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2359             {
2360                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2361                                         "handle_realserver_select() : catch exception e = %d. thread id : %d.");
2362                 formatter % e % boost::this_thread::get_id();
2363                 putLogDebug(100077, formatter.str(), __FILE__, __LINE__ );
2364             }
2365             status = FINALIZE;
2366             /*------DEBUG LOG END------*/
2367         }
2368         catch (const std::exception& ex)
2369         {
2370             std::cerr << "protocol_module_sessionless::handle_realserver_select() : exception : error = " << ex.what() << "." << std::endl;
2371             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2372                                     "handle_realserver_select() : exception : error = %s. thread id : %d.");
2373             formatter % ex.what() % boost::this_thread::get_id();
2374             putLogError(100040, formatter.str(), __FILE__, __LINE__ );
2375             status = FINALIZE;
2376         }
2377         catch (...)
2378         {
2379             std::cerr << "protocol_module_sessionless::handle_realserver_select() : Unknown exception." << std::endl;
2380             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2381                                     "protocol_module_sessionless::handle_realserver_select() : "
2382                                     "Unknown exception. thread id : %d.");
2383             formatter % boost::this_thread::get_id();
2384             putLogError(100041, formatter.str(), __FILE__, __LINE__ );
2385             status = FINALIZE;
2386         }
2387
2388         /*-------- DEBUG LOG --------*/
2389         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2390         {
2391             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2392                                     "handle_realserver_select(const boost::thread::id thread_id, "
2393                                     "boost::asio::ip::tcp::endpoint & rs_endpoint)"
2394                                     " : return_value = %d. thread id : %d.");
2395             formatter % status % boost::this_thread::get_id();
2396             putLogDebug(100078, formatter.str(), __FILE__, __LINE__ );
2397         }
2398         /*------DEBUG LOG END------*/
2399
2400         return status;
2401     }
2402
2403     //! called from after realserver select
2404     //! @param[in]    upstream thread id
2405     //! @param[out]    realserver UDP endpoint
2406     //! @param[out]    sendbuffer reference
2407     //! @param[out]    send data length
2408     //! @return        session use EVENT mode.
2409     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_select(
2410         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint& rs_endpoint, boost::array<char,
2411         MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
2412     {
2413         /*-------- DEBUG LOG --------*/
2414         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2415         {
2416             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2417                                     "handle_realserver_select(const boost::thread::id thread_id, "
2418                                     "boost::asio::ip::udp::endpoint& rs_endpoint, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2419                                     "size_t& datalen) : "
2420                                     "return_value = %d. thread id : %d.");
2421             formatter % STOP % boost::this_thread::get_id();
2422             putLogDebug(100079, formatter.str(), __FILE__, __LINE__ );
2423         }
2424         /*------DEBUG LOG END------*/
2425         return STOP;
2426     }
2427     //! called from after realserver connect
2428     //! @param[in]    upstream thread id
2429     //! @param[out]    sendbuffer reference
2430     //! @param[out]    send data length
2431     //! @return        session use EVENT mode.
2432     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_connect(
2433         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
2434     {
2435         /*-------- DEBUG LOG --------*/
2436         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2437         {
2438             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2439                                     "handle_realserver_connect(const boost::thread::id thread_id, "
2440                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
2441                                     "thread_id = %d.");
2442             formatter % thread_id;
2443             putLogDebug(100080, formatter.str(), __FILE__, __LINE__ );
2444         }
2445         /*------DEBUG LOG END------*/
2446
2447         EVENT_TAG status = FINALIZE;
2448         bool ret = false;
2449         size_t header_offset = 0;
2450         size_t header_offset_len = 0;
2451         size_t send_buffer_remian_size = 0;
2452         size_t copy_size = 0;
2453         const int send_buffer_end_size = sendbuffer.max_size();
2454         const std::string http_header = "";
2455         const std::string str_forword_for = "X-Forwarded-For";
2456         thread_data_ptr session_data;
2457         session_thread_data_map_it session_thread_it;
2458         recive_data_map_it recive_data_it;
2459
2460         try
2461         {
2462             {
2463                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
2464
2465                 //thread id check
2466                 session_thread_it = session_thread_data_map.find(thread_id);
2467                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2468                 {
2469                     boost::format formatter("Invalid thread id. thread id : %d.");
2470                     formatter % boost::this_thread::get_id();
2471                     putLogError(100042, formatter.str(), __FILE__, __LINE__ );
2472                     throw -1;
2473                 }
2474
2475                 session_data = session_thread_it->second;
2476             }
2477
2478             //endpoint check
2479             recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
2480             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
2481             {
2482                 boost::format formatter("Invalid endpoint. thread id : %d.");
2483                 formatter % boost::this_thread::get_id();
2484                 putLogError(100043, formatter.str(), __FILE__, __LINE__ );
2485                 throw -1;
2486             }
2487
2488             //recive_buffer pointer check
2489             recive_data& recv_data = recive_data_it->second;
2490             if (unlikely(recv_data.recive_buffer == NULL))
2491             {
2492                 boost::format formatter("Invalid pointer. thread id : %d.");
2493                 formatter % boost::this_thread::get_id();
2494                 putLogError(100044, formatter.str(), __FILE__, __LINE__ );
2495                 throw -1;
2496             }
2497
2498             //send list check
2499             send_status_it it = recv_data.send_status_list.begin();
2500             send_status_it it_end = recv_data.send_status_list.end();
2501             it = find_if(it, it_end, data_send_possible());
2502             if (unlikely(it == it_end))
2503             {
2504                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
2505                 formatter % boost::this_thread::get_id();
2506                 putLogError(100045, formatter.str(), __FILE__, __LINE__ );
2507                 throw -1;
2508             }
2509
2510             //send buffer rest size initialization
2511             send_buffer_remian_size = send_buffer_end_size;
2512
2513             //edit_division flag on
2514             if (it->edit_division == EDIT_DIVISION_EDIT  && forwarded_for == FORWARDED_FOR_ON)
2515             {
2516                 //edit list is empty
2517                 if (it->edit_data_list.empty())
2518                 {
2519                     //edit data create
2520                     edit_data edata;
2521                     edata.data_size = 0;
2522                     edata.insert_posission = 0;
2523                     edata.replace_size = 0;
2524                     //search X-Forwarded-For header
2525                     ret = find_http_header(recv_data.recive_buffer + it->send_offset, it->send_possible_size,
2526                                            str_forword_for, header_offset, header_offset_len);
2527                     /*-------- DEBUG LOG --------*/
2528                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2529                     {
2530                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2531                                                 "handle_realserver_connect() : call find_http_header : "
2532                                                 "return_value = %d. thread id : %d.");
2533                         formatter % static_cast<int>(ret) % boost::this_thread::get_id();
2534                         putLogDebug(100081, formatter.str(), __FILE__, __LINE__ );
2535                     }
2536                     /*------DEBUG LOG END------*/
2537                     //search http header result is OK
2538                     if (ret)
2539                     {
2540                         //edit X-Forwarded-For header, set it to edata.data
2541                         edata.data.assign(recv_data.recive_buffer + it->send_offset + header_offset, header_offset_len);
2542                         edata.data += ", ";
2543                         edata.data += session_data->client_endpoint_tcp.address().to_string();
2544                         //save new X-Forwarded-For header offset
2545                         edata.insert_posission = header_offset;
2546                         //save new X-Forwarded-For header length
2547                         edata.data_size = edata.data.size();
2548                         //save old X-Forwarded-For header length
2549                         edata.replace_size = header_offset_len;
2550                     }
2551                     //search http header result is NG
2552                     else
2553                     {
2554                         //search whole http header, get whole http header's offset and length
2555                         ret = find_http_header(recv_data.recive_buffer + it->send_offset, it->send_possible_size, "",
2556                                                header_offset, header_offset_len);
2557                         /*-------- DEBUG LOG --------*/
2558                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2559                         {
2560                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2561                                                     "handle_realserver_connect() : call find_http_header : "
2562                                                     "return_value = %d. thread id : %d.");
2563                             formatter % static_cast<int>(ret) % boost::this_thread::get_id();
2564                             putLogDebug(100082, formatter.str(), __FILE__, __LINE__ );
2565                         }
2566                         /*------DEBUG LOG END------*/
2567                         if (!ret)
2568                         {
2569                             boost::format formatter("find_http_header() function failure. thread id : %d.");
2570                             formatter % boost::this_thread::get_id();
2571                             putLogError(100046, formatter.str(), __FILE__, __LINE__ );
2572                             /*-------- DEBUG LOG --------*/
2573                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2574                             {
2575                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2576                                                         "handle_realserver_connect(const boost::thread::id thread_id, "
2577                                                         "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2578                                                         "size_t& datalen) : return_value = %d. thread id : %d.");
2579                                 formatter % FINALIZE % boost::this_thread::get_id();
2580                                 putLogDebug(100083, formatter.str(), __FILE__, __LINE__ );
2581                             }
2582                             /*------DEBUG LOG END------*/
2583                             return FINALIZE;
2584                         }
2585                         //create X-Forwarded-For header, put it to edata.data
2586                         edata.data = str_forword_for;
2587                         edata.data += ": ";
2588                         edata.data += session_data->client_endpoint_tcp.address().to_string();
2589                         edata.data += "\r\n";
2590                         //save new X-Forwarded-For header offset
2591                         edata.insert_posission = header_offset;
2592                         //save new X-Forwarded-For header length
2593                         edata.data_size = edata.data.size();
2594                         //save old X-Forwarded-For header length
2595                         edata.replace_size = 0;
2596                     }
2597
2598                     //add to edit_data_list
2599                     it->edit_data_list.push_back(edata);
2600                 }
2601
2602                 /*-------- DEBUG LOG --------*/
2603                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2604                 {
2605                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2606                                             "handle_realserver_connect() : Copy data loop start. thread id : %d.");
2607                     formatter % boost::this_thread::get_id();
2608                     putLogDebug(100084, formatter.str(), __FILE__, __LINE__ );
2609                 }
2610                 /*------DEBUG LOG END------*/
2611                 while (true)
2612                 {
2613                     //edit_data_list is empty
2614                     if (it->edit_data_list.empty())
2615                     {
2616                         //set edit_division flag on
2617                         it->edit_division = EDIT_DIVISION_NO_EDIT;
2618
2619                         if (send_buffer_remian_size > 0 && it->send_possible_size > 0)
2620                         {
2621                             //send_buffer_remian_size is larger
2622                             if (send_buffer_remian_size >= it->send_possible_size)
2623                             {
2624                                 copy_size = it->send_possible_size;
2625                                 /*-------- DEBUG LOG --------*/
2626                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2627                                 {
2628                                     std::string datadump;
2629                                     dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
2630                                                 it->send_possible_size, datadump);
2631                                     boost::format formatter(
2632                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2633                                         "handle_realserver_connect() : before memcpy (data dump) : "
2634                                         "data begin = %d, data_size = %d, data = %s");
2635                                     formatter % (it->send_offset + it->send_end_size)
2636                                     % copy_size % datadump;
2637                                     putLogDebug(100085, formatter.str(), __FILE__, __LINE__ );
2638                                 }
2639                                 /*------DEBUG LOG END------*/
2640                                 //copy data from recive_buffer to sendbuffer by sending_possible size
2641                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2642                                        recv_data.recive_buffer + it->send_offset + it->send_end_size,
2643                                        it->send_possible_size);
2644                                 /*-------- DEBUG LOG --------*/
2645                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2646                                 {
2647                                     std::string datadump;
2648                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2649                                                 it->send_possible_size, datadump);
2650
2651                                     boost::format formatter(
2652                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2653                                         "handle_realserver_connect() : after memcpy (data dump) : "
2654                                         "data begin = %d, data_size = %d, data = %s");
2655                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
2656                                     % copy_size % datadump;
2657                                     putLogDebug(100086, formatter.str(), __FILE__, __LINE__ );
2658                                 }
2659                                 /*------DEBUG LOG END------*/
2660
2661                                 it->send_end_size += copy_size;
2662                                 it->send_possible_size = 0;
2663                                 send_buffer_remian_size -= copy_size;
2664                             }
2665                             //send_possible_size is larger
2666                             else
2667                             {
2668                                 copy_size = send_buffer_remian_size;
2669                                 /*-------- DEBUG LOG --------*/
2670                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2671                                 {
2672                                     std::string datadump;
2673                                     dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
2674                                                 copy_size, datadump);
2675
2676                                     boost::format formatter(
2677                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2678                                         "handle_realserver_connect() : before memcpy (data dump) : "
2679                                         "data begin = %d, data_size = %d, data = %s");
2680                                     formatter % (it->send_offset + it->send_end_size)
2681                                     % copy_size % datadump;
2682                                     putLogDebug(100087, formatter.str(), __FILE__, __LINE__ );
2683                                 }
2684                                 /*------DEBUG LOG END------*/
2685                                 //copy data from recive_buffer to sendbuffer by send buffer rest size
2686                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2687                                        recv_data.recive_buffer + it->send_offset + it->send_end_size, copy_size);
2688                                 /*-------- DEBUG LOG --------*/
2689                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2690                                 {
2691                                     std::string datadump;
2692                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2693                                                 copy_size, datadump);
2694
2695                                     boost::format formatter(
2696                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2697                                         "handle_realserver_connect() : after memcpy (data dump) : "
2698                                         "data begin = %d, data_size = %d, data = %s");
2699                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
2700                                     % copy_size % datadump;
2701                                     putLogDebug(100088, formatter.str(), __FILE__, __LINE__ );
2702                                 }
2703                                 /*------DEBUG LOG END------*/
2704                                 it->send_end_size += copy_size;
2705                                 it->send_possible_size -= copy_size;
2706                                 send_buffer_remian_size = 0;
2707                             }
2708                         }
2709                         break;
2710                     }
2711                     //edit_data_list is not empty
2712                     else
2713                     {
2714                         //find the item in the list which has mininum insert_posission
2715                         std::list<edit_data>::iterator edit_min = std::min_element(it->edit_data_list.begin(),
2716                                 it->edit_data_list.end());
2717                         //send_buffer_remian_size is larger than data that before X-Forwarded-For
2718                         if (send_buffer_remian_size >= edit_min->insert_posission - it->send_end_size)
2719                         {
2720                             //copy data before X-Forwarded-For
2721                             copy_size = edit_min->insert_posission - it->send_end_size;
2722                             /*-------- DEBUG LOG --------*/
2723                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2724                             {
2725                                 std::string datadump;
2726                                 dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
2727                                             copy_size, datadump);
2728
2729                                 boost::format formatter(
2730                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2731                                     "handle_realserver_connect() : before memcpy (data dump) : "
2732                                     "data begin = %d, data_size = %d, data = %s");
2733                                 formatter % (it->send_offset + it->send_end_size)
2734                                 % copy_size % datadump;
2735                                 putLogDebug(100089, formatter.str(), __FILE__, __LINE__ );
2736                             }
2737                             /*------DEBUG LOG END------*/
2738                             memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2739                                    recv_data.recive_buffer + it->send_offset + it->send_end_size, copy_size);
2740                             /*-------- DEBUG LOG --------*/
2741                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2742                             {
2743                                 std::string datadump;
2744                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2745                                             copy_size, datadump);
2746                                 boost::format formatter(
2747                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2748                                     "handle_realserver_connect() : after memcpy (data dump) : "
2749                                     "data begin = %d, data_size = %d, data = %s");
2750                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
2751                                 % copy_size % datadump;
2752                                 putLogDebug(100090, formatter.str(), __FILE__, __LINE__ );
2753                             }
2754                             /*------DEBUG LOG END------*/
2755                             it->send_end_size += copy_size;
2756                             it->send_possible_size -= copy_size;
2757                             send_buffer_remian_size -= copy_size;
2758
2759                             //there is remain buffer for copy X-Forwarded-For
2760                             if (send_buffer_remian_size >= edit_min->data_size)
2761                             {
2762                                 /*-------- DEBUG LOG --------*/
2763                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2764                                 {
2765                                     std::string datadump;
2766                                     dump_memory(edit_min->data.c_str(),
2767                                                 edit_min->data_size, datadump);
2768
2769                                     boost::format formatter(
2770                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2771                                         "handle_realserver_connect() : before memcpy (data dump) : "
2772                                         "data begin = 0, data_size = %d, data = %s");
2773                                     formatter % edit_min->data_size % datadump;
2774                                     putLogDebug(100091, formatter.str(), __FILE__, __LINE__ );
2775                                 }
2776                                 /*------DEBUG LOG END------*/
2777                                 //copy  X-Forwarded-For
2778                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2779                                        edit_min->data.c_str(), edit_min->data_size);
2780                                 /*-------- DEBUG LOG --------*/
2781                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2782                                 {
2783                                     std::string datadump;
2784                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2785                                                 edit_min->data_size, datadump);
2786                                     boost::format formatter(
2787                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2788                                         "handle_realserver_connect() : after memcpy (data dump) : "
2789                                         "data begin = %d, data_size = %d, data = %s");
2790                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
2791                                     % edit_min->data_size % datadump;
2792                                     putLogDebug(100092, formatter.str(), __FILE__, __LINE__ );
2793                                 }
2794                                 /*------DEBUG LOG END------*/
2795                                 it->send_end_size += edit_min->replace_size;
2796                                 it->send_possible_size -= edit_min->replace_size;
2797                                 send_buffer_remian_size -= edit_min->data_size;
2798                                 it->edit_data_list.erase(edit_min);
2799                             }
2800                             //
2801                             else
2802                             {
2803                                 break;
2804                             }
2805                         }
2806                         //data that before X-Forwarded-For is larger than send_buffer_remian_size
2807                         else
2808                         {
2809                             copy_size = send_buffer_remian_size;
2810                             /*-------- DEBUG LOG --------*/
2811                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2812                             {
2813                                 std::string datadump;
2814                                 dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
2815                                             copy_size, datadump);
2816
2817                                 boost::format formatter(
2818                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2819                                     "handle_realserver_connect() : before memcpy (data dump) : "
2820                                     "data begin = %d, data_size = %d, data = %s");
2821                                 formatter % (it->send_offset + it->send_end_size)
2822                                 % copy_size % datadump;
2823                                 putLogDebug(100093, formatter.str(), __FILE__, __LINE__ );
2824                             }
2825                             /*------DEBUG LOG END------*/
2826                             //copy data as large as possible
2827                             memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2828                                    recv_data.recive_buffer + it->send_offset + it->send_end_size, copy_size);
2829                             /*-------- DEBUG LOG --------*/
2830                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2831                             {
2832                                 std::string datadump;
2833                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2834                                             copy_size, datadump);
2835
2836                                 boost::format formatter(
2837                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2838                                     "handle_realserver_connect() : after memcpy (data dump) : "
2839                                     "data begin = %d, data_size = %d, data = %s");
2840                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
2841                                 % copy_size % datadump;
2842                                 putLogDebug(100094, formatter.str(), __FILE__, __LINE__ );
2843                             }
2844                             /*------DEBUG LOG END------*/
2845
2846                             it->send_end_size += copy_size;
2847                             it->send_possible_size -= copy_size;
2848                             send_buffer_remian_size -= copy_size;
2849                             break;
2850                         }
2851                     }
2852                 }
2853                 /*-------- DEBUG LOG --------*/
2854                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2855                 {
2856                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2857                                             "handle_realserver_connect() : Copy data loop end. thread id : %d.");
2858                     formatter % boost::this_thread::get_id();
2859                     putLogDebug(100095, formatter.str(), __FILE__, __LINE__ );
2860                 }
2861                 /*------DEBUG LOG END------*/
2862             }
2863             //edit_division flag is off
2864             else
2865             {
2866                 //copy data as large as possible
2867                 //send_possible_size is larger
2868                 if (send_buffer_remian_size >= it->send_possible_size)
2869                 {
2870                     copy_size = it->send_possible_size;
2871                     /*-------- DEBUG LOG --------*/
2872                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2873                     {
2874                         std::string datadump;
2875                         dump_memory(recv_data.recive_buffer + it->send_offset,
2876                                     copy_size, datadump);
2877                         boost::format formatter(
2878                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2879                             "handle_realserver_connect() : before memcpy (data dump) : "
2880                             "data begin = %d, data_size = %d, data = %s");
2881                         formatter % it->send_offset
2882                         % copy_size % datadump;
2883                         putLogDebug(100096, formatter.str(), __FILE__, __LINE__ );
2884                     }
2885                     /*------DEBUG LOG END------*/
2886                     //copy data by send_possible size
2887                     memcpy(sendbuffer.data(), recv_data.recive_buffer + it->send_offset, copy_size);
2888                     /*-------- DEBUG LOG --------*/
2889                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2890                     {
2891                         std::string datadump;
2892                         dump_memory(sendbuffer.data(), copy_size, datadump);
2893                         boost::format formatter(
2894                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2895                             "handle_realserver_connect() : after memcpy (data dump) : "
2896                             "data begin = 0, data_size = %d, data = %s");
2897                         formatter % copy_size % datadump;
2898                         putLogDebug(100097, formatter.str(), __FILE__, __LINE__ );
2899                     }
2900                     /*------DEBUG LOG END------*/
2901                     it->send_end_size = copy_size;
2902                     it->send_possible_size = 0;
2903                     send_buffer_remian_size -= copy_size;
2904                 }
2905                 //buffer rest size is larger
2906                 else
2907                 {
2908                     /*-------- DEBUG LOG --------*/
2909                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2910                     {
2911                         std::string datadump;
2912                         dump_memory(recv_data.recive_buffer + it->send_offset, send_buffer_remian_size, datadump);
2913
2914                         boost::format formatter(
2915                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2916                             "handle_realserver_connect() : before memcpy (data dump) : "
2917                             "data begin = %d, data_size = %d, data = %s");
2918                         formatter % it->send_offset
2919                         % send_buffer_remian_size % datadump;
2920                         putLogDebug(100098, formatter.str(), __FILE__, __LINE__ );
2921                     }
2922                     /*------DEBUG LOG END------*/
2923                     //copy data by buffer rest size
2924                     memcpy(sendbuffer.data(), recv_data.recive_buffer + it->send_offset, send_buffer_remian_size);
2925                     /*-------- DEBUG LOG --------*/
2926                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2927                     {
2928                         std::string datadump;
2929                         dump_memory(sendbuffer.data(), send_buffer_remian_size, datadump);
2930                         boost::format formatter(
2931                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2932                             "handle_realserver_connect() : after memcpy (data dump) : "
2933                             "data begin = 0, data_size = %d, data = %s");
2934                         formatter % send_buffer_remian_size % datadump;
2935                         putLogDebug(100099, formatter.str(), __FILE__, __LINE__ );
2936                     }
2937                     /*------DEBUG LOG END------*/
2938                     it->send_end_size = send_buffer_remian_size;
2939                     it->send_possible_size -= send_buffer_remian_size;
2940                     send_buffer_remian_size = 0;
2941                 }
2942             }
2943
2944             //set copyed data length
2945             datalen = send_buffer_end_size - send_buffer_remian_size;
2946             status = REALSERVER_SEND;
2947         }
2948         catch (int e)
2949         {
2950             /*-------- DEBUG LOG --------*/
2951             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2952             {
2953                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2954                                         "handle_realserver_connect() : catch exception e = %d. thread id : %d.");
2955                 formatter % e % boost::this_thread::get_id();
2956                 putLogDebug(100100, formatter.str(), __FILE__, __LINE__ );
2957             }
2958             status = FINALIZE;
2959             /*------DEBUG LOG END------*/
2960         }
2961         catch (const std::exception& ex)
2962         {
2963             std::cerr << "protocol_module_sessionless::handle_realserver_connect() : exception : error = " << ex.what() << "." << std::endl;
2964             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2965                                     "handle_realserver_connect() : exception : error = %s. thread id : %d.");
2966             formatter % ex.what() % boost::this_thread::get_id();
2967             putLogError(100047, formatter.str(), __FILE__, __LINE__ );
2968             status = FINALIZE;
2969         }
2970         catch (...)
2971         {
2972             std::cerr << "protocol_module_sessionless::handle_realserver_connect() : Unknown exception." << std::endl;
2973             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2974                                     "handle_realserver_connect() : Unknown exception. thread id : %d.");
2975             formatter % boost::this_thread::get_id();
2976             putLogError(100048, formatter.str(), __FILE__, __LINE__ );
2977             status = FINALIZE;
2978         }
2979
2980         /*-------- DEBUG LOG --------*/
2981         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2982         {
2983             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2984                                     "handle_realserver_connect(const boost::thread::id thread_id, "
2985                                     "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2986                                     "size_t& datalen) : return_value = %d. thread id : %d.");
2987             formatter % status % boost::this_thread::get_id();
2988             putLogDebug(100101, formatter.str(), __FILE__, __LINE__ );
2989         }
2990         /*------DEBUG LOG END------*/
2991
2992         return status;
2993     }
2994
2995     //! called from after realserver connection fail
2996     //! @param[in]    upstream thread id
2997     //! @param[in]    fail realserver endpoint reference
2998     //! @return        session use EVENT mode.
2999     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_connection_fail(
3000         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint)
3001     {
3002         /*-------- DEBUG LOG --------*/
3003         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3004         {
3005             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3006                                     "handle_realserver_connection_fail(const boost::thread::id thread_id, "
3007                                     "const boost::asio::ip::tcp::endpoint & rs_endpoint) : "
3008                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
3009             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
3010             putLogDebug(100102, formatter.str(), __FILE__, __LINE__ );
3011         }
3012         /*------DEBUG LOG END------*/
3013
3014         EVENT_TAG status = FINALIZE;
3015         thread_data_ptr session_data;
3016         session_thread_data_map_it session_thread_it;
3017
3018         try
3019         {
3020             boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
3021
3022             session_thread_it = session_thread_data_map.find(thread_id);
3023             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3024             {
3025                 boost::format formatter("Invalid thread id. thread id : %d.");
3026                 formatter % boost::this_thread::get_id();
3027                 putLogError(100049, formatter.str(), __FILE__, __LINE__ );
3028                 throw -1;
3029             }
3030
3031             session_data = session_thread_it->second;
3032
3033             //set end flag ON
3034             session_data->end_flag = END_FLAG_ON;
3035             /*-------- DEBUG LOG --------*/
3036             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3037             {
3038                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3039                                         "handle_realserver_connection_fail() : END_FLAG_ON. thread id : %d.");
3040                 formatter % boost::this_thread::get_id();
3041                 putLogDebug(100103, formatter.str(), __FILE__, __LINE__ );
3042             }
3043             /*------DEBUG LOG END------*/
3044             status = CLIENT_DISCONNECT;
3045         }
3046         catch (int e)
3047         {
3048             /*-------- DEBUG LOG --------*/
3049             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3050             {
3051                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3052                                         "handle_realserver_connection_fail() : catch exception e = %d. thread id : %d.");
3053                 formatter % e % boost::this_thread::get_id();
3054                 putLogDebug(100104, formatter.str(), __FILE__, __LINE__ );
3055             }
3056             status = FINALIZE;
3057             /*------DEBUG LOG END------*/
3058         }
3059         catch (std::exception& ex)
3060         {
3061             std::cerr << "protocol_module_sessionless::handle_realserver_connection_fail() : exception : error = " << ex.what() << "." << std::endl;
3062             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3063                                     "handle_realserver_connection_fail() : exception : error = %s. thread id : %d.");
3064             formatter % ex.what() % boost::this_thread::get_id();
3065             putLogError(100050, formatter.str(), __FILE__, __LINE__ );
3066             status = FINALIZE;
3067         }
3068         catch (...)
3069         {
3070             std::cerr << "protocol_module_sessionless::handle_realserver_connection_fail() : Unknown exception." << std::endl;
3071             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3072                                     "handle_realserver_connection_fail() : Unknown exception. thread id : %d.");
3073             formatter % boost::this_thread::get_id();
3074             putLogError(100051, formatter.str(), __FILE__, __LINE__ );
3075             status = FINALIZE;
3076         }
3077
3078         /*-------- DEBUG LOG --------*/
3079         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3080         {
3081             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3082                                     "handle_realserver_connection_fail(const boost::thread::id thread_id, "
3083                                     "const boost::asio::ip::tcp::endpoint & rs_endpoint) : return_value = %d. thread id : %d.");
3084             formatter % status % boost::this_thread::get_id();
3085             putLogDebug(100105, formatter.str(), __FILE__, __LINE__ );
3086         }
3087         /*------DEBUG LOG END------*/
3088         return status;
3089     }
3090     //! called from after realserver send.
3091     //! @param[in]    upstream thread id
3092     //! @return        session use EVENT mode.
3093     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_send(
3094         const boost::thread::id thread_id)
3095     {
3096         /*-------- DEBUG LOG --------*/
3097         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3098         {
3099             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3100                                     "handle_realserver_send(const boost::thread::id thread_id) : "
3101                                     "thread_id = %d.");
3102             formatter % thread_id;
3103             putLogDebug(100106, formatter.str(), __FILE__, __LINE__ );
3104         }
3105         /*------DEBUG LOG END------*/
3106         EVENT_TAG status = FINALIZE;
3107         thread_data_ptr session_data;
3108         session_thread_data_map_it session_thread_it;
3109         recive_data_map_it recive_data_it;
3110
3111         try
3112         {
3113             {
3114                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3115
3116                 //thread_id check
3117                 session_thread_it = session_thread_data_map.find(thread_id);
3118                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3119                 {
3120                     boost::format formatter("Invalid thread id. thread id : %d.");
3121                     formatter % boost::this_thread::get_id();
3122                     putLogError(100052, formatter.str(), __FILE__, __LINE__ );
3123                     throw -1;
3124                 }
3125
3126                 session_data = session_thread_it->second;
3127             }
3128
3129             //endpoint check
3130             recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
3131             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
3132             {
3133                 boost::format formatter("Invalid endpoint. thread id : %d.");
3134                 formatter % boost::this_thread::get_id();
3135                 putLogError(100053, formatter.str(), __FILE__, __LINE__ );
3136                 throw -1;
3137             }
3138
3139             recive_data& recv_data = recive_data_it->second;
3140
3141             send_status_it it = recv_data.send_status_list.begin();
3142             send_status_it it_end = recv_data.send_status_list.end();
3143
3144             //status list check
3145             it = std::adjacent_find(it, it_end, data_send_list_incorrect());
3146             if (unlikely(it != it_end))
3147             {
3148                 boost::format formatter("Sending possible data is invalid. thread id : %d.");
3149                 formatter % boost::this_thread::get_id();
3150                 putLogError(100054, formatter.str(), __FILE__, __LINE__ );
3151                 throw -1;
3152             }
3153
3154             //status list check
3155             it = recv_data.send_status_list.begin();
3156             it = std::find_if(it, it_end, data_send_ok());
3157             if (unlikely(it == it_end))
3158             {
3159                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
3160                 formatter % boost::this_thread::get_id();
3161                 putLogError(100055, formatter.str(), __FILE__, __LINE__ );
3162                 throw -1;
3163             }
3164
3165             //sending possible data is exist
3166             if (it->send_possible_size > 0)
3167             {
3168                 //status remain SEND_OK
3169                 it->status = SEND_OK;
3170                 //offset recalc
3171                 it->send_offset += it->send_end_size;
3172
3173                 //insert_posission recalc
3174                 for (std::list<edit_data>::iterator list_it = it->edit_data_list.begin(); list_it
3175                         != it->edit_data_list.end(); ++list_it)
3176                 {
3177                     list_it->insert_posission -= it->send_end_size;
3178                 }
3179
3180                 //send_end_size recalc
3181                 it->send_end_size = 0;
3182             }
3183             //sending possible data is not exist
3184             else
3185             {
3186                 //can recive from client continue
3187                 if (it->send_rest_size > 0)
3188                 {
3189                     //change status from SEND_OK to SEND_CONTINUE
3190                     it->status = SEND_CONTINUE;
3191                 }
3192                 //can not recive from client continue
3193                 else
3194                 {
3195                     //change status from SEND_OK to SEND_END
3196                     it->status = SEND_END;
3197                 }
3198             }
3199
3200             it = recv_data.send_status_list.begin();
3201             it = find_if(it, it_end, data_send_ok());
3202             //send_ok item is exist
3203             if (it != it_end)
3204             {
3205                 status = REALSERVER_CONNECT;
3206             }
3207             //send_ok item is exist
3208             else
3209             {
3210                 status = CLIENT_RECV;
3211             }
3212
3213         }
3214         catch (int e)
3215         {
3216             /*-------- DEBUG LOG --------*/
3217             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3218             {
3219                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3220                                         "handle_realserver_send() : catch exception e = %d. thread id : %d.");
3221                 formatter % e % boost::this_thread::get_id();
3222                 putLogDebug(100107, formatter.str(), __FILE__, __LINE__ );
3223             }
3224             status = FINALIZE;
3225             /*------DEBUG LOG END------*/
3226         }
3227         catch (const std::exception& ex)
3228         {
3229             std::cerr << "protocol_module_sessionless::handle_realserver_send() : exception : error = " << ex.what() << "." << std::endl;
3230             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3231                                     "handle_realserver_send() : exception : error = %s. thread id : %d.");
3232             formatter % ex.what() % boost::this_thread::get_id();
3233             putLogError(100056, formatter.str(), __FILE__, __LINE__ );
3234
3235             status = FINALIZE;
3236         }
3237         catch (...)
3238         {
3239             std::cerr << "protocol_module_sessionless::handle_realserver_send() : Unknown exception." << std::endl;
3240             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3241                                     "handle_realserver_send() : Unknown exception. thread id : %d.");
3242             formatter % boost::this_thread::get_id();
3243             putLogError(100057, formatter.str(), __FILE__, __LINE__ );
3244             status = FINALIZE;
3245         }
3246
3247         /*-------- DEBUG LOG --------*/
3248         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3249         {
3250             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3251                                     "handle_realserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
3252             formatter % status % boost::this_thread::get_id();
3253             putLogDebug(100108, formatter.str(), __FILE__, __LINE__ );
3254         }
3255         /*------DEBUG LOG END------*/
3256
3257         return status;
3258     }
3259
3260     //! called from after sorryserver select
3261     //! @param[in]    upstream thread id
3262     //! @param[in]    sorryserver endpiont reference
3263     //! @return        session use EVENT mode.
3264     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_select(
3265         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint& sorry_endpoint)
3266     {
3267         /*-------- DEBUG LOG --------*/
3268         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3269         {
3270             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3271                                     "handle_sorryserver_select(const boost::thread::id thread_id, "
3272                                     "boost::asio::ip::tcp::endpoint& sorry_endpoint) : "
3273                                     "thread_id = %d, sorry_endpoint = [%s]:%d.");
3274             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
3275             putLogDebug(100109, formatter.str(), __FILE__, __LINE__ );
3276         }
3277         /*------DEBUG LOG END------*/
3278         EVENT_TAG status = FINALIZE;
3279         boost::asio::ip::tcp::endpoint client_endpoint;
3280
3281         thread_data_ptr session_data;
3282         session_thread_data_map_it session_thread_it;
3283         recive_data_map_it recive_data_it;
3284
3285         try
3286         {
3287             boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3288
3289             session_thread_it = session_thread_data_map.find(thread_id);
3290             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second== NULL))
3291             {
3292                 boost::format formatter("Invalid thread id. thread id : %d.");
3293                 formatter % boost::this_thread::get_id();
3294                 putLogError(100058, formatter.str(), __FILE__, __LINE__ );
3295                 throw -1;
3296             }
3297
3298             session_data = session_thread_it->second;
3299             //set sorry_endpoint
3300             session_data->target_endpoint = sorry_endpoint;
3301
3302             //endpoint check
3303             recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
3304             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
3305             {
3306                 boost::format formatter("Invalid endpoint. thread id : %d.");
3307                 formatter % boost::this_thread::get_id();
3308                 putLogError(100059, formatter.str(), __FILE__, __LINE__ );
3309                 throw -1;
3310             }
3311
3312             recive_data& recv_data = recive_data_it->second;
3313
3314             send_status_it it = recv_data.send_status_list.begin();
3315             send_status_it it_end = recv_data.send_status_list.end();
3316
3317             it = find_if(it, it_end, data_send_possible());
3318             if (it != it_end)
3319             {
3320                 status = SORRYSERVER_CONNECT;
3321             }
3322             else
3323             {
3324                 status = CLIENT_RECV;
3325             }
3326
3327         }
3328         catch (int e)
3329         {
3330             /*-------- DEBUG LOG --------*/
3331             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3332             {
3333                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3334                                         "handle_sorryserver_select() : catch exception e = %d. thread id : %d.");
3335                 formatter % e % boost::this_thread::get_id();
3336                 putLogDebug(100110, formatter.str(), __FILE__, __LINE__ );
3337             }
3338             status = FINALIZE;
3339             /*------DEBUG LOG END------*/
3340         }
3341         catch (const std::bad_alloc& ex)
3342         {
3343             std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : exception : Could not allocate memory." << std::endl;
3344             boost::format formatter("Could not allocate memory. thread id : %d.");
3345             formatter % boost::this_thread::get_id();
3346             putLogError(100060, formatter.str(), __FILE__, __LINE__ );
3347             status = FINALIZE;
3348         }
3349         catch (const std::exception& ex)
3350         {
3351             std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : exception : error = " << ex.what() << "." << std::endl;
3352             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3353                                     "handle_sorryserver_select() : exception : error = %s. thread id : %d.");
3354             formatter % ex.what() % boost::this_thread::get_id();
3355             putLogError(100061, formatter.str(), __FILE__, __LINE__ );
3356             status = FINALIZE;
3357         }
3358         catch (...)
3359         {
3360             std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : Unknown exception." << std::endl;
3361             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3362                                     "handle_sorryserver_select() : Unknown exception. thread id : %d.");
3363             formatter % boost::this_thread::get_id();
3364             putLogError(100062, formatter.str(), __FILE__, __LINE__ );
3365             status = FINALIZE;
3366         }
3367
3368         /*-------- DEBUG LOG --------*/
3369         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3370         {
3371             boost::format
3372             formatter(
3373                 "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3374                 "handle_sorryserver_select(const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d."
3375                 " thread id : %d.");
3376             formatter % status % boost::this_thread::get_id();
3377             putLogDebug(100111, formatter.str(), __FILE__, __LINE__ );
3378         }
3379         /*------DEBUG LOG END------*/
3380         return status;
3381     }
3382
3383     //! called from after sorryserver connect
3384     //!    @param[in]    upstream thread id
3385     //! @param[out]    send buffer reference.
3386     //! @param[out]    send length
3387     //! @return        session use EVENT mode.
3388     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_connect(
3389         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
3390     {
3391         /*-------- DEBUG LOG --------*/
3392         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3393         {
3394             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3395                                     "handle_sorryserver_connect(const boost::thread::id thread_id, "
3396                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
3397                                     "thread_id = %d.");
3398             formatter % thread_id;
3399             putLogDebug(100112, formatter.str(), __FILE__, __LINE__ );
3400         }
3401         /*------DEBUG LOG END------*/
3402         EVENT_TAG status = FINALIZE;
3403         bool ret = false;
3404         size_t header_offset = 0;
3405         size_t header_offset_len = 0;
3406         size_t url_offset = 0;
3407         size_t url_offset_len = 0;
3408         size_t send_buffer_remian_size = 0;
3409         size_t copy_size = 0;
3410         const int send_buffer_end_size = sendbuffer.max_size();
3411         const std::string http_header = "";
3412         const std::string str_forword_for = "X-Forwarded-For";
3413         thread_data_ptr session_data;
3414         session_thread_data_map_it session_thread_it;
3415         recive_data_map_it recive_data_it;
3416
3417         try
3418         {
3419             {
3420                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3421
3422                 //thread id check
3423                 session_thread_it = session_thread_data_map.find(thread_id);
3424                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3425                 {
3426                     boost::format formatter("Invalid thread id. thread id : %d.");
3427                     formatter % boost::this_thread::get_id();
3428                     putLogError(100063, formatter.str(), __FILE__, __LINE__ );
3429                     throw -1;
3430                 }
3431
3432                 session_data = session_thread_it->second;
3433             }
3434
3435             //endpoint check
3436             recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
3437             if (unlikely(recive_data_it
3438                          == session_data->recive_data_map.end()))
3439             {
3440                 boost::format formatter("Invalid endpoint. thread id : %d.");
3441                 formatter % boost::this_thread::get_id();
3442                 putLogError(100064, formatter.str(), __FILE__, __LINE__ );
3443                 throw -1;
3444             }
3445
3446             //recive_buffer pointer check
3447             recive_data& recv_data = recive_data_it->second;
3448             if (unlikely(recv_data.recive_buffer == NULL))
3449             {
3450                 boost::format formatter("Invalid pointer. thread id : %d.");
3451                 formatter % boost::this_thread::get_id();
3452                 putLogError(100065, formatter.str(), __FILE__, __LINE__ );
3453                 throw -1;
3454             }
3455
3456             //send list check
3457             send_status_it it = recv_data.send_status_list.begin();
3458             send_status_it it_end = recv_data.send_status_list.end();
3459
3460             it = find_if(it, it_end, data_send_possible());
3461             if (unlikely(it == it_end))
3462             {
3463                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
3464                 formatter % boost::this_thread::get_id();
3465                 putLogError(100066, formatter.str(), __FILE__, __LINE__ );
3466                 throw -1;
3467             }
3468
3469             //send buffer rest size initialization
3470             send_buffer_remian_size = send_buffer_end_size;
3471
3472             //edit_division flag on
3473             if (it->edit_division == EDIT_DIVISION_EDIT)
3474             {
3475                 //edit list is empty
3476                 if (it->edit_data_list.empty())
3477                 {
3478                     //edit data create
3479                     edit_data edata;
3480                     edata.data_size = 0;
3481                     edata.insert_posission = 0;
3482                     edata.replace_size = 0;
3483                     //search uri
3484                     if (strlen(sorry_uri.data()) > 0)
3485                     {
3486                         ret = find_uri(recv_data.recive_buffer + it->send_offset, it->send_possible_size, url_offset,
3487                                        url_offset_len);
3488                         /*-------- DEBUG LOG --------*/
3489                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3490                         {
3491                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3492                                                     "handle_sorryserver_connect() : call find_uri : "
3493                                                     "return_value = %d. thread id : %d.");
3494                             formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3495                             putLogDebug(100113, formatter.str(), __FILE__, __LINE__ );
3496                         }
3497                         /*------DEBUG LOG END------*/
3498                         //search http header result is OK
3499                         if (ret)
3500                         {
3501                             //edit sorry_uri, put it to edata.data
3502                             edata.data = sorry_uri.data();
3503                             //save new uri offset
3504                             edata.insert_posission = url_offset;
3505                             //save new uri length
3506                             edata.data_size = edata.data.size();
3507                             //save old uri length
3508                             edata.replace_size = url_offset_len;
3509                             //add to edit_data_list
3510                             it->edit_data_list.push_back(edata);
3511                         }
3512                     }
3513
3514                     if (forwarded_for == FORWARDED_FOR_ON)
3515                     {
3516                         //search X-Forwarded-For header
3517                         ret = find_http_header(recv_data.recive_buffer + it->send_offset, it->send_possible_size,
3518                                                str_forword_for.c_str(), header_offset, header_offset_len);
3519                         /*-------- DEBUG LOG --------*/
3520                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3521                         {
3522                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3523                                                     "handle_sorryserver_connect() : call find_http_header : "
3524                                                     "return_value = %d. thread id : %d.");
3525                             formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3526                             putLogDebug(100114, formatter.str(), __FILE__, __LINE__ );
3527                         }
3528                         /*------DEBUG LOG END------*/
3529
3530                         //search http header result is OK
3531                         if (ret)
3532                         {
3533                             //edit X-Forwarded-For header, put it to edata.data
3534                             edata.data.assign(recv_data.recive_buffer + it->send_offset + header_offset, header_offset_len);
3535                             edata.data += ", ";
3536                             edata.data += session_data->client_endpoint_tcp.address().to_string();
3537                             //save new X-Forwarded-For header offset
3538                             edata.insert_posission = header_offset;
3539                             //save new X-Forwarded-For header length
3540                             edata.data_size = edata.data.size();
3541                             //save old X-Forwarded-For header length
3542                             edata.replace_size = header_offset_len;
3543                         }
3544                         //search http header result is NG
3545                         else
3546                         {
3547                             //search whole http header, get whole http header's offset and length
3548                             ret = find_http_header(recv_data.recive_buffer + it->send_offset, it->send_possible_size, "",
3549                                                    header_offset, header_offset_len);
3550                             /*-------- DEBUG LOG --------*/
3551                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3552                             {
3553                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3554                                                         "handle_sorryserver_connect() : call find_http_header : "
3555                                                         "return_value = %d. thread id : %d.");
3556                                 formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3557                                 putLogDebug(100115, formatter.str(), __FILE__, __LINE__ );
3558                             }
3559                             /*------DEBUG LOG END------*/
3560                             if (!ret)
3561                             {
3562                                 boost::format formatter("find_http_header() function failure. thread id : %d.");
3563                                 formatter % boost::this_thread::get_id();
3564                                 putLogError(100067, formatter.str(), __FILE__, __LINE__ );
3565                                 /*-------- DEBUG LOG --------*/
3566                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3567                                 {
3568                                     boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3569                                                             "handle_sorryserver_connect(const boost::thread::id thread_id, "
3570                                                             "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
3571                                                             "size_t& datalen) : return_value = %d. thread id : %d.");
3572                                     formatter % FINALIZE % boost::this_thread::get_id();
3573                                     putLogDebug(100116, formatter.str(), __FILE__, __LINE__ );
3574                                 }
3575                                 /*------DEBUG LOG END------*/
3576                                 return FINALIZE;
3577                             }
3578                             //create X-Forwarded-For header, set it to edata.data
3579                             edata.data = str_forword_for;
3580                             edata.data += ": ";
3581                             edata.data += session_data->client_endpoint_tcp.address().to_string();
3582                             edata.data += "\r\n";
3583                             //save new X-Forwarded-For header offset
3584                             edata.insert_posission = header_offset;
3585                             //save new X-Forwarded-For header length
3586                             edata.data_size = edata.data.size();
3587                             //save old X-Forwarded-For header length
3588                             edata.replace_size = 0;
3589                         }
3590
3591                         //add to edit_data_list
3592                         it->edit_data_list.push_back(edata);
3593                     }
3594                 }
3595
3596                 /*-------- DEBUG LOG --------*/
3597                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3598                 {
3599                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3600                                             "handle_sorryserver_connect() : Copy data loop start. thread id : %d.");
3601                     formatter % boost::this_thread::get_id();
3602                     putLogDebug(100117, formatter.str(), __FILE__, __LINE__ );
3603                 }
3604                 /*------DEBUG LOG END------*/
3605                 while (true)
3606                 {
3607                     //edit_data_list is empty
3608                     if (it->edit_data_list.empty())
3609                     {
3610                         //set edit_division flag off
3611                         it->edit_division = EDIT_DIVISION_NO_EDIT;
3612
3613                         if (send_buffer_remian_size > 0 && it->send_possible_size > 0)
3614                         {
3615                             //send_buffer_remian_size is larger
3616                             if (send_buffer_remian_size > it->send_possible_size)
3617                             {
3618                                 //copy data from recive_buffer to sendbuffer by sending_possible size
3619                                 copy_size = it->send_possible_size;
3620                                 /*-------- DEBUG LOG --------*/
3621                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3622                                 {
3623                                     std::string datadump;
3624                                     dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
3625                                                 copy_size, datadump);
3626                                     boost::format formatter(
3627                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3628                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3629                                         "data begin = %d, data_size = %d, data = %s");
3630                                     formatter % (it->send_offset + it->send_end_size)
3631                                     % copy_size % datadump;
3632                                     putLogDebug(100118, formatter.str(), __FILE__, __LINE__ );
3633                                 }
3634                                 /*------DEBUG LOG END------*/
3635                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3636                                        recv_data.recive_buffer + it->send_offset + it->send_end_size, copy_size);
3637                                 /*-------- DEBUG LOG --------*/
3638                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3639                                 {
3640                                     std::string datadump;
3641                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3642                                                 copy_size, datadump);
3643                                     boost::format formatter(
3644                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3645                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3646                                         "data begin = %d, data_size = %d, data = %s");
3647                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
3648                                     % copy_size % datadump;
3649                                     putLogDebug(100119, formatter.str(), __FILE__, __LINE__ );
3650                                 }
3651                                 /*------DEBUG LOG END------*/
3652                                 it->send_end_size += copy_size;
3653                                 it->send_possible_size = 0;
3654                                 send_buffer_remian_size -= copy_size;
3655                             }
3656                             //send_possible_size is larger
3657                             else
3658                             {
3659                                 copy_size = send_buffer_remian_size;
3660                                 /*-------- DEBUG LOG --------*/
3661                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3662                                 {
3663                                     std::string datadump;
3664                                     dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
3665                                                 copy_size, datadump);
3666                                     boost::format formatter(
3667                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3668                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3669                                         "data begin = %d, data_size = %d, data = %s");
3670                                     formatter % (it->send_offset + it->send_end_size)
3671                                     % copy_size % datadump;
3672                                     putLogDebug(100120, formatter.str(), __FILE__, __LINE__ );
3673                                 }
3674                                 /*------DEBUG LOG END------*/
3675                                 //copy data from recive_buffer to sendbuffer by send buffer rest size
3676                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3677                                        recv_data.recive_buffer + it->send_offset + it->send_end_size,
3678                                        copy_size);
3679                                 /*-------- DEBUG LOG --------*/
3680                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3681                                 {
3682                                     std::string datadump;
3683                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3684                                                 copy_size, datadump);
3685                                     boost::format formatter(
3686                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3687                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3688                                         "data begin = %d, data_size = %d, data = %s");
3689                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
3690                                     % copy_size % datadump;
3691                                     putLogDebug(100121, formatter.str(), __FILE__, __LINE__ );
3692                                 }
3693                                 /*------DEBUG LOG END------*/
3694                                 it->send_end_size += copy_size;
3695                                 it->send_possible_size -= copy_size;
3696                                 send_buffer_remian_size = 0;
3697                             }
3698                         }
3699
3700                         break;
3701                     }
3702                     //edit_data_list is not empty
3703                     else
3704                     {
3705                         //search item which insert_posission is mininum
3706                         std::list<edit_data>::iterator edit_min = std::min_element(it->edit_data_list.begin(),
3707                                 it->edit_data_list.end());
3708                         //send_buffer_remian_size is larger than data that before X-Forwarded-For/uri
3709                         if (send_buffer_remian_size >= edit_min->insert_posission - it->send_end_size)
3710                         {
3711                             //copy data before X-Forwarded-For/url
3712                             copy_size = edit_min->insert_posission - it->send_end_size;
3713                             /*-------- DEBUG LOG --------*/
3714                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3715                             {
3716                                 std::string datadump;
3717                                 dump_memory(recv_data.recive_buffer + it->send_offset + it->send_end_size,
3718                                             copy_size, datadump);
3719
3720                                 boost::format formatter(
3721                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3722                                     "handle_sorryserver_connect() : before memcpy (data dump) : "
3723                                     "data begin = %d, data_size = %d, data = %s");
3724                                 formatter % (it->send_offset + it->send_end_size)
3725                                 % copy_size % datadump;
3726                                 putLogDebug(100122, formatter.str(), __FILE__, __LINE__ );
3727                             }
3728                             /*------DEBUG LOG END------*/
3729                             memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3730                                    recv_data.recive_buffer + it->send_offset + it->send_end_size, copy_size);
3731                             /*-------- DEBUG LOG --------*/
3732                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3733                             {
3734                                 std::string datadump;
3735                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3736                                             copy_size, datadump);
3737                                 boost::format formatter(
3738                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3739                                     "handle_sorryserver_connect() : after memcpy (data dump) : "
3740                                     "data begin = %d, data_size = %d, data = %s");
3741                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
3742                                 % copy_size % datadump;
3743                                 putLogDebug(100123, formatter.str(), __FILE__, __LINE__ );
3744                             }
3745                             /*------DEBUG LOG END------*/
3746                             it->send_end_size += copy_size;
3747                             it->send_possible_size -= copy_size;
3748                             send_buffer_remian_size -= copy_size;
3749
3750                             //there is remain buffer for copy X-Forwarded-For/url
3751                             if (send_buffer_remian_size >= edit_min->data_size)
3752                             {
3753                                 /*-------- DEBUG LOG --------*/
3754                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3755                                 {
3756                                     std::string datadump;
3757                                     dump_memory(edit_min->data.c_str(),
3758                                                 edit_min->data_size, datadump);
3759                                     boost::format formatter(
3760                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3761                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3762                                         "data begin = 0, data_size = %d, data = %s");
3763                                     formatter % edit_min->data_size % datadump;
3764                                     putLogDebug(100124, formatter.str(), __FILE__, __LINE__ );
3765                                 }
3766                                 /*------DEBUG LOG END------*/
3767                                 //copy X-Forwarded-For/uri
3768                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3769                                        edit_min->data.c_str(), edit_min->data_size);
3770                                 /*-------- DEBUG LOG --------*/
3771                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3772                                 {
3773                                     std::string datadump;
3774                                     dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3775                                                 edit_min->data_size, datadump);
3776
3777                                     boost::format formatter(
3778                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3779                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3780                                         "data begin = %d, data_size = %d, data = %s");
3781                                     formatter % (send_buffer_end_size - send_buffer_remian_size)
3782                                     % edit_min->data_size % datadump;
3783                                     putLogDebug(100125, formatter.str(), __FILE__, __LINE__ );
3784                                 }
3785                                 /*------DEBUG LOG END------*/
3786                                 it->send_end_size += edit_min->replace_size;
3787                                 it->send_possible_size -= edit_min->replace_size;
3788                                 send_buffer_remian_size -= edit_min->data_size;
3789                                 it->edit_data_list.erase(edit_min);
3790                             }
3791                             //
3792                             else
3793                             {
3794                                 break;
3795                             }
3796                         }
3797                         //data that before X-Forwarded-For/uri is larger than send_buffer_remian_size
3798                         else
3799                         {
3800                             copy_size = send_buffer_remian_size;
3801                             /*-------- DEBUG LOG --------*/
3802                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3803                             {
3804                                 std::string datadump;
3805                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3806                                             copy_size, datadump);
3807
3808                                 boost::format formatter(
3809                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3810                                     "handle_sorryserver_connect() : before memcpy (data dump) : "
3811                                     "data begin = %d, data_size = %d, data = %s");
3812                                 formatter % (it->send_offset + it->send_end_size)
3813                                 % copy_size % datadump;
3814                                 putLogDebug(100126, formatter.str(), __FILE__, __LINE__ );
3815                             }
3816                             /*------DEBUG LOG END------*/
3817                             //copy data as large as possible
3818                             memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3819                                    recv_data.recive_buffer + it->send_offset + it->send_end_size, send_buffer_remian_size);
3820                             /*-------- DEBUG LOG --------*/
3821                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3822                             {
3823                                 std::string datadump;
3824                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3825                                             copy_size, datadump);
3826
3827                                 boost::format formatter(
3828                                     "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3829                                     "handle_sorryserver_connect() : after memcpy (data dump) : "
3830                                     "data begin = %d, data_size = %d, data = %s");
3831                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
3832                                 % copy_size % datadump;
3833                                 putLogDebug(100127, formatter.str(), __FILE__, __LINE__ );
3834                             }
3835                             /*------DEBUG LOG END------*/
3836                             it->send_end_size += copy_size;
3837                             it->send_possible_size -= copy_size;
3838                             send_buffer_remian_size -= copy_size;
3839                             break;
3840                         }
3841                     }
3842                 }
3843                 /*-------- DEBUG LOG --------*/
3844                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3845                 {
3846                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3847                                             "handle_sorryserver_connect() : Copy data loop end. thread id : %d.");
3848                     formatter % boost::this_thread::get_id();
3849                     putLogDebug(100128, formatter.str(), __FILE__, __LINE__ );
3850                 }
3851                 /*------DEBUG LOG END------*/
3852             }
3853             //edit_division flag is off
3854             else
3855             {
3856                 //copy data as large as possible
3857                 //send_possible_size is larger
3858                 if (send_buffer_remian_size >= it->send_possible_size)
3859                 {
3860                     copy_size = it->send_possible_size;
3861                     /*-------- DEBUG LOG --------*/
3862                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
3863                     {
3864                         std::string datadump;
3865                         dump_memory(recv_data.recive_buffer+ it->send_offset, copy_size, datadump);
3866
3867                         boost::format formatter(
3868                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3869                             "handle_sorryserver_connect() : before memcpy (data dump) : "
3870                             "data begin = %d, data_size = %d, data = %s");
3871                         formatter % it->send_offset
3872                         % copy_size % datadump;
3873                         putLogDebug(100129, formatter.str(), __FILE__, __LINE__ );
3874                     }
3875                     /*------DEBUG LOG END------*/
3876                     //copy data by send_possible size
3877                     memcpy(sendbuffer.data(), recv_data.recive_buffer
3878                            + it->send_offset, copy_size);
3879                     /*-------- DEBUG LOG --------*/
3880                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
3881                     {
3882                         std::string datadump;
3883                         dump_memory(sendbuffer.data(), copy_size, datadump);
3884
3885                         boost::format formatter(
3886                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3887                             "handle_sorryserver_connect() : after memcpy (data dump) : "
3888                             "data begin = 0, data_size = %d, data = %s");
3889                         formatter % copy_size % datadump;
3890                         putLogDebug(100130, formatter.str(), __FILE__, __LINE__ );
3891                     }
3892                     /*------DEBUG LOG END------*/
3893                     it->send_end_size = it->send_possible_size;
3894                     it->send_possible_size = 0;
3895                     send_buffer_remian_size -= copy_size;
3896                 }
3897                 //buffer rest size is larger
3898                 else
3899                 {
3900                     /*-------- DEBUG LOG --------*/
3901                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
3902                     {
3903                         std::string datadump;
3904                         dump_memory(recv_data.recive_buffer+ it->send_offset, send_buffer_remian_size, datadump);
3905
3906                         boost::format formatter(
3907                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3908                             "handle_sorryserver_connect() : before memcpy (data dump) : "
3909                             "data begin = %d, data_size = %d, data = %s");
3910                         formatter % it->send_offset
3911                         % send_buffer_remian_size % datadump;
3912                         putLogDebug(100131, formatter.str(), __FILE__, __LINE__ );
3913                     }
3914                     /*------DEBUG LOG END------*/
3915                     //copy data by buffer rest size
3916                     memcpy(sendbuffer.data(), recv_data.recive_buffer
3917                            + it->send_offset, send_buffer_remian_size);
3918                     /*-------- DEBUG LOG --------*/
3919                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
3920                     {
3921                         std::string datadump;
3922                         dump_memory(sendbuffer.data(), send_buffer_remian_size, datadump);
3923
3924                         boost::format formatter(
3925                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3926                             "handle_sorryserver_connect() : after memcpy (data dump) : "
3927                             "data begin = 0, data_size = %d, data = %s");
3928                         formatter % send_buffer_remian_size % datadump;
3929                         putLogDebug(100132, formatter.str(), __FILE__, __LINE__ );
3930                     }
3931                     /*------DEBUG LOG END------*/
3932                     it->send_end_size = send_buffer_remian_size;
3933                     it->send_possible_size -= send_buffer_remian_size;
3934                     send_buffer_remian_size = 0;
3935                 }
3936             }
3937
3938             //set copyed data length
3939             datalen = send_buffer_end_size - send_buffer_remian_size;
3940
3941             status = SORRYSERVER_SEND;
3942
3943         }
3944         catch (int e)
3945         {
3946             /*-------- DEBUG LOG --------*/
3947             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3948             {
3949                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3950                                         "handle_sorryserver_connect() : catch exception e = %d. thread id : %d.");
3951                 formatter % e % boost::this_thread::get_id();
3952                 putLogDebug(100133, formatter.str(), __FILE__, __LINE__ );
3953             }
3954             status = FINALIZE;
3955             /*------DEBUG LOG END------*/
3956         }
3957         catch (const std::exception& ex)
3958         {
3959             std::cerr << "protocol_module_sessionless::handle_sorryserver_connect() : exception : error = " << ex.what() << "." << std::endl;
3960             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3961                                     "handle_sorryserver_connect() : exception : error = %s. thread id : %d.");
3962             formatter % ex.what() % boost::this_thread::get_id();
3963             putLogError(100068, formatter.str(), __FILE__, __LINE__ );
3964             status = FINALIZE;
3965         }
3966         catch (...)
3967         {
3968             std::cerr << "protocol_module_sessionless::handle_sorryserver_connect() : Unknown exception." << std::endl;
3969             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3970                                     "handle_sorryserver_connect() : Unknown exception. thread id : %d.");
3971             formatter % boost::this_thread::get_id();
3972             putLogError(100069, formatter.str(), __FILE__, __LINE__ );
3973             status = FINALIZE;
3974         }
3975
3976         /*-------- DEBUG LOG --------*/
3977         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3978         {
3979             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3980                                     "handle_sorryserver_connect(const boost::thread::id thread_id, "
3981                                     "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
3982                                     "size_t& datalen) : return_value = %d. thread id : %d.");
3983             formatter % status % boost::this_thread::get_id();
3984             putLogDebug(100134, formatter.str(), __FILE__, __LINE__ );
3985         }
3986         /*------DEBUG LOG END------*/
3987
3988         return status;
3989     }
3990
3991     //! called from after sorryserver connection fail
3992     //! @param[in]    upstream thread id
3993     //! @param[in]    sorryserver endpoint reference.
3994     //! @return        session use EVENT mode.
3995     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_connection_fail(
3996         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint)
3997     {
3998         /*-------- DEBUG LOG --------*/
3999         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4000         {
4001             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4002                                     "handle_sorryserver_connection_fail(const boost::thread::id thread_id, "
4003                                     "const boost::asio::ip::tcp::endpoint & sorry_endpoint) : "
4004                                     "thread_id = %d, sorry_endpoint = [%s]:%d.");
4005             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
4006             putLogDebug(100135, formatter.str(), __FILE__, __LINE__ );
4007         }
4008         /*------DEBUG LOG END------*/
4009
4010         EVENT_TAG status = FINALIZE;
4011         thread_data_ptr session_data;
4012         session_thread_data_map_it session_thread_it;
4013
4014         try
4015         {
4016             boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
4017
4018             session_thread_it = session_thread_data_map.find(thread_id);
4019             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4020             {
4021                 boost::format formatter("Invalid thread id. thread id : %d.");
4022                 formatter % boost::this_thread::get_id();
4023                 putLogError(100070, formatter.str(), __FILE__, __LINE__ );
4024                 throw -1;
4025             }
4026
4027             session_data = session_thread_it->second;
4028
4029             //set end flag on
4030             session_data->end_flag = END_FLAG_ON;
4031             /*-------- DEBUG LOG --------*/
4032             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4033             {
4034                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4035                                         "handle_sorryserver_connection_fail() : END_FLAG_ON. thread id : %d.");
4036                 formatter % boost::this_thread::get_id();
4037                 putLogDebug(100136, formatter.str(), __FILE__, __LINE__ );
4038             }
4039             /*------DEBUG LOG END------*/
4040
4041             status = CLIENT_DISCONNECT;
4042         }
4043         catch (int e)
4044         {
4045             /*-------- DEBUG LOG --------*/
4046             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4047             {
4048                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4049                                         "handle_sorryserver_connection_fail() : catch exception e = %d. thread id : %d.");
4050                 formatter % e % boost::this_thread::get_id();
4051                 putLogDebug(100137, formatter.str(), __FILE__, __LINE__ );
4052             }
4053             status = FINALIZE;
4054             /*------DEBUG LOG END------*/
4055         }
4056         catch (const std::exception& ex)
4057         {
4058             std::cerr << "protocol_module_sessionless::handle_sorryserver_connection_fail() : exception : error=" << ex.what() << "." << std::endl;
4059             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4060                                     "handle_sorryserver_connection_fail() : exception : error = %s. thread id : %d.");
4061             formatter % ex.what() % boost::this_thread::get_id();
4062             putLogError(100071, formatter.str(), __FILE__, __LINE__ );
4063             status = FINALIZE;
4064         }
4065         catch (...)
4066         {
4067             std::cerr << "protocol_module_sessionless::handle_sorryserver_connection_fail() : Unknown exception." << std::endl;
4068             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4069                                     "handle_sorryserver_connection_fail() : Unknown exception. thread id : %d.");
4070             formatter % boost::this_thread::get_id();
4071             putLogError(100072, formatter.str(), __FILE__, __LINE__ );
4072             status = FINALIZE;
4073         }
4074
4075         /*-------- DEBUG LOG --------*/
4076         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4077         {
4078             boost::format
4079             formatter(
4080                 "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4081                 "handle_sorryserver_connection_fail( const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint) : return_value = %d."
4082                 " thread id : %d.");
4083             formatter % status % boost::this_thread::get_id();
4084             putLogDebug(100138, formatter.str(), __FILE__, __LINE__ );
4085         }
4086         /*------DEBUG LOG END------*/
4087         return status;
4088     }
4089
4090     //! called from after sorryserver send
4091     //! @param[in]    upstream thread id
4092     //! @return        session use EVENT mode.
4093     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_send(
4094         const boost::thread::id thread_id)
4095     {
4096         /*-------- DEBUG LOG --------*/
4097         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4098         {
4099             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4100                                     "handle_sorryserver_send(const boost::thread::id thread_id) : "
4101                                     "thread_id = %d.");
4102             formatter % thread_id;
4103             putLogDebug(100139, formatter.str(), __FILE__, __LINE__ );
4104         }
4105         /*------DEBUG LOG END------*/
4106         EVENT_TAG status = FINALIZE;
4107         thread_data_ptr session_data;
4108         session_thread_data_map_it session_thread_it;
4109         recive_data_map_it recive_data_it;
4110
4111         try
4112         {
4113             {
4114                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
4115
4116                 //thread_id check
4117                 session_thread_it = session_thread_data_map.find(thread_id);
4118                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4119                 {
4120                     boost::format formatter("Invalid thread id. thread id : %d.");
4121                     formatter % boost::this_thread::get_id();
4122                     putLogError(100073, formatter.str(), __FILE__, __LINE__ );
4123                     throw -1;
4124                 }
4125
4126                 session_data = session_thread_it->second;
4127             }
4128
4129             //endpoint check
4130             recive_data_it = session_data->recive_data_map.find(session_data->client_endpoint_tcp);
4131             if (unlikely(recive_data_it
4132                          == session_data->recive_data_map.end()))
4133             {
4134                 boost::format formatter("Invalid endpoint. thread id : %d.");
4135                 formatter % boost::this_thread::get_id();
4136                 putLogError(100074, formatter.str(), __FILE__, __LINE__ );
4137                 throw -1;
4138             }
4139
4140             recive_data& recv_data = recive_data_it->second;
4141
4142             send_status_it it = recv_data.send_status_list.begin();
4143             send_status_it it_end = recv_data.send_status_list.end();
4144             //status list check
4145             it = std::adjacent_find(it, it_end, data_send_list_incorrect());
4146             if (unlikely(it != it_end))
4147             {
4148                 boost::format formatter("Sending possible data is invalid. thread id : %d.");
4149                 formatter % boost::this_thread::get_id();
4150                 putLogError(100075, formatter.str(), __FILE__, __LINE__ );
4151                 throw -1;
4152             }
4153
4154             //status list check
4155             it = recv_data.send_status_list.begin();
4156             it = find_if(it, it_end, data_send_ok());
4157             if (unlikely(it == it_end))
4158             {
4159                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
4160                 formatter % boost::this_thread::get_id();
4161                 putLogError(100076, formatter.str(), __FILE__, __LINE__ );
4162                 throw -1;
4163             }
4164
4165             //sending possible data is exist
4166             if (it->send_possible_size > 0)
4167             {
4168                 //status remain SEND_OK
4169                 it->status = SEND_OK;
4170                 //offset recalc
4171                 it->send_offset += it->send_end_size;
4172
4173                 //insert_posission recalc
4174                 for (std::list<edit_data>::iterator list_it = it->edit_data_list.begin(); list_it
4175                         != it->edit_data_list.end(); ++list_it)
4176                 {
4177                     list_it->insert_posission -= it->send_end_size;
4178                 }
4179
4180                 //send_end_size recalc
4181                 it->send_end_size = 0;
4182             }
4183             //sending possible data is not exist
4184             else
4185             {
4186                 //can recive from clent continue
4187                 if (it->send_rest_size > 0)
4188                 {
4189                     //change status from SEND_OK to SEND_CONTINUE
4190                     it->status = SEND_CONTINUE;
4191                 }
4192                 //can not recive from clent continue
4193                 else
4194                 {
4195                     //change status from SEND_OK to SEND_END
4196                     it->status = SEND_END;
4197                 }
4198             }
4199
4200             it = recv_data.send_status_list.begin();
4201             it = find_if(it, it_end, data_send_ok());
4202             //send_ok item is exist
4203             if (it != it_end)
4204             {
4205                 status = SORRYSERVER_CONNECT;
4206             }
4207             //send_ok item is exist
4208             else
4209             {
4210                 status = CLIENT_RECV;
4211             }
4212
4213         }
4214         catch (int e)
4215         {
4216             /*-------- DEBUG LOG --------*/
4217             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4218             {
4219                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4220                                         "handle_sorryserver_send() : catch exception e = %d. thread id : %d.");
4221                 formatter % e % boost::this_thread::get_id();
4222                 putLogDebug(100140, formatter.str(), __FILE__, __LINE__ );
4223             }
4224             status = FINALIZE;
4225             /*------DEBUG LOG END------*/
4226         }
4227         catch (const std::exception& ex)
4228         {
4229             std::cerr << "protocol_module_sessionless::handle_sorryserver_send() : exception : error = " << ex.what() << "." << std::endl;
4230             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4231                                     "handle_sorryserver_send() : exception : error = %s. thread id : %d.");
4232             formatter % ex.what() % boost::this_thread::get_id();
4233             putLogError(100077, formatter.str(), __FILE__, __LINE__ );
4234
4235             status = FINALIZE;
4236         }
4237         catch (...)
4238         {
4239             std::cerr << "protocol_module_sessionless::handle_sorryserver_send() : Unknown exception." << std::endl;
4240             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4241                                     "handle_sorryserver_send() : Unknown exception. thread id : %d.");
4242             formatter % boost::this_thread::get_id();
4243             putLogError(100078, formatter.str(), __FILE__, __LINE__ );
4244             status = FINALIZE;
4245         }
4246
4247         /*-------- DEBUG LOG --------*/
4248         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4249         {
4250             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4251                                     "handle_sorryserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
4252             formatter % status % boost::this_thread::get_id();
4253             putLogDebug(100141, formatter.str(), __FILE__, __LINE__ );
4254         }
4255         /*------DEBUG LOG END------*/
4256
4257         return status;
4258     }
4259
4260     //! called from after realserver recive.for UDP
4261     //! @param[in]    downstream thread id
4262     //! @param[in]    realserver UDP endpoint reference
4263     //! @param[in]    recive from realserver buffer reference
4264     //! @param[in]    recv data length
4265     //! @return        session use EVENT mode.
4266     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_recv(
4267         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint& rs_endpoint, const boost::array<char,
4268         MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
4269     {
4270         /*-------- DEBUG LOG --------*/
4271         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4272         {
4273             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4274                                     "handle_realserver_recv(const boost::thread::id thread_id, "
4275                                     "const boost::asio::ip::udp::endpoint& rs_endpoint, "
4276                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4277                                     "const size_t recvlen) : "
4278                                     "return_value = %d. thread id : %d.");
4279             formatter % STOP % boost::this_thread::get_id();
4280             putLogDebug(100142, formatter.str(), __FILE__, __LINE__ );
4281         }
4282         /*------DEBUG LOG END------*/
4283         return STOP;
4284     }
4285
4286     //! called from after realserver recvive for TCP/IP
4287     //! @param[in]    downstream thread id
4288     //! @param[in]    realserver TCP/IP endpoint reference
4289     //! @param[in]    realserver recive buffer reference.
4290     //! @param[in]    recv data length
4291     //! @return        session use EVENT mode.
4292     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_recv(
4293         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& rs_endpoint, const boost::array<char,
4294         MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
4295     {
4296         /*-------- DEBUG LOG --------*/
4297         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4298         {
4299             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
4300             std::string buffer;
4301             dump_memory(recvbuffer.data(), buffer_size, buffer);
4302             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4303                                     "handle_realserver_recv(const boost::thread::id thread_id, "
4304                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4305                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4306                                     "const size_t recvlen) : thread_id = %d, rs_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
4307             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port()
4308             % buffer % recvlen;
4309             putLogDebug(100143, formatter.str(), __FILE__, __LINE__ );
4310         }
4311         /*------DEBUG LOG END------*/
4312
4313         EVENT_TAG status = FINALIZE;
4314         size_t data_remain_start = 0;
4315         size_t data_remain_size = 0;
4316         size_t request_data_remain_size = 0;
4317         size_t header_offset = 0;
4318         size_t header_offset_len = 0;
4319         size_t content_length_header_offset = 0;
4320         size_t content_length_header_len = 0;
4321         size_t content_len_value = 0;
4322         size_t pos = 0;
4323         size_t buffer_size = 0;
4324         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
4325         const size_t cr_lf_len = strlen("\r\n");
4326         thread_data_ptr session_data;
4327         char* buffer1 = NULL;
4328         char* buffer2 = NULL;
4329         std::string str_value;
4330         const std::string http_header = "";
4331         const std::string content_header = "Content-Length";
4332         bool bret = false;
4333         CHECK_RESULT_TAG check_result;
4334         session_thread_data_map_it session_thread_it;
4335         recive_data_map_it recive_data_it;
4336
4337         //parameter check
4338         if (recvlen > recvbuffer.size())
4339         {
4340             std::cerr << "protocol_module_sessionless::handle_realserver_recv() : Data size bigger than buffer size." << std::endl;
4341             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
4342             formatter % boost::this_thread::get_id();
4343             putLogError(100079, formatter.str(), __FILE__, __LINE__ );
4344             /*-------- DEBUG LOG --------*/
4345             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4346             {
4347                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4348                                         "handle_realserver_recv(const boost::thread::id thread_id, "
4349                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4350                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4351                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
4352                 formatter % FINALIZE % boost::this_thread::get_id();
4353                 putLogDebug(100144, formatter.str(), __FILE__, __LINE__ );
4354             }
4355             /*------DEBUG LOG END------*/
4356             return FINALIZE;
4357         }
4358
4359         try
4360         {
4361             {
4362                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
4363
4364                 session_thread_it = session_thread_data_map.find(thread_id);
4365                 if (unlikely(session_thread_it == session_thread_data_map.end()
4366                              ||session_thread_it->second == NULL))
4367                 {
4368                     boost::format formatter("Invalid thread id. thread id : %d.");
4369                     formatter % boost::this_thread::get_id();
4370                     putLogError(100080, formatter.str(), __FILE__, __LINE__ );
4371                     throw -1;
4372                 }
4373
4374                 session_data = session_thread_it->second;
4375             }
4376
4377             recive_data_it = session_data->recive_data_map.find(rs_endpoint);
4378             if (recive_data_it == session_data->recive_data_map.end())
4379             {
4380                 recive_data recv_data;
4381                 session_data->recive_data_map[rs_endpoint] = recv_data;
4382             }
4383
4384             session_data->target_endpoint = rs_endpoint;
4385
4386             recive_data& recv_data = session_data->recive_data_map[rs_endpoint];
4387
4388             send_status_it it = recv_data.send_status_list.begin();
4389             send_status_it it_end = recv_data.send_status_list.end();
4390
4391             //status list check
4392             it = std::find_if(it, it_end, data_send_ok());
4393             if (unlikely(it != it_end))
4394             {
4395                 boost::format formatter("Sending data is not correct. thread id : %d.");
4396                 formatter % boost::this_thread::get_id();
4397                 putLogError(100081, formatter.str(), __FILE__, __LINE__ );
4398                 throw -1;
4399             }
4400
4401             //status list check
4402             it = recv_data.send_status_list.begin();
4403             it = std::adjacent_find(it, it_end, data_send_repeated());
4404             if (unlikely(it != it_end))
4405             {
4406                 boost::format formatter("Sending data is not correct. thread id : %d.");
4407                 formatter % boost::this_thread::get_id();
4408                 putLogError(100082, formatter.str(), __FILE__, __LINE__ );
4409                 throw -1;
4410             }
4411
4412             /*-------- DEBUG LOG --------*/
4413             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4414             {
4415                 std::string datadump;
4416                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4417                                         "send_rest_size = %d, send_possible_size = %d, "
4418                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
4419                 int i = 0;
4420                 for (it = recv_data.send_status_list.begin();
4421                         it != recv_data.send_status_list.end();
4422                         ++it, ++i)
4423                 {
4424                     formatter % i % it->status % it->send_end_size
4425                     % it->send_rest_size % it->send_possible_size
4426                     % it->send_offset % it->unsend_size % it->edit_division;
4427                     datadump += formatter.str();
4428                 }
4429
4430                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4431                                 "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4432
4433                 formatter % recv_data.send_status_list.size() % datadump;
4434                 putLogDebug(100145, formatter.str(), __FILE__, __LINE__ );
4435             }
4436             /*------DEBUG LOG END------*/
4437             it = recv_data.send_status_list.begin();
4438             //get original status info
4439             while (it != it_end)
4440             {
4441                 //item status is SEND_END
4442                 if (it->status == SEND_END)
4443                 {
4444                     //erase from list
4445                     recv_data.send_status_list.erase(it++);
4446                     continue;
4447                 }
4448                 //item status is SEND_CONTINUE
4449                 else if (it->status == SEND_CONTINUE)
4450                 {
4451                     it->send_offset += it->send_end_size;
4452                     data_remain_start = it->send_offset;
4453                     break;
4454                 }
4455                 //item status is SEND_NG
4456                 else
4457                 {
4458                     data_remain_start = it->send_offset;
4459                     data_remain_size = it->unsend_size;
4460                     break;
4461                 }
4462
4463                 ++it;
4464             }
4465             /*-------- DEBUG LOG --------*/
4466             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4467             {
4468                 std::string datadump;
4469                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4470                                         "send_rest_size = %d, send_possible_size = %d, "
4471                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
4472                 int i = 0;
4473                 for (it = recv_data.send_status_list.begin();
4474                         it != recv_data.send_status_list.end();
4475                         ++it, ++i)
4476                 {
4477                     formatter % i % it->status % it->send_end_size
4478                     % it->send_rest_size % it->send_possible_size
4479                     % it->send_offset % it->unsend_size % it->edit_division;
4480                     datadump += formatter.str();
4481                 }
4482
4483                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4484                                 "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4485
4486                 formatter % recv_data.send_status_list.size() % datadump;
4487                 putLogDebug(100146, formatter.str(), __FILE__, __LINE__ );
4488             }
4489             /*------DEBUG LOG END------*/
4490             //recive buffer process
4491             //buffer rest size < request size
4492             if (recv_data.recive_buffer_rest_size < recvlen)
4493             {
4494                 //buffer max size < remain size + request size
4495                 //buffer is need reallocate
4496                 if (recv_data.recive_buffer_max_size < data_remain_size + recvlen)
4497                 {
4498                     //the buffer's size that will be allocated is exceed the upper limit value
4499                     if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen)
4500                     {
4501                         std::cerr << "protocol_module_sessionless::handle_realserver_recv() : the buffer's size that will be allacted is exceed the upper limit value." << std::endl;
4502                         boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
4503                         formatter % boost::this_thread::get_id();
4504                         putLogError(100083, formatter.str(), __FILE__, __LINE__ );
4505
4506                         /*-------- DEBUG LOG --------*/
4507                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4508                         {
4509                             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4510                                                     "handle_realserver_recv(const boost::thread::id thread_id, "
4511                                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4512                                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4513                                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
4514                             formatter % FINALIZE % boost::this_thread::get_id();
4515                             putLogDebug(100147, formatter.str(), __FILE__, __LINE__ );
4516                         }
4517                         /*------DEBUG LOG END------*/
4518
4519                         return FINALIZE;
4520                     }
4521                     //recive_buffer1's memory allocate and initialization
4522                     buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
4523                     buffer1 = new char[buffer_size];
4524                     /*-------- DEBUG LOG --------*/
4525                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4526                     {
4527                         boost::format formatter("new : address = &(%d), size = %lu.");
4528                         formatter % static_cast<void*>(buffer1) % buffer_size;
4529                         putLogDebug(100148, formatter.str(), __FILE__, __LINE__ );
4530                     }
4531                     /*-------- DEBUG LOG --------*/
4532                     memset(buffer1, 0, buffer_size);
4533                     //recive_buffer2's memory allocate and initialization
4534                     buffer2 = new char[buffer_size];
4535                     /*-------- DEBUG LOG --------*/
4536                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4537                     {
4538                         boost::format formatter("new : address = &(%d), size = %lu.");
4539                         formatter % static_cast<void*>(buffer2) % buffer_size;
4540                         putLogDebug(100149, formatter.str(), __FILE__, __LINE__ );
4541                     }
4542                     /*-------- DEBUG LOG END--------*/
4543                     memset(buffer2, 0, buffer_size);
4544
4545                     /*-------- DEBUG LOG --------*/
4546                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4547                     {
4548                         std::string datadump;
4549                         dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
4550
4551                         boost::format formatter(
4552                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4553                             "handle_realserver_recv() : before memcpy (data dump) : "
4554                             "data begin = %d, data_size = %d, data = %s");
4555                         formatter % data_remain_start % data_remain_size % datadump;
4556                         putLogDebug(100150, formatter.str(), __FILE__, __LINE__ );
4557                     }
4558                     /*------DEBUG LOG END------*/
4559                     //copy data from old buffer to new buffer
4560                     memcpy(buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
4561                     /*-------- DEBUG LOG --------*/
4562                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4563                     {
4564                         std::string datadump;
4565                         dump_memory(buffer1, data_remain_size, datadump);
4566
4567                         boost::format formatter(
4568                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4569                             "handle_realserver_recv() : after memcpy (data dump) : "
4570                             "data begin = 0, data_size = %d, data = %s");
4571                         formatter % data_remain_size % datadump;
4572                         putLogDebug(100151, formatter.str(), __FILE__, __LINE__ );
4573                     }
4574                     /*------DEBUG LOG END------*/
4575
4576                     /*-------- DEBUG LOG --------*/
4577                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4578                     {
4579                         std::string datadump;
4580                         dump_memory(recvbuffer.data(), recvlen, datadump);
4581                         boost::format formatter(
4582                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4583                             "handle_realserver_recv() : before memcpy (data dump) : "
4584                             "data begin = 0, data_size = %d, data = %s");
4585                         formatter % recvlen % datadump;
4586                         putLogDebug(100152, formatter.str(), __FILE__, __LINE__ );
4587                     }
4588                     /*------DEBUG LOG END------*/
4589                     memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
4590                     /*-------- DEBUG LOG --------*/
4591                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4592                     {
4593                         std::string datadump;
4594                         dump_memory(buffer1 + data_remain_size, recvlen, datadump);
4595                         boost::format formatter(
4596                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4597                             "handle_realserver_recv() : after memcpy (data dump) : "
4598                             "data begin = %d, data_size = %d, data = %s");
4599                         formatter % data_remain_size % recvlen % datadump;
4600                         putLogDebug(100153, formatter.str(), __FILE__, __LINE__ );
4601                     }
4602                     /*------DEBUG LOG END------*/
4603                     //free old buffer1 and old buffer2
4604                     if (recv_data.recive_buffer1 != NULL)
4605                     {
4606                         /*-------- DEBUG LOG --------*/
4607                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4608                         {
4609                             boost::format formatter("delete : address = &(%d).");
4610                             formatter % static_cast<void*>(recv_data.recive_buffer1);
4611                             putLogDebug(100154, formatter.str(), __FILE__,
4612                                         __LINE__ );
4613                         }
4614                         /*------DEBUG LOG END------*/
4615                         delete[] recv_data.recive_buffer1;
4616                         recv_data.recive_buffer1 = NULL;
4617                     }
4618
4619                     if (recv_data.recive_buffer2 != NULL)
4620                     {
4621                         /*-------- DEBUG LOG --------*/
4622                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4623                         {
4624                             boost::format formatter("delete : address = &(%d).");
4625                             formatter % static_cast<void*>(recv_data.recive_buffer2);
4626                             putLogDebug(100155, formatter.str(), __FILE__,
4627                                         __LINE__ );
4628                         }
4629                         /*------DEBUG LOG END------*/
4630                         delete[] recv_data.recive_buffer2;
4631                         recv_data.recive_buffer2 = NULL;
4632                     }
4633
4634                     //set new buffer pointer
4635                     recv_data.recive_buffer1 = buffer1;
4636                     recv_data.recive_buffer2 = buffer2;
4637                     recv_data.recive_buffer = recv_data.recive_buffer1;
4638                     //set new buffer's max size
4639                     recv_data.recive_buffer_max_size = buffer_size;
4640                 }
4641                 //buffer's max size >= remain data size + requst size
4642                 //buffer isn't need reallocate, but switch
4643                 else
4644                 {
4645                     //pointer valid check
4646                     if (unlikely(recv_data.recive_buffer1 == NULL || recv_data.recive_buffer2 == NULL))
4647                     {
4648                         boost::format formatter("Invalid pointer. thread id : %d.");
4649                         formatter % boost::this_thread::get_id();
4650                         putLogError(100084, formatter.str(), __FILE__, __LINE__ );
4651                         throw -1;
4652                     }
4653                     //using buffer is buffer1
4654                     if (recv_data.recive_buffer == recv_data.recive_buffer1)
4655                     {
4656                         //buffer2 initialization
4657                         memset(recv_data.recive_buffer2, 0, recv_data.recive_buffer_max_size);
4658                         /*-------- DEBUG LOG --------*/
4659                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4660                         {
4661                             std::string datadump;
4662                             dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
4663                             boost::format formatter(
4664                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4665                                 "handle_realserver_recv() : before memcpy (data dump) : "
4666                                 "data begin = %d, data_size = %d, data = %s");
4667                             formatter % data_remain_start % data_remain_size % datadump;
4668                             putLogDebug(100156, formatter.str(), __FILE__, __LINE__ );
4669                         }
4670                         /*------DEBUG LOG END------*/
4671                         //copy data from buffer1 to buffer2
4672                         memcpy(recv_data.recive_buffer2, recv_data.recive_buffer + data_remain_start, data_remain_size);
4673                         /*-------- DEBUG LOG --------*/
4674                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4675                         {
4676                             std::string datadump;
4677                             dump_memory(recv_data.recive_buffer2, data_remain_size, datadump);
4678                             boost::format formatter(
4679                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4680                                 "handle_realserver_recv() : after memcpy (data dump) : "
4681                                 "data begin = 0, data_size = %d, data = %s");
4682                             formatter % data_remain_size % datadump;
4683                             putLogDebug(100157, formatter.str(), __FILE__, __LINE__ );
4684                         }
4685                         /*------DEBUG LOG END------*/
4686                         /*-------- DEBUG LOG --------*/
4687                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4688                         {
4689                             std::string datadump;
4690                             dump_memory(recvbuffer.data(), recvlen, datadump);
4691                             boost::format formatter(
4692                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4693                                 "handle_realserver_recv() : before memcpy (data dump) : "
4694                                 "data begin = 0, data_size = %d, data = %s");
4695                             formatter % data_remain_size % datadump;
4696                             putLogDebug(100158, formatter.str(), __FILE__, __LINE__ );
4697                         }
4698                         /*------DEBUG LOG END------*/
4699                         memcpy(recv_data.recive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
4700                         /*-------- DEBUG LOG --------*/
4701                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4702                         {
4703                             std::string datadump;
4704                             dump_memory(recv_data.recive_buffer2 + data_remain_size, recvlen, datadump);
4705
4706                             boost::format formatter(
4707                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4708                                 "handle_realserver_recv() : after memcpy (data dump) : "
4709                                 "data begin = %d, data_size = %d, data = %s");
4710                             formatter % data_remain_size % recvlen % datadump;
4711                             putLogDebug(100159, formatter.str(), __FILE__, __LINE__ );
4712                         }
4713                         /*------DEBUG LOG END------*/
4714                         //set buffer2 as using buffer
4715                         recv_data.recive_buffer = recv_data.recive_buffer2;
4716                     }
4717                     //using buffer is buffer2
4718                     else
4719                     {
4720                         //buffer1 initialization
4721                         memset(recv_data.recive_buffer1, 0, recv_data.recive_buffer_max_size);
4722                         /*-------- DEBUG LOG --------*/
4723                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4724                         {
4725                             std::string datadump;
4726                             dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
4727
4728                             boost::format formatter(
4729                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4730                                 "handle_realserver_recv() : before memcpy (data dump) : "
4731                                 "data begin = %d, data_size = %d, data = %s");
4732                             formatter % data_remain_start % data_remain_size % datadump;
4733                             putLogDebug(100160, formatter.str(), __FILE__, __LINE__ );
4734                         }
4735                         /*------DEBUG LOG END------*/
4736                         //copy data from buffer2 to buffer1
4737                         memcpy(recv_data.recive_buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
4738                         /*-------- DEBUG LOG --------*/
4739                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4740                         {
4741                             std::string datadump;
4742                             dump_memory(recv_data.recive_buffer1, data_remain_size, datadump);
4743
4744                             boost::format formatter(
4745                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4746                                 "handle_realserver_recv() : after memcpy (data dump) : "
4747                                 "data begin = 0, data_size = %d, data = %s");
4748                             formatter % data_remain_size % datadump;
4749                             putLogDebug(100161, formatter.str(), __FILE__, __LINE__ );
4750                         }
4751                         /*------DEBUG LOG END------*/
4752                         /*-------- DEBUG LOG --------*/
4753                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4754                         {
4755                             std::string datadump;
4756                             dump_memory(recvbuffer.data(), recvlen, datadump);
4757
4758                             boost::format formatter(
4759                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4760                                 "handle_realserver_recv() : before memcpy (data dump) : "
4761                                 "data begin = 0, data_size = %d, data = %s");
4762                             formatter % recvlen % datadump;
4763                             putLogDebug(100162, formatter.str(), __FILE__, __LINE__ );
4764                         }
4765                         /*------DEBUG LOG END------*/
4766                         memcpy(recv_data.recive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
4767                         /*-------- DEBUG LOG --------*/
4768                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4769                         {
4770                             std::string datadump;
4771                             dump_memory(recv_data.recive_buffer1 + data_remain_size, recvlen, datadump);
4772
4773                             boost::format formatter(
4774                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4775                                 "handle_realserver_recv() : after memcpy (data dump) : "
4776                                 "data begin = %d, data_size = %d, data = %s");
4777                             formatter % data_remain_size % recvlen % datadump;
4778                             putLogDebug(100163, formatter.str(), __FILE__, __LINE__ );
4779                         }
4780                         /*------DEBUG LOG END------*/
4781                         //set buffer1 as using buffer
4782                         recv_data.recive_buffer = recv_data.recive_buffer1;
4783                     }
4784                 }
4785
4786                 //set buffer's rest size
4787                 recv_data.recive_buffer_rest_size = recv_data.recive_buffer_max_size - data_remain_size - recvlen;
4788
4789                 //remain_size recalc
4790                 data_remain_size += recvlen;
4791
4792                 send_status_it it_begin = recv_data.send_status_list.begin();
4793                 send_status_it it_end = recv_data.send_status_list.end();
4794                 //offset recalc
4795                 for (; it_begin != it_end; ++it_begin)
4796                 {
4797                     it_begin->send_offset -= data_remain_start;
4798                 }
4799             }
4800             //buffer's rest size >= request size
4801             //copy directly
4802             else
4803             {
4804                 //pointer valid check
4805                 if (unlikely(recv_data.recive_buffer == NULL))
4806                 {
4807                     boost::format formatter("Invalid pointer. thread id : %d.");
4808                     formatter % boost::this_thread::get_id();
4809                     putLogError(100085, formatter.str(), __FILE__, __LINE__ );
4810                     throw -1;
4811                 }
4812                 /*-------- DEBUG LOG --------*/
4813                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
4814                 {
4815                     std::string datadump;
4816                     dump_memory(recvbuffer.data(), recvlen, datadump);
4817                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4818                                             "handle_realserver_recv() : before memcpy (data dump) : "
4819                                             "data begin = 0, data_size = %d, data = %s");
4820                     formatter % recvlen % datadump;
4821                     putLogDebug(100164, formatter.str(), __FILE__, __LINE__ );
4822                 }
4823                 /*------DEBUG LOG END------*/
4824
4825                 //copy data from parameter to using buffer
4826                 memcpy(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size,
4827                        recvbuffer.data(), recvlen);
4828                 /*-------- DEBUG LOG --------*/
4829                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
4830                 {
4831                     std::string datadump;
4832                     dump_memory(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size,
4833                                 recvlen, datadump);
4834                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4835                                             "handle_realserver_recv() : before memcpy (data dump) : "
4836                                             "data begin = %d, data_size = %d, data = %s");
4837                     formatter % (recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size )
4838                     % recvlen % datadump;
4839                     putLogDebug(100165, formatter.str(), __FILE__, __LINE__ );
4840                 }
4841                 /*------DEBUG LOG END------*/
4842                 //buffer's rest size recalc
4843                 recv_data.recive_buffer_rest_size -= recvlen;
4844                 //remain data size recalc
4845                 data_remain_size += recvlen;
4846             }
4847
4848             it = recv_data.send_status_list.begin();
4849             it_end = recv_data.send_status_list.end();
4850             //request rest size initialization
4851             request_data_remain_size = recvlen;
4852             //original status process
4853             for (; it != it_end; ++it)
4854             {
4855                 //status is SEND_CONTINUE
4856                 if (it->status == SEND_CONTINUE)
4857                 {
4858                     //send rest size > request size
4859                     if (it->send_rest_size > request_data_remain_size)
4860                     {
4861                         //send possible size recalc
4862                         it->send_possible_size = request_data_remain_size;
4863                         //send rest size recalc
4864                         it->send_rest_size -= request_data_remain_size;
4865                         //send end size recalc
4866                         it->send_end_size = 0;
4867                         //request size recalc
4868                         request_data_remain_size = 0;
4869                     }
4870                     //send rest size <= request size
4871                     else
4872                     {
4873                         //send possible size recalc
4874                         it->send_possible_size = it->send_rest_size;
4875                         //send rest size recalc
4876                         request_data_remain_size -= it->send_rest_size;
4877                         //send end size recalc
4878                         it->send_end_size = 0;
4879                         //request size recalc
4880                         it->send_rest_size = 0;
4881                     }
4882                     //change status from SEND_CONTINUE to SEND_OK
4883                     it->status = SEND_OK;
4884                 }
4885                 //status is SEND_NG
4886                 else if (it->status == SEND_NG)
4887                 {
4888                     //check http method
4889                     check_result = check_status_code(recv_data.recive_buffer + it->send_offset, data_remain_size);
4890                     /*-------- DEBUG LOG --------*/
4891                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4892                     {
4893                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4894                                                 "handle_realserver_recv() : call check_http_method : "
4895                                                 "return_value = %d. thread id : %d.");
4896                         formatter % check_result % boost::this_thread::get_id();
4897                         putLogDebug(100166, formatter.str(), __FILE__, __LINE__ );
4898                     }
4899                     /*------DEBUG LOG END------*/
4900                     //check http method result is OK
4901                     if (check_result == CHECK_OK)
4902                     {
4903                         //check http version
4904                         check_result = check_http_version(recv_data.recive_buffer + it->send_offset, data_remain_size);
4905                         /*-------- DEBUG LOG --------*/
4906                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4907                         {
4908                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4909                                                     "handle_realserver_recv() : call check_http_version : "
4910                                                     "return_value = %d. thread id : %d.");
4911                             formatter % check_result % boost::this_thread::get_id();
4912                             putLogDebug(100167, formatter.str(), __FILE__, __LINE__ );
4913                         }
4914                         /*------DEBUG LOG END------*/
4915                     }
4916                     //check method and version result is OK
4917                     if (check_result == CHECK_OK)
4918                     {
4919                         //search http header
4920                         bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size, http_header,
4921                                                 header_offset, header_offset_len);
4922                         /*-------- DEBUG LOG --------*/
4923                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4924                         {
4925                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4926                                                     "handle_realserver_recv() : call find_http_header : "
4927                                                     "return_value = %d. thread id : %d.");
4928                             formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4929                             putLogDebug(100168, formatter.str(), __FILE__, __LINE__ );
4930                         }
4931                         /*------DEBUG LOG END------*/
4932                         //search http header result is OK
4933                         if (bret)
4934                         {
4935                             //search Content_Length header
4936                             bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size,
4937                                                     content_header, content_length_header_offset, content_length_header_len);
4938                             /*-------- DEBUG LOG --------*/
4939                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4940                             {
4941                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4942                                                         "handle_realserver_recv() : call find_http_header : "
4943                                                         "return_value = %d. thread id : %d.");
4944                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4945                                 putLogDebug(100169, formatter.str(), __FILE__, __LINE__ );
4946                             }
4947                             /*------DEBUG LOG END------*/
4948                             //search Content_Length result is OK
4949                             if (bret)
4950                             {
4951                                 //Get Content_Length header's numeric value
4952                                 for (pos = 0; recv_data.recive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
4953                                         < content_length_header_len; ++pos)
4954                                     ;
4955                                 if (pos == content_length_header_len)
4956                                 {
4957                                     throw std::string("Content_Length field's value is invalid.");
4958                                 }
4959
4960                                 ++pos;
4961
4962                                 str_value.assign(recv_data.recive_buffer + it->send_offset + content_length_header_offset + pos,
4963                                                  content_length_header_len - pos);
4964
4965                                 size_t pos_end = str_value.find_last_of('\r');
4966                                 if (pos_end != std::string::npos)
4967                                 {
4968                                     str_value = str_value.erase(pos_end);
4969                                 }
4970
4971                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
4972                                     ;
4973
4974                                 str_value = str_value.substr(pos);
4975
4976                                 try
4977                                 {
4978                                     content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
4979                                 }
4980                                 catch (const boost::bad_lexical_cast& ex)
4981                                 {
4982                                     throw std::string("Content_Length field's value is invalid.");
4983                                 }
4984                                 //send_rest_size recalc
4985                                 //set whole http header's length + Content_Length's value
4986                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
4987                             }
4988                             //search Content_Length result is NG
4989                             else
4990                             {
4991                                 //send_rest_size recalc
4992                                 //set whole http header's length
4993                                 if (header_offset_len == 0)
4994                                 {
4995                                     it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
4996                                 } else
4997                                 {
4998                                     it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
4999                                 }
5000                             }
5001                         }
5002                         //search http header result is NG
5003                         else
5004                         {
5005                             it->unsend_size += request_data_remain_size;
5006                             request_data_remain_size = 0;
5007                             break;
5008                         }
5009                     }
5010                     //check method and version result is NG
5011                     else if (check_result == CHECK_NG)
5012                     {
5013                         //send_rest_size recalc
5014                         it->send_rest_size = it->unsend_size + request_data_remain_size;
5015                     }
5016                     //check method and version result is CHECK_INPOSSIBLE
5017                     else
5018                     {
5019                         it->unsend_size += request_data_remain_size;
5020                         request_data_remain_size = 0;
5021                         break;
5022                     }
5023
5024                     //recalc fields value according to send_rest_size and request rest size
5025                     if (it->send_rest_size > it->unsend_size + request_data_remain_size)
5026                     {
5027                         it->send_possible_size = it->unsend_size+ request_data_remain_size;
5028                         it->send_rest_size -= (it->unsend_size + request_data_remain_size);
5029                         it->send_end_size = 0;
5030                         it->unsend_size = 0;
5031                         request_data_remain_size = 0;
5032                     }
5033                     else
5034                     {
5035                         it->send_possible_size = it->send_rest_size;
5036                         request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
5037                         it->send_end_size = 0;
5038                         it->unsend_size = 0;
5039                         it->send_rest_size = 0;
5040                     }
5041
5042                     //change status from SEND_NG to SEND_OK
5043                     it->status = SEND_OK;
5044                 }
5045                 //no request rest data to process
5046                 if (request_data_remain_size <= 0)
5047                 {
5048                     break;
5049                 }
5050             }
5051
5052             /*-------- DEBUG LOG --------*/
5053             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5054             {
5055                 std::string datadump;
5056                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5057                                         "send_rest_size = %d, send_possible_size = %d, "
5058                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
5059                 int i = 0;
5060                 for (it = recv_data.send_status_list.begin();
5061                         it != recv_data.send_status_list.end();
5062                         ++it, ++i)
5063                 {
5064                     formatter % i % it->status % it->send_end_size
5065                     % it->send_rest_size % it->send_possible_size
5066                     % it->send_offset % it->unsend_size % it->edit_division;
5067                     datadump += formatter.str();
5068                 }
5069
5070                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5071                                 "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
5072
5073                 formatter % recv_data.send_status_list.size() % datadump;
5074                 putLogDebug(100170, formatter.str(), __FILE__, __LINE__ );
5075             }
5076             /*------DEBUG LOG END------*/
5077             //there are still rest data need to process
5078             //new status created and add to status list
5079             while (request_data_remain_size > 0)
5080             {
5081                 //new status created
5082                 send_status new_send_state;
5083                 new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
5084                 new_send_state.send_end_size = 0;
5085                 new_send_state.send_offset = 0;
5086                 new_send_state.send_possible_size = 0;
5087                 new_send_state.unsend_size = 0;
5088                 new_send_state.send_rest_size = 0;
5089                 //status initialize to SEND_NG
5090                 new_send_state.status = SEND_NG;
5091                 //add new status to status_list
5092                 recv_data.send_status_list.push_back(new_send_state);
5093                 std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
5094                 //calc offset
5095                 new_send_it->send_offset = recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size
5096                                            - request_data_remain_size;
5097
5098                 //check http method
5099                 check_result = check_status_code(recv_data.recive_buffer + new_send_it->send_offset,
5100                                                  request_data_remain_size);
5101                 /*-------- DEBUG LOG --------*/
5102                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5103                 {
5104                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5105                                             "handle_realserver_recv() : call check_http_method : "
5106                                             "return_value = %d. thread id : %d.");
5107                     formatter % check_result % boost::this_thread::get_id();
5108                     putLogDebug(100171, formatter.str(), __FILE__, __LINE__ );
5109                 }
5110                 /*------DEBUG LOG END------*/
5111                 //check http method result is OK
5112                 if (check_result == CHECK_OK)
5113                 {
5114                     //check http version
5115                     check_result = check_http_version(recv_data.recive_buffer + new_send_it->send_offset,
5116                                                       request_data_remain_size);
5117                     /*-------- DEBUG LOG --------*/
5118                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5119                     {
5120                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5121                                                 "handle_realserver_recv() : call check_http_version : "
5122                                                 "return_value = %d. thread id : %d.");
5123                         formatter % check_result % boost::this_thread::get_id();
5124                         putLogDebug(100172, formatter.str(), __FILE__, __LINE__ );
5125                     }
5126                     /*------DEBUG LOG END------*/
5127                 }
5128                 //check http method and version result is OK
5129                 if (check_result == CHECK_OK)
5130                 {
5131                     //search whole http header, get whole http header's offset and length
5132                     bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset, request_data_remain_size,
5133                                             http_header, header_offset, header_offset_len);
5134                     /*-------- DEBUG LOG --------*/
5135                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5136                     {
5137                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5138                                                 "handle_realserver_recv() : call find_http_header : "
5139                                                 "return_value = %d. thread id : %d.");
5140                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5141                         putLogDebug(100173, formatter.str(), __FILE__, __LINE__ );
5142                     }
5143                     /*------DEBUG LOG END------*/
5144                     //searched whole http header
5145                     if (bret)
5146                     {
5147                         //search ContentLength http header, get ContentLength header's offset and length
5148                         bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset,
5149                                                 request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
5150                         /*-------- DEBUG LOG --------*/
5151                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5152                         {
5153                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5154                                                     "handle_realserver_recv() : call find_http_header : "
5155                                                     "return_value = %d. thread id : %d.");
5156                             formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5157                             putLogDebug(100174, formatter.str(), __FILE__, __LINE__ );
5158                         }
5159                         /*------DEBUG LOG END------*/
5160
5161                         //searched ContentLength http header
5162                         if (bret)
5163                         {
5164                             //Get Content_Length header's numeric value
5165                             for (pos = 0; recv_data.recive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
5166                                     && pos < content_length_header_len; ++pos)
5167                                 ;
5168                             if (pos == content_length_header_len)
5169                             {
5170                                 throw std::string("Content_Length field's value is invalid.");
5171                             }
5172
5173                             ++pos;
5174
5175                             str_value.assign(recv_data.recive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
5176                                              content_length_header_len - pos);
5177
5178                             size_t pos_end = str_value.find_last_of('\r');
5179                             if (pos_end != std::string::npos)
5180                             {
5181                                 str_value = str_value.erase(pos_end);
5182                             }
5183
5184                             for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
5185                                 ;
5186
5187                             str_value = str_value.substr(pos);
5188                             try
5189                             {
5190                                 content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
5191                             }
5192                             catch (const boost::bad_lexical_cast& ex)
5193                             {
5194                                 throw std::string("Content_Length field's value is invalid.");
5195                             }
5196                             //send_rest_size recalc
5197                             //set whole http header's  + whole http header's length + Content_Length's value
5198                             new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
5199                         }
5200                         //not searched ContentLength http header
5201                         else
5202                         {
5203                             //send_rest_size recalc
5204                             //set whole http header's  + whole http header's length
5205                             if (header_offset_len == 0)
5206                             {
5207                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
5208                             }
5209                             else
5210                             {
5211                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
5212                             }
5213
5214                         }
5215                     }
5216                     //not searched whole http header
5217                     else
5218                     {
5219                         new_send_it->unsend_size = request_data_remain_size;
5220                         request_data_remain_size = 0;
5221                         break;
5222                     }
5223                 }
5224                 //check http method or version result is NG
5225                 else if (check_result == CHECK_NG)
5226                 {
5227                     new_send_it->send_rest_size = request_data_remain_size;
5228                 }
5229                 //check http method or version result is impossible
5230                 else
5231                 {
5232                     new_send_it->unsend_size = request_data_remain_size;
5233                     request_data_remain_size = 0;
5234                     break;
5235                 }
5236
5237                 //recalc fields value according to send_rest_size and request rest size
5238                 if (new_send_it->send_rest_size > request_data_remain_size)
5239                 {
5240                     new_send_it->send_possible_size = request_data_remain_size;
5241                     new_send_it->send_rest_size -= request_data_remain_size;
5242                     new_send_it->send_end_size = 0;
5243                     new_send_it->send_end_size = 0;
5244                     request_data_remain_size = 0;
5245                 }
5246                 else
5247                 {
5248                     new_send_it->send_possible_size = new_send_it->send_rest_size;
5249                     request_data_remain_size -= new_send_it->send_rest_size;
5250                     new_send_it->send_end_size = 0;
5251                     new_send_it->send_rest_size = 0;
5252                 }
5253
5254                 //change status from SEND_NG to SEND_OK
5255                 new_send_it->status = SEND_OK;
5256             }
5257             /*-------- DEBUG LOG --------*/
5258             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5259             {
5260                 std::string datadump;
5261                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5262                                         "send_rest_size = %d, send_possible_size = %d, "
5263                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
5264                 int i = 0;
5265                 for (it = recv_data.send_status_list.begin();
5266                         it != recv_data.send_status_list.end();
5267                         ++it, ++i)
5268                 {
5269                     formatter % i % it->status % it->send_end_size
5270                     % it->send_rest_size % it->send_possible_size
5271                     % it->send_offset % it->unsend_size % it->edit_division;
5272                     datadump += formatter.str();
5273                 }
5274
5275                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5276                                 "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
5277
5278                 formatter % recv_data.send_status_list.size() % datadump;
5279                 putLogDebug(100175, formatter.str(), __FILE__, __LINE__ );
5280             }
5281             /*------DEBUG LOG END------*/
5282
5283             //search for send_possible item in status list
5284             send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
5285                                              data_send_possible());
5286             //the data that can be sent possible is exist
5287             if (it_find != recv_data.send_status_list.end())
5288             {
5289                 status = CLIENT_CONNECTION_CHECK;
5290             }
5291             //the data that can be sent possible is not exist
5292             else
5293             {
5294                 status = REALSERVER_RECV;
5295             }
5296         }
5297         catch (int e)
5298         {
5299             /*-------- DEBUG LOG --------*/
5300             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5301             {
5302                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5303                                         "handle_realserver_recv() : catch exception e = %d. thread id : %d.");
5304                 formatter % e % boost::this_thread::get_id();
5305                 putLogDebug(100176, formatter.str(), __FILE__, __LINE__ );
5306             }
5307             /*------DEBUG LOG END------*/
5308             status = FINALIZE;
5309         }
5310         catch (const std::string& ex)
5311         {
5312             std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : " << ex << std::endl;
5313             boost::format formatter("protocol_module_sessionless::handle_realserver_recv() : exception : %s. thread id : %d.");
5314             formatter % ex.c_str() % boost::this_thread::get_id();
5315             putLogError(100086, formatter.str(), __FILE__, __LINE__ );
5316             status = FINALIZE;
5317         }
5318         catch (const std::bad_alloc&)
5319         {
5320             std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : Could not allocate memory." << std::endl;
5321             boost::format formatter("Could not allocate memory. thread id : %d.");
5322             formatter % boost::this_thread::get_id();
5323             putLogError(100087, formatter.str(), __FILE__, __LINE__ );
5324             status = FINALIZE;
5325         }
5326         catch (const std::exception& ex)
5327         {
5328             std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : error = " << ex.what() << "." << std::endl;
5329             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5330                                     "handle_realserver_recv() : exception : error = %s. thread id : %d.");
5331             formatter % ex.what() % boost::this_thread::get_id();
5332             putLogError(100088, formatter.str(), __FILE__, __LINE__ );
5333
5334             status = FINALIZE;
5335         }
5336         catch (...)
5337         {
5338             std::cerr << "protocol_module_sessionless::handle_realserver_recv() : Unknown exception." << std::endl;
5339             boost::format formatter("function : protocol_module_base::EVENT_TAG "
5340                                     "protocol_module_sessionless::handle_realserver_recv() : "
5341                                     "Unknown exception. thread id : %d.");
5342             formatter % boost::this_thread::get_id();
5343             putLogError(100089, formatter.str(), __FILE__, __LINE__ );
5344             status = FINALIZE;
5345         }
5346
5347         /*-------- DEBUG LOG --------*/
5348         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5349         {
5350             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5351                                     "handle_realserver_recv(const boost::thread::id thread_id, "
5352                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
5353                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5354                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
5355             formatter % status % boost::this_thread::get_id();
5356             putLogDebug(100177, formatter.str(), __FILE__, __LINE__ );
5357         }
5358         /*------DEBUG LOG END------*/
5359
5360         return status;
5361     }
5362
5363
5364
5365     //! called from after sorryserver recive
5366     //! @param[in]    downstream thread id
5367     //! @param[in]    sorryserver endpoint reference
5368     //! @param[in]    recive from realserver buffer reference.
5369     //! @param[in]    recv data length
5370     //! @return     session use EVENT mode
5371     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_recv(
5372         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& sorry_endpoint, const boost::array<
5373         char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
5374     {
5375         /*-------- DEBUG LOG --------*/
5376         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5377         {
5378             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
5379             std::string buffer;
5380             dump_memory(recvbuffer.data(), buffer_size, buffer);
5381             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5382                                     "handle_sorryserver_recv(const boost::thread::id thread_id, "
5383                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5384                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5385                                     "const size_t recvlen) : thread_id = %d, sorry_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
5386             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port()
5387             % buffer % recvlen;
5388             putLogDebug(100178, formatter.str(), __FILE__, __LINE__ );
5389         }
5390         /*------DEBUG LOG END------*/
5391
5392         EVENT_TAG status = FINALIZE;
5393         size_t data_remain_start = 0;
5394         size_t data_remain_size = 0;
5395         size_t request_data_remain_size = 0;
5396         size_t header_offset = 0;
5397         size_t header_offset_len = 0;
5398         size_t content_length_header_offset = 0;
5399         size_t content_length_header_len = 0;
5400         size_t content_len_value = 0;
5401         size_t pos = 0;
5402         size_t buffer_size = 0;
5403         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
5404         const size_t cr_lf_len = strlen("\r\n");
5405         std::string str_value;
5406         const std::string http_header = "";
5407         const std::string content_header = "Content-Length";
5408         thread_data_ptr session_data;
5409         char* buffer1 = NULL;
5410         char* buffer2 = NULL;
5411         bool bret = false;
5412         CHECK_RESULT_TAG check_result;
5413         session_thread_data_map_it session_thread_it;
5414         recive_data_map_it recive_data_it;
5415
5416         //parameter check
5417         if (recvlen > recvbuffer.size())
5418         {
5419             std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : Data size bigger than buffer size." << std::endl;
5420             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
5421             formatter % boost::this_thread::get_id();
5422             putLogError(100090, formatter.str(), __FILE__,
5423                         __LINE__ );
5424             /*-------- DEBUG LOG --------*/
5425             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5426             {
5427                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5428                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
5429                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5430                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5431                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
5432                 formatter % FINALIZE % boost::this_thread::get_id();
5433                 putLogDebug(100179, formatter.str(), __FILE__, __LINE__ );
5434             }
5435             /*------DEBUG LOG END------*/
5436             return FINALIZE;
5437         }
5438
5439         try
5440         {
5441             {
5442                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex );
5443
5444                 session_thread_it = session_thread_data_map.find(thread_id);
5445                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
5446                 {
5447                     boost::format formatter("Invalid thread id. thread id : %d.");
5448                     formatter % boost::this_thread::get_id();
5449                     putLogError(100091, formatter.str(), __FILE__, __LINE__ );
5450                     throw -1;
5451                 }
5452
5453                 session_data = session_thread_it->second;
5454             }
5455
5456             recive_data_it = session_data->recive_data_map.find(sorry_endpoint);
5457             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
5458             {
5459                 recive_data recv_data;
5460                 session_data->recive_data_map[sorry_endpoint] = recv_data;
5461             }
5462
5463             session_data->target_endpoint = sorry_endpoint;
5464
5465             recive_data& recv_data = session_data->recive_data_map[sorry_endpoint];
5466
5467             //status list check
5468             send_status_it it = recv_data.send_status_list.begin();
5469             send_status_it it_end = recv_data.send_status_list.end();
5470             it = std::find_if(it, it_end, data_send_ok());
5471             if (unlikely(it != it_end))
5472             {
5473                 boost::format formatter("Sending data is invalid. thread id : %d.");
5474                 formatter % boost::this_thread::get_id();
5475                 putLogError(100092, formatter.str(), __FILE__, __LINE__ );
5476                 throw -1;
5477             }
5478
5479             //status list check
5480             it = recv_data.send_status_list.begin();
5481             it = std::adjacent_find(it, it_end, data_send_repeated());
5482             if (unlikely(it != it_end))
5483             {
5484                 boost::format formatter("Sending data is invalid. thread id : %d.");
5485                 formatter % boost::this_thread::get_id();
5486                 putLogError(100093, formatter.str(), __FILE__, __LINE__ );
5487                 throw -1;
5488             }
5489
5490             /*-------- DEBUG LOG --------*/
5491             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5492             {
5493                 std::string datadump;
5494                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5495                                         "send_rest_size = %d, send_possible_size = %d, "
5496                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
5497                 int i = 0;
5498                 for (it = recv_data.send_status_list.begin();
5499                         it != recv_data.send_status_list.end();
5500                         ++it, ++i)
5501                 {
5502                     formatter % i % it->status % it->send_end_size
5503                     % it->send_rest_size % it->send_possible_size
5504                     % it->send_offset % it->unsend_size % it->edit_division;
5505                     datadump += formatter.str();
5506                 }
5507
5508                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5509                                 "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5510
5511                 formatter % recv_data.send_status_list.size() % datadump;
5512                 putLogDebug(100180, formatter.str(), __FILE__, __LINE__ );
5513             }
5514             /*------DEBUG LOG END------*/
5515
5516             it = recv_data.send_status_list.begin();
5517             //get original status info
5518             while (it != it_end)
5519             {
5520                 //item status is SEND_END
5521                 if (it->status == SEND_END)
5522                 {
5523                     //erase from list
5524                     recv_data.send_status_list.erase(it++);
5525                     continue;
5526                 }
5527                 //item status is SEND_CONTINUE
5528                 else if (it->status == SEND_CONTINUE)
5529                 {
5530                     it->send_offset += it->send_end_size;
5531                     data_remain_start = it->send_offset;
5532                     break;
5533                 }
5534                 //item status is SEND_NG
5535                 else
5536                 {
5537                     data_remain_start = it->send_offset;
5538                     data_remain_size = it->unsend_size;
5539                     break;
5540                 }
5541
5542                 ++it;
5543             }
5544             /*-------- DEBUG LOG --------*/
5545             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5546             {
5547                 std::string datadump;
5548                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5549                                         "send_rest_size = %d, send_possible_size = %d, "
5550                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
5551                 int i = 0;
5552                 for (it = recv_data.send_status_list.begin();
5553                         it != recv_data.send_status_list.end();
5554                         ++it, ++i)
5555                 {
5556                     formatter % i % it->status % it->send_end_size
5557                     % it->send_rest_size % it->send_possible_size
5558                     % it->send_offset % it->unsend_size % it->edit_division;
5559                     datadump += formatter.str();
5560                 }
5561
5562                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5563                                 "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5564
5565                 formatter % recv_data.send_status_list.size() % datadump;
5566                 putLogDebug(100181, formatter.str(), __FILE__, __LINE__ );
5567             }
5568             /*------DEBUG LOG END------*/
5569             //recive buffer process
5570             //buffer rest size < request size
5571             if (recv_data.recive_buffer_rest_size < recvlen)
5572             {
5573                 //buffer max size < remain size + request size
5574                 //buffer is need reallocate
5575                 if (recv_data.recive_buffer_max_size < data_remain_size + recvlen)
5576                 {
5577                     //the buffer's size that will be allocated is exceed the upper limit value
5578                     if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen)
5579                     {
5580                         std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : the buffer's size that will be allacted is exceed the upper limit value." << std::endl;
5581                         boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
5582                         formatter % boost::this_thread::get_id();
5583                         putLogError(100094, formatter.str(), __FILE__, __LINE__ );
5584                         /*-------- DEBUG LOG --------*/
5585                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5586                         {
5587                             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5588                                                     "handle_sorryserver_recv(const boost::thread::id thread_id, "
5589                                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5590                                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5591                                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
5592                             formatter % FINALIZE % boost::this_thread::get_id();
5593                             putLogDebug(100182, formatter.str(), __FILE__, __LINE__ );
5594                         }
5595                         /*------DEBUG LOG END------*/
5596                         return FINALIZE;
5597                     }
5598                     //recive_buffer1's memory allocate and initialization
5599                     buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
5600                     buffer1 = new char[buffer_size];
5601                     /*-------- DEBUG LOG --------*/
5602                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5603                     {
5604                         boost::format formatter("new : address = &(%d), size = %lu.");
5605                         formatter % static_cast<void*>(buffer1) % buffer_size;
5606                         putLogDebug(100183, formatter.str(), __FILE__, __LINE__ );
5607                     }
5608                     /*-------- DEBUG LOG END--------*/
5609                     memset(buffer1, 0, buffer_size);
5610                     //recive_buffer2's memory allocate and initialization
5611                     buffer2 = new char[buffer_size];
5612                     /*-------- DEBUG LOG --------*/
5613                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5614                     {
5615                         boost::format formatter("new : address = &(%d), size = %lu.");
5616                         formatter % static_cast<void*>(buffer2) % buffer_size;
5617                         putLogDebug(100184, formatter.str(), __FILE__, __LINE__ );
5618                     }
5619                     /*-------- DEBUG LOG END--------*/
5620                     memset(buffer2, 0, buffer_size);
5621
5622                     /*-------- DEBUG LOG --------*/
5623                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5624                     {
5625                         std::string datadump;
5626                         dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
5627                         boost::format formatter(
5628                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5629                             "handle_sorryserver_recv() : before memcpy (data dump) : "
5630                             "data begin = %d, data_size = %d, data = %s");
5631                         formatter % data_remain_start % data_remain_size % datadump;
5632                         putLogDebug(100185, formatter.str(), __FILE__, __LINE__ );
5633                     }
5634                     /*------DEBUG LOG END------*/
5635                     //copy data from old buffer to new buffer
5636                     memcpy(buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
5637                     /*-------- DEBUG LOG --------*/
5638                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5639                     {
5640                         std::string datadump;
5641                         dump_memory(buffer1, data_remain_size, datadump);
5642                         boost::format formatter(
5643                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5644                             "handle_sorryserver_recv() : after memcpy (data dump) : "
5645                             "data begin = 0, data_size = %d, data = %s");
5646                         formatter % data_remain_size % datadump;
5647                         putLogDebug(100186, formatter.str(), __FILE__, __LINE__ );
5648                     }
5649                     /*------DEBUG LOG END------*/
5650
5651                     /*-------- DEBUG LOG --------*/
5652                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5653                     {
5654                         std::string datadump;
5655                         dump_memory(recvbuffer.data(), recvlen, datadump);
5656                         boost::format formatter(
5657                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5658                             "handle_sorryserver_recv() : before memcpy (data dump) : "
5659                             "data begin = 0, data_size = %d, data = %s");
5660                         formatter % recvlen % datadump;
5661                         putLogDebug(100187, formatter.str(), __FILE__, __LINE__ );
5662                     }
5663                     /*------DEBUG LOG END------*/
5664                     memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
5665                     /*-------- DEBUG LOG --------*/
5666                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5667                     {
5668                         std::string datadump;
5669                         dump_memory(buffer1 + data_remain_size, recvlen, datadump);
5670                         boost::format formatter(
5671                             "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5672                             "handle_sorryserver_recv() : after memcpy (data dump) : "
5673                             "data begin = %d, data_size = %d, data = %s");
5674                         formatter % data_remain_size % recvlen % datadump;
5675                         putLogDebug(100188, formatter.str(), __FILE__, __LINE__ );
5676                     }
5677                     /*------DEBUG LOG END------*/
5678                     //free old buffer1 and old buffer2
5679                     if (recv_data.recive_buffer1 != NULL)
5680                     {
5681                         /*-------- DEBUG LOG --------*/
5682                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5683                         {
5684                             boost::format formatter("delete : address = &(%d).");
5685                             formatter % static_cast<void*>(recv_data.recive_buffer1);
5686                             putLogDebug(100189, formatter.str(), __FILE__,
5687                                         __LINE__ );
5688                         }
5689                         /*------DEBUG LOG END------*/
5690                         delete[] recv_data.recive_buffer1;
5691                         recv_data.recive_buffer1 = NULL;
5692                     }
5693
5694                     if (recv_data.recive_buffer2 != NULL)
5695                     {
5696                         /*-------- DEBUG LOG --------*/
5697                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5698                         {
5699                             boost::format formatter("delete : address = &(%d).");
5700                             formatter % static_cast<void*>(recv_data.recive_buffer2);
5701                             putLogDebug(100190, formatter.str(), __FILE__,
5702                                         __LINE__ );
5703                         }
5704                         /*------DEBUG LOG END------*/
5705                         delete[] recv_data.recive_buffer2;
5706                         recv_data.recive_buffer2 = NULL;
5707                     }
5708
5709                     //set new buffer pointer
5710                     recv_data.recive_buffer1 = buffer1;
5711                     recv_data.recive_buffer2 = buffer2;
5712                     recv_data.recive_buffer = recv_data.recive_buffer1;
5713                     //set new buffer's max size
5714                     recv_data.recive_buffer_max_size = buffer_size;
5715                 }
5716                 //buffer's max size >= remain data size + requst size
5717                 //buffer isn't need reallocate, but switch
5718                 else
5719                 {
5720                     //pointer valid check
5721                     if (unlikely(recv_data.recive_buffer1 == NULL || recv_data.recive_buffer2 == NULL))
5722                     {
5723                         boost::format formatter("Invalid pointer. thread id : %d.");
5724                         formatter % boost::this_thread::get_id();
5725                         putLogError(100095, formatter.str(), __FILE__, __LINE__ );
5726                         throw -1;
5727                     }
5728                     //using buffer is buffer1
5729                     if (recv_data.recive_buffer == recv_data.recive_buffer1)
5730                     {
5731                         //buffer2 initialization
5732                         memset(recv_data.recive_buffer2, 0, recv_data.recive_buffer_max_size);
5733                         /*-------- DEBUG LOG --------*/
5734                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5735                         {
5736                             std::string datadump;
5737                             dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
5738                             boost::format formatter(
5739                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5740                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5741                                 "data begin = %d, data_size = %d, data = %s");
5742                             formatter % data_remain_start % data_remain_size % datadump;
5743                             putLogDebug(100191, formatter.str(), __FILE__, __LINE__ );
5744                         }
5745                         /*------DEBUG LOG END------*/
5746                         //copy data from buffer1 to buffer2
5747                         memcpy(recv_data.recive_buffer2, recv_data.recive_buffer + data_remain_start, data_remain_size);
5748                         /*-------- DEBUG LOG --------*/
5749                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5750                         {
5751                             std::string datadump;
5752                             dump_memory(recv_data.recive_buffer2, data_remain_size, datadump);
5753                             boost::format formatter(
5754                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5755                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5756                                 "data begin = 0, data_size = %d, data = %s");
5757                             formatter % data_remain_size % datadump;
5758                             putLogDebug(100192, formatter.str(), __FILE__, __LINE__ );
5759                         }
5760                         /*------DEBUG LOG END------*/
5761                         /*-------- DEBUG LOG --------*/
5762                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5763                         {
5764                             std::string datadump;
5765                             dump_memory(recvbuffer.data(), recvlen, datadump);
5766                             boost::format formatter(
5767                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5768                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5769                                 "data begin = 0, data_size = %d, data = %s");
5770                             formatter % recvlen % datadump;
5771                             putLogDebug(100193, formatter.str(), __FILE__, __LINE__ );
5772                         }
5773                         /*------DEBUG LOG END------*/
5774                         memcpy(recv_data.recive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
5775                         /*-------- DEBUG LOG --------*/
5776                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5777                         {
5778                             std::string datadump;
5779                             dump_memory(recv_data.recive_buffer2 + data_remain_size, recvlen, datadump);
5780                             boost::format formatter(
5781                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5782                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5783                                 "data begin = %d, data_size = %d, data = %s");
5784                             formatter % data_remain_size % recvlen % datadump;
5785                             putLogDebug(100194, formatter.str(), __FILE__, __LINE__ );
5786                         }
5787                         /*------DEBUG LOG END------*/
5788                         //set buffer2 as using buffer
5789                         recv_data.recive_buffer = recv_data.recive_buffer2;
5790                     }
5791                     //using buffer is buffer2
5792                     else
5793                     {
5794                         //buffer1 initializtion
5795                         memset(recv_data.recive_buffer1, 0, recv_data.recive_buffer_max_size);
5796                         /*-------- DEBUG LOG --------*/
5797                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5798                         {
5799                             std::string datadump;
5800                             dump_memory(recv_data.recive_buffer + data_remain_start, data_remain_size, datadump);
5801                             boost::format formatter(
5802                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5803                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5804                                 "data begin = %d, data_size = %d, data = %s");
5805                             formatter % data_remain_start % data_remain_size % datadump;
5806                             putLogDebug(100195, formatter.str(), __FILE__, __LINE__ );
5807                         }
5808                         /*------DEBUG LOG END------*/
5809                         //copy data from buffer2 to buffer1
5810                         memcpy(recv_data.recive_buffer1, recv_data.recive_buffer + data_remain_start, data_remain_size);
5811                         /*-------- DEBUG LOG --------*/
5812                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5813                         {
5814                             std::string datadump;
5815                             dump_memory(recv_data.recive_buffer1, data_remain_size, datadump);
5816                             boost::format formatter(
5817                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5818                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5819                                 "data begin = 0, data_size = %d, data = %s");
5820                             formatter % data_remain_size % datadump;
5821                             putLogDebug(100196, formatter.str(), __FILE__, __LINE__ );
5822                         }
5823                         /*------DEBUG LOG END------*/
5824
5825                         /*-------- DEBUG LOG --------*/
5826                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5827                         {
5828                             std::string datadump;
5829                             dump_memory(recvbuffer.data(), recvlen, datadump);
5830                             boost::format formatter(
5831                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5832                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5833                                 "data begin = 0, data_size = %d, data = %s");
5834                             formatter % recvlen % datadump;
5835                             putLogDebug(100197, formatter.str(), __FILE__, __LINE__ );
5836                         }
5837                         /*------DEBUG LOG END------*/
5838                         memcpy(recv_data.recive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
5839                         /*-------- DEBUG LOG --------*/
5840                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5841                         {
5842                             std::string datadump;
5843                             dump_memory(recv_data.recive_buffer1 + data_remain_size, recvlen, datadump);
5844                             boost::format formatter(
5845                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5846                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5847                                 "data begin = %d, data_size = %d, data = %s");
5848                             formatter % data_remain_size % recvlen % datadump;
5849                             putLogDebug(100198, formatter.str(), __FILE__, __LINE__ );
5850                         }
5851                         /*------DEBUG LOG END------*/
5852                         //set buffer1 as using buffer
5853                         recv_data.recive_buffer = recv_data.recive_buffer1;
5854                     }
5855                 }
5856
5857                 //set buffer's rest size
5858                 recv_data.recive_buffer_rest_size = recv_data.recive_buffer_max_size - data_remain_size - recvlen;
5859
5860                 //remain_size recalc
5861                 data_remain_size += recvlen;
5862
5863                 send_status_it it_begin = recv_data.send_status_list.begin();
5864                 send_status_it it_end = recv_data.send_status_list.end();
5865                 //offset recalc
5866                 for (; it_begin != it_end; ++it_begin)
5867                 {
5868                     it_begin->send_offset -= data_remain_start;
5869                 }
5870             }
5871             //buffer's rest size >= request size
5872             //copy directly
5873             else
5874             {
5875                 //pointer valid check
5876                 if (unlikely(recv_data.recive_buffer == NULL))
5877                 {
5878                     boost::format formatter("Invalid pointer. thread id : %d");
5879                     formatter % boost::this_thread::get_id();
5880                     putLogError(100096, formatter.str(), __FILE__, __LINE__ );
5881                     throw -1;
5882                 }
5883                 /*-------- DEBUG LOG --------*/
5884                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5885                 {
5886                     std::string datadump;
5887                     dump_memory(recvbuffer.data(), recvlen, datadump);
5888                     boost::format formatter(
5889                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5890                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5891                         "data begin = 0, data_size = %d, data = %s");
5892                     formatter % recvlen % datadump;
5893                     putLogDebug(100199, formatter.str(), __FILE__, __LINE__ );
5894                 }
5895                 /*------DEBUG LOG END------*/
5896
5897                 //copy data from parameter to using buffer
5898                 memcpy(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size,
5899                        recvbuffer.data(), recvlen);
5900                 /*-------- DEBUG LOG --------*/
5901                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5902                 {
5903                     std::string datadump;
5904                     dump_memory(recv_data.recive_buffer + recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size, recvlen, datadump);
5905                     boost::format formatter(
5906                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5907                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5908                         "data begin = %d, data_size = %d, data = %s");
5909                     formatter % (recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size) % recvlen % datadump;
5910                     putLogDebug(100200, formatter.str(), __FILE__, __LINE__ );
5911                 }
5912                 /*------DEBUG LOG END------*/
5913                 //buffer's rest size recalc
5914                 recv_data.recive_buffer_rest_size -= recvlen;
5915                 //remain data size recalc
5916                 data_remain_size += recvlen;
5917             }
5918
5919             it = recv_data.send_status_list.begin();
5920             it_end = recv_data.send_status_list.end();
5921             //request rest size initialization
5922             request_data_remain_size = recvlen;
5923             //original status process
5924             for (; it != it_end; ++it)
5925             {
5926                 //status is SEND_CONTINUE
5927                 if (it->status == SEND_CONTINUE)
5928                 {
5929                     //
5930                     if (it->send_rest_size > request_data_remain_size)
5931                     {
5932                         it->send_possible_size = request_data_remain_size;
5933                         it->send_rest_size -= request_data_remain_size;
5934                         it->send_end_size = 0;
5935                         request_data_remain_size = 0;
5936                     }
5937                     else
5938                     {
5939                         it->send_possible_size = it->send_rest_size;
5940                         request_data_remain_size -= it->send_rest_size;
5941                         it->send_end_size = 0;
5942                         it->send_rest_size = 0;
5943                     }
5944
5945                     //change status from SEND_CONTINUE to SEND_OK
5946                     it->status = SEND_OK;
5947                 }
5948                 //status is SEND_NG
5949                 else if (it->status == SEND_NG)
5950                 {
5951                     //check http method
5952                     check_result = check_status_code(recv_data.recive_buffer + it->send_offset, data_remain_size);
5953                     /*-------- DEBUG LOG --------*/
5954                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5955                     {
5956                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5957                                                 "handle_sorryserver_recv() : call check_status_code : "
5958                                                 "return_value = %d. thread id : %d.");
5959                         formatter % check_result % boost::this_thread::get_id();
5960                         putLogDebug(100201, formatter.str(), __FILE__, __LINE__ );
5961                     }
5962                     /*------DEBUG LOG END------*/
5963                     //check http method result is OK
5964                     if (check_result == CHECK_OK)
5965                     {
5966                         //check http version
5967                         check_result = check_http_version(recv_data.recive_buffer + it->send_offset, data_remain_size);
5968                         /*-------- DEBUG LOG --------*/
5969                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5970                         {
5971                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5972                                                     "handle_sorryserver_recv() : call check_http_version : "
5973                                                     "return_value = %d. thread id : %d.");
5974                             formatter % check_result % boost::this_thread::get_id();
5975                             putLogDebug(100202, formatter.str(), __FILE__, __LINE__ );
5976                         }
5977                         /*------DEBUG LOG END------*/
5978                     }
5979                     //check method and version result is OK
5980                     if (check_result == CHECK_OK)
5981                     {
5982                         //search http header
5983                         bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size, http_header,
5984                                                 header_offset, header_offset_len);
5985                         /*-------- DEBUG LOG --------*/
5986                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5987                         {
5988                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5989                                                     "handle_sorryserver_recv() : call find_http_header : "
5990                                                     "return_value = %d. thread id : %d.");
5991                             formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5992                             putLogDebug(100203, formatter.str(), __FILE__, __LINE__ );
5993                         }
5994                         /*------DEBUG LOG END------*/
5995                         //search http header result is OK
5996                         if (bret)
5997                         {
5998                             //search Content_Length header
5999                             bret = find_http_header(recv_data.recive_buffer + it->send_offset, data_remain_size,
6000                                                     content_header, content_length_header_offset, content_length_header_len);
6001                             /*-------- DEBUG LOG --------*/
6002                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6003                             {
6004                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6005                                                         "handle_sorryserver_recv() : call find_http_header : "
6006                                                         "return_value = %d. thread id : %d.");
6007                                 formatter % static_cast<int>(bret) %boost::this_thread::get_id();
6008                                 putLogDebug(100204, formatter.str(), __FILE__, __LINE__ );
6009                             }
6010                             /*------DEBUG LOG END------*/
6011                             //search Content_Length result is OK
6012                             if (bret)
6013                             {
6014                                 //Get Content_Length header's numeric value
6015                                 for (pos = 0; recv_data.recive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
6016                                         < content_length_header_len; ++pos)
6017                                     ;
6018                                 if (pos == content_length_header_len)
6019                                 {
6020                                     throw std::string("Content_Length field's value is invalid.");
6021                                 }
6022
6023                                 ++pos;
6024
6025                                 str_value.assign(recv_data.recive_buffer + it->send_offset + content_length_header_offset + pos,
6026                                                  content_length_header_len - pos);
6027
6028                                 size_t pos_end = str_value.find_last_of('\r');
6029                                 if (pos_end != std::string::npos)
6030                                 {
6031                                     str_value = str_value.erase(pos_end);
6032                                 }
6033
6034                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
6035                                     ;
6036
6037                                 str_value = str_value.substr(pos);
6038
6039                                 try
6040                                 {
6041                                     content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
6042                                 }
6043                                 catch (const boost::bad_lexical_cast& ex)
6044                                 {
6045                                     throw std::string("Content_Length field's value is invalid.");
6046                                 }
6047                                 //send_rest_size recalc
6048                                 //set whole http header's length + Content_Length's value
6049                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
6050                             }
6051                             //search Content_Length result is NG
6052                             else
6053                             {
6054                                 //send_rest_size recalc
6055                                 //set whole http header's length
6056                                 if (header_offset_len == 0)
6057                                 {
6058                                     it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
6059                                 }
6060                                 else
6061                                 {
6062                                     it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
6063                                 }
6064                             }
6065                         }
6066                         //search http header result is NG
6067                         else
6068                         {
6069                             it->unsend_size += request_data_remain_size;
6070                             request_data_remain_size = 0;
6071                             break;
6072                         }
6073                     }
6074                     //check method or version result is NG
6075                     else if (check_result == CHECK_NG)
6076                     {
6077                         it->send_rest_size = it->unsend_size+request_data_remain_size;
6078                     }
6079                     //check method and version result is CHECK_INPOSSIBLE
6080                     else
6081                     {
6082                         it->unsend_size += request_data_remain_size;
6083                         request_data_remain_size = 0;
6084                         break;
6085                     }
6086
6087                     //recalc fields value according to send_rest_size and request rest size
6088                     if (it->send_rest_size > it->unsend_size + request_data_remain_size)
6089                     {
6090                         it->send_possible_size = it->unsend_size + request_data_remain_size;
6091                         it->send_rest_size -= (it->unsend_size + request_data_remain_size);
6092                         it->send_end_size = 0;
6093                         it->unsend_size = 0;
6094                         request_data_remain_size = 0;
6095                     }
6096                     else
6097                     {
6098                         it->send_possible_size = it->send_rest_size;
6099                         request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
6100                         it->send_end_size = 0;
6101                         it->unsend_size = 0;
6102                         it->send_rest_size = 0;
6103                     }
6104
6105                     //change status from SEND_NG to SEND_OK
6106                     it->status = SEND_OK;
6107                 }
6108                 //no request rest data to process
6109                 if (request_data_remain_size <= 0)
6110                 {
6111                     break;
6112                 }
6113             }
6114             /*-------- DEBUG LOG --------*/
6115             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6116             {
6117                 std::string datadump;
6118                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
6119                                         "send_rest_size = %d, send_possible_size = %d, "
6120                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
6121                 int i = 0;
6122                 for (it = recv_data.send_status_list.begin();
6123                         it != recv_data.send_status_list.end();
6124                         ++it, ++i)
6125                 {
6126                     formatter % i % it->status % it->send_end_size
6127                     % it->send_rest_size % it->send_possible_size
6128                     % it->send_offset % it->unsend_size % it->edit_division;
6129                     datadump += formatter.str();
6130                 }
6131
6132                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6133                                 "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
6134
6135                 formatter % recv_data.send_status_list.size() % datadump;
6136                 putLogDebug(100205, formatter.str(), __FILE__, __LINE__ );
6137             }
6138             /*------DEBUG LOG END------*/
6139
6140             //there are still rest data need to process
6141             //new status created and add to status list
6142             while (request_data_remain_size > 0)
6143             {
6144                 //new status created
6145                 send_status new_send_state;
6146                 new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
6147                 new_send_state.send_end_size = 0;
6148                 new_send_state.send_offset = 0;
6149                 new_send_state.send_possible_size = 0;
6150                 new_send_state.unsend_size = 0;
6151                 new_send_state.send_rest_size = 0;
6152                 //status initialize to SEND_NG
6153                 new_send_state.status = SEND_NG;
6154                 //add new status to status_list
6155                 recv_data.send_status_list.push_back(new_send_state);
6156                 std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
6157                 //cacl offset
6158                 new_send_it->send_offset = recv_data.recive_buffer_max_size - recv_data.recive_buffer_rest_size
6159                                            - request_data_remain_size;
6160
6161                 //check http method
6162                 check_result = check_status_code(recv_data.recive_buffer + new_send_it->send_offset,
6163                                                  request_data_remain_size);
6164                 /*-------- DEBUG LOG --------*/
6165                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6166                 {
6167                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6168                                             "handle_sorryserver_recv() : call check_status_code : "
6169                                             "return_value = %d. thread id : %d.");
6170                     formatter % check_result % boost::this_thread::get_id();
6171                     putLogDebug(100206, formatter.str(), __FILE__, __LINE__ );
6172                 }
6173                 /*------DEBUG LOG END------*/
6174                 //check http method result is OK
6175                 if (check_result == CHECK_OK)
6176                 {
6177                     //check http version
6178                     check_result = check_http_version(recv_data.recive_buffer + new_send_it->send_offset,
6179                                                       request_data_remain_size);
6180                     /*-------- DEBUG LOG --------*/
6181                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
6182                     {
6183                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6184                                                 "handle_sorryserver_recv() : call check_http_version : "
6185                                                 "return_value = %d. thread id : %d.");
6186                         formatter % check_result % boost::this_thread::get_id();
6187                         putLogDebug(100207, formatter.str(), __FILE__, __LINE__ );
6188                     }
6189                     /*------DEBUG LOG END------*/
6190                 }
6191                 //check http method and version result is OK
6192                 if (check_result == CHECK_OK)
6193                 {
6194                     //search whole http header, get whole http header's offset and length
6195                     bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset, request_data_remain_size,
6196                                             http_header, header_offset, header_offset_len);
6197                     /*-------- DEBUG LOG --------*/
6198                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
6199                     {
6200                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6201                                                 "handle_sorryserver_recv() : call find_http_header : "
6202                                                 "return_value = %d. thread id : %d.");
6203                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
6204                         putLogDebug(100208, formatter.str(), __FILE__, __LINE__ );
6205                     }
6206                     /*------DEBUG LOG END------*/
6207                     //searched whole http header
6208                     if (bret)
6209                     {
6210                         //search ContentLength http header, get ContentLength header's offset and length
6211                         bret = find_http_header(recv_data.recive_buffer + new_send_it->send_offset,
6212                                                 request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
6213                         /*-------- DEBUG LOG --------*/
6214                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6215                         {
6216                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6217                                                     "handle_sorryserver_recv() : call find_http_header : "
6218                                                     "return_value = %d. thread id : %d.");
6219                             formatter % static_cast<int>(bret) % boost::this_thread::get_id();
6220                             putLogDebug(100209, formatter.str(), __FILE__, __LINE__ );
6221                         }
6222                         /*------DEBUG LOG END------*/
6223
6224                         //searched ContentLength http header
6225                         if (bret)
6226                         {
6227                             //Get Content_Length header's numeric value
6228                             for (pos = 0; recv_data.recive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
6229                                     && pos < content_length_header_len; ++pos)
6230                                 ;
6231                             if (pos == content_length_header_len)
6232                             {
6233                                 throw std::string("Content_Length field's value is invalid.");
6234                             }
6235                             ++pos;
6236
6237                             str_value.assign(recv_data.recive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
6238                                              content_length_header_len - pos);
6239
6240                             size_t pos_end = str_value.find_last_of('\r');
6241                             if (pos_end != std::string::npos)
6242                             {
6243                                 str_value = str_value.erase(pos_end);
6244                             }
6245
6246                             for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
6247                                 ;
6248
6249                             str_value = str_value.substr(pos);
6250                             try
6251                             {
6252                                 content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
6253                             }
6254                             catch (const boost::bad_lexical_cast& ex)
6255                             {
6256                                 throw std::string("Content_Length field's value is invalid.");
6257                             }
6258                             //send_rest_size recalc
6259                             //set whole http header's  + whole http header's length + Content_Length's value
6260                             new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
6261                         }
6262                         //not searched ContentLength http header
6263                         else
6264                         {
6265                             //send_rest_size recalc
6266                             //set whole http header's  + whole http header's length
6267                             if (header_offset_len == 0)
6268                             {
6269                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
6270                             }
6271                             else
6272                             {
6273                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
6274                             }
6275
6276
6277                         }
6278                     }
6279                     //not searched whole http header
6280                     else
6281                     {
6282                         new_send_it->unsend_size = request_data_remain_size;
6283                         request_data_remain_size = 0;
6284                         break;
6285                     }
6286                 }
6287                 //check http method or version result is NG
6288                 else if (check_result == CHECK_NG)
6289                 {
6290                     new_send_it->send_rest_size = request_data_remain_size;
6291                 }
6292                 //check http method or version result is impossible
6293                 else
6294                 {
6295                     new_send_it->unsend_size = request_data_remain_size;
6296                     request_data_remain_size = 0;
6297                     break;
6298                 }
6299
6300                 //recalc fields value according to send_rest_size and request rest size
6301                 if (new_send_it->send_rest_size > request_data_remain_size)
6302                 {
6303                     new_send_it->send_possible_size = request_data_remain_size;
6304                     new_send_it->send_rest_size -= request_data_remain_size;
6305                     new_send_it->send_end_size = 0;
6306                     request_data_remain_size = 0;
6307                 }
6308                 else
6309                 {
6310                     new_send_it->send_possible_size = new_send_it->send_rest_size;
6311                     request_data_remain_size -= new_send_it->send_rest_size;
6312                     new_send_it->send_end_size = 0;
6313                     new_send_it->send_rest_size = 0;
6314                 }
6315
6316                 //change status from SEND_NG to SEND_OK
6317                 new_send_it->status = SEND_OK;
6318             }
6319             /*-------- DEBUG LOG --------*/
6320             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6321             {
6322                 std::string datadump;
6323                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
6324                                         "send_rest_size = %d, send_possible_size = %d, "
6325                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
6326                 int i = 0;
6327                 for (it = recv_data.send_status_list.begin();
6328                         it != recv_data.send_status_list.end();
6329                         ++it, ++i)
6330                 {
6331                     formatter % i % it->status % it->send_end_size
6332                     % it->send_rest_size % it->send_possible_size
6333                     % it->send_offset % it->unsend_size % it->edit_division;
6334                     datadump += formatter.str();
6335                 }
6336
6337                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6338                                 "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
6339
6340                 formatter % recv_data.send_status_list.size() % datadump;
6341                 putLogDebug(100210, formatter.str(), __FILE__, __LINE__ );
6342             }
6343             /*------DEBUG LOG END------*/
6344
6345             //search for send_possible item in status list
6346             send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6347                                              data_send_possible());
6348             //the data that can be sent possible is exist
6349             if (it_find != recv_data.send_status_list.end())
6350             {
6351                 status = CLIENT_CONNECTION_CHECK;
6352             }
6353             //the data that can be sent possible is not exist
6354             else
6355             {
6356                 status = SORRYSERVER_RECV;
6357             }
6358         }
6359         catch (int e)
6360         {
6361             /*-------- DEBUG LOG --------*/
6362             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6363             {
6364                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6365                                         "handle_sorryserver_recv() : catch exception e = %d. thread id : %d.");
6366                 formatter % e % boost::this_thread::get_id();
6367                 putLogDebug(100211, formatter.str(), __FILE__, __LINE__ );
6368             }
6369             /*------DEBUG LOG END------*/
6370             status = FINALIZE;
6371         }
6372         catch (const std::string& ex)
6373         {
6374             std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : " << ex << std::endl;
6375             boost::format formatter("protocol_module_sessionless::handle_sorryserver_recv() : exception : %s. thread id : %d.");
6376             formatter % ex.c_str() % boost::this_thread::get_id();
6377             putLogError(100097, formatter.str(), __FILE__, __LINE__ );
6378             status = FINALIZE;
6379         }
6380         catch (const std::bad_alloc&)
6381         {
6382             std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : Could not allocate memory." << std::endl;
6383             boost::format formatter("Could not allocate memory. thread id : %d.");
6384             formatter % boost::this_thread::get_id();
6385             putLogError(100098, formatter.str(), __FILE__, __LINE__ );
6386             status = FINALIZE;
6387         }
6388         catch (const std::exception& ex)
6389         {
6390             std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : error = " << ex.what() << "." << std::endl;
6391             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6392                                     "handle_sorryserver_recv() : exception : error = %s. thread id : %d.");
6393             formatter % ex.what() % boost::this_thread::get_id();
6394             putLogError(100099, formatter.str(), __FILE__, __LINE__ );
6395
6396             status = FINALIZE;
6397         }
6398         catch (...)
6399         {
6400             std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : Unknown exception." << std::endl;
6401             boost::format formatter("function : protocol_module_base::EVENT_TAG "
6402                                     "protocol_module_sessionless::handle_sorryserver_recv() : "
6403                                     "Unknown exception. thread id : %d.");
6404             formatter % boost::this_thread::get_id();
6405             putLogError(100100, formatter.str(), __FILE__, __LINE__ );
6406             status = FINALIZE;
6407         }
6408
6409         /*-------- DEBUG LOG --------*/
6410         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6411         {
6412             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6413                                     "handle_sorryserver_recv(const boost::thread::id thread_id, "
6414                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
6415                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
6416                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
6417             formatter % status % boost::this_thread::get_id();
6418             putLogDebug(100212, formatter.str(), __FILE__, __LINE__ );
6419         }
6420         /*------DEBUG LOG END------*/
6421
6422         return status;
6423     }
6424
6425     //! called from UPSTEEARM thread. make module original message.
6426     //! @param[in]    downstream thread id.
6427     //! @return     session use EVENT mode
6428     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_response_send_inform(
6429         const boost::thread::id thread_id)
6430     {
6431         /*-------- DEBUG LOG --------*/
6432         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6433         {
6434             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6435                                     "handle_response_send_inform(const boost::thread::id thread_id) : "
6436                                     "return_value = %d. thread id : %d.");
6437             formatter % STOP % boost::this_thread::get_id();
6438             putLogDebug(100213, formatter.str(), __FILE__, __LINE__ );
6439         }
6440         /*------DEBUG LOG END------*/
6441         return STOP;
6442     }
6443
6444     //! called from after client connection check. use TCP/IP only. create client send message.
6445     //! @param[in]    downstream thread id
6446     //! @param[out]    send budffer reference
6447     //! @param[out]    send data length
6448     //! @return     session use EVENT mode
6449     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_connection_check(
6450         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
6451     {
6452
6453         /*-------- DEBUG LOG --------*/
6454         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6455         {
6456             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6457                                     "handle_client_connection_check(const boost::thread::id thread_id, "
6458                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
6459                                     "thread_id = %d.");
6460             formatter % thread_id;
6461             putLogDebug(100214, formatter.str(), __FILE__, __LINE__ );
6462         }
6463         /*------DEBUG LOG END------*/
6464
6465         EVENT_TAG status = FINALIZE;
6466         size_t send_buffer_size = sendbuffer.max_size();
6467         thread_data_ptr session_data;
6468         session_thread_data_map_it session_thread_it;
6469         recive_data_map_it recive_data_it;
6470
6471         try
6472         {
6473             {
6474                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6475
6476                 session_thread_it = session_thread_data_map.find(thread_id);
6477                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
6478                 {
6479                     boost::format formatter("Invalid thread id. thread id : %d.");
6480                     formatter % boost::this_thread::get_id();
6481                     putLogError(100101, formatter.str(), __FILE__, __LINE__ );
6482                     throw -1;
6483                 }
6484
6485                 session_data = session_thread_it->second;
6486             }
6487
6488             recive_data_it = session_data->recive_data_map.find(session_data->target_endpoint);
6489             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
6490             {
6491                 boost::format formatter("Invalid endpoint. thread id : %d.");
6492                 formatter % boost::this_thread::get_id();
6493                 putLogError(100102, formatter.str(), __FILE__, __LINE__ );
6494                 throw -1;
6495             }
6496
6497             recive_data& recv_data = recive_data_it->second;
6498
6499             //get the data that can be sent possible
6500             send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6501                                         data_send_possible());
6502             if (unlikely(it == recv_data.send_status_list.end()))
6503             {
6504                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
6505                 formatter % boost::this_thread::get_id();
6506                 putLogError(100103, formatter.str(), __FILE__, __LINE__ );
6507                 throw -1;
6508             }
6509
6510             //buffer size >= sending_possible size
6511             if (send_buffer_size > it->send_possible_size)
6512             {
6513                 /*-------- DEBUG LOG --------*/
6514                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6515                 {
6516                     std::string datadump;
6517                     dump_memory(recv_data.recive_buffer + it->send_offset, it->send_possible_size, datadump);
6518
6519                     boost::format formatter(
6520                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6521                         "handle_client_connection_check() : before memcpy (data dump) : "
6522                         "data begin = %d, data_size = %d, data = %s");
6523                     formatter % it->send_offset % (it->send_possible_size) % datadump;
6524                     putLogDebug(100215, formatter.str(), __FILE__, __LINE__ );
6525                 }
6526                 /*------DEBUG LOG END------*/
6527                 //copy data from recive_buffer to sendbuffer by sending_possible size
6528                 memcpy(sendbuffer.data(), recv_data.recive_buffer + it->send_offset, it->send_possible_size);
6529                 /*-------- DEBUG LOG --------*/
6530                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6531                 {
6532                     std::string datadump;
6533                     dump_memory(sendbuffer.data(), it->send_possible_size, datadump);
6534
6535                     boost::format formatter(
6536                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6537                         "handle_client_connection_check() : after memcpy (data dump) : "
6538                         "data begin = 0, data_size = %d, data = %s");
6539                     formatter % (it->send_possible_size) % datadump;
6540                     putLogDebug(100216, formatter.str(), __FILE__, __LINE__ );
6541                 }
6542                 /*------DEBUG LOG END------*/
6543                 //send_end_size recalc
6544                 it->send_end_size = it->send_possible_size;
6545                 //set copyed data length
6546                 datalen = it->send_possible_size;
6547                 //sending_possible size recalc
6548                 it->send_possible_size = 0;
6549             }
6550             //buffer size < sending_possible size
6551             else
6552             {
6553                 /*-------- DEBUG LOG --------*/
6554                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6555                 {
6556                     std::string datadump;
6557                     dump_memory(recv_data.recive_buffer + it->send_offset, send_buffer_size, datadump);
6558
6559                     boost::format formatter(
6560                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6561                         "handle_client_connection_check() : before memcpy (data dump) : "
6562                         "data begin = %d, data_size = %d, data = %s");
6563                     formatter % it->send_offset % send_buffer_size % datadump;
6564                     putLogDebug(100217, formatter.str(), __FILE__, __LINE__ );
6565                 }
6566                 /*------DEBUG LOG END------*/
6567                 //copy data from recive_buffer to sendbuffer by buffer size
6568                 memcpy(sendbuffer.data(), recv_data.recive_buffer + it->send_offset, send_buffer_size);
6569                 /*-------- DEBUG LOG --------*/
6570                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6571                 {
6572                     std::string datadump;
6573                     dump_memory(sendbuffer.data(), send_buffer_size, datadump);
6574
6575                     boost::format formatter(
6576                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6577                         "handle_client_connection_check() : after memcpy (data dump) : "
6578                         "data begin = 0, data_size = %d, data = %s");
6579                     formatter % send_buffer_size % datadump;
6580                     putLogDebug(100218, formatter.str(), __FILE__, __LINE__ );
6581                 }
6582                 /*------DEBUG LOG END------*/
6583                 //send_end_size recalc
6584                 it->send_end_size = send_buffer_size;
6585                 //sending_possible size recalc
6586                 it->send_possible_size -= send_buffer_size;
6587                 //set copyed data length
6588                 datalen = send_buffer_size;
6589             }
6590
6591             status = CLIENT_SEND;
6592         }
6593         catch (int e)
6594         {
6595             /*-------- DEBUG LOG --------*/
6596             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6597             {
6598                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6599                                         "handle_client_connection_check() : catch exception e = %d. thread id : %d.");
6600                 formatter % e % boost::this_thread::get_id();
6601                 putLogDebug(100219, formatter.str(), __FILE__, __LINE__ );
6602             }
6603             /*------DEBUG LOG END------*/
6604             status = FINALIZE;
6605         }
6606         catch (const std::exception& ex)
6607         {
6608             std::cerr << "protocol_module_sessionless::handle_client_connection_check() : exception : error = " << ex.what() << "." << std::endl;
6609             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6610                                     "handle_client_connection_check() : exception : error = %s. thread id : %d.");
6611             formatter % ex.what() % boost::this_thread::get_id();
6612             putLogError(100104, formatter.str(), __FILE__, __LINE__ );
6613             status = FINALIZE;
6614         }
6615         catch (...)
6616         {
6617             std::cerr << "protocol_module_sessionless::handle_client_connection_check() : Unknown exception." << std::endl;
6618             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6619                                     "handle_client_connection_check() : Unknown exception. thread id : %d.");
6620             formatter % boost::this_thread::get_id();
6621             putLogError(100105, formatter.str(), __FILE__, __LINE__ );
6622             status = FINALIZE;
6623         }
6624
6625         /*-------- DEBUG LOG --------*/
6626         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6627         {
6628             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6629                                     "handle_client_connection_check(const boost::thread::id thread_id, "
6630                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)"
6631                                     " : return_value = %d. thread id : %d.");
6632             formatter % status % boost::this_thread::get_id();
6633             putLogDebug(100220, formatter.str(), __FILE__, __LINE__ );
6634         }
6635         /*------DEBUG LOG END------*/
6636         return status;
6637     }
6638
6639     //! called from after client select. use UDP only
6640     //! @param[in]    downstream thread id
6641     //!    @param[in]    client udp endpoint
6642     //! @param[out]    send buffer reference
6643     //! @param[out]    send data length
6644     //! @return     session use EVENT mode
6645     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_select(
6646         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint& cl_endpoint, boost::array<char,
6647         MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
6648     {
6649         /*-------- DEBUG LOG --------*/
6650         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6651         {
6652             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6653                                     "handle_client_select(const boost::thread::id thread_id, "
6654                                     "boost::asio::ip::udp::endpoint& cl_endpoint, "
6655                                     "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
6656                                     "size_t& datalen) : "
6657                                     "return_value = %d. thread id : %d.");
6658             formatter % STOP % boost::this_thread::get_id();
6659             putLogDebug(100221, formatter.str(), __FILE__, __LINE__ );
6660         }
6661         /*------DEBUG LOG END------*/
6662         return STOP;
6663     }
6664
6665     //!    called from after client send
6666     //!    @param[in]    downstream thread id
6667     //! @return     session use EVENT mode
6668     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_send(
6669         const boost::thread::id thread_id)
6670     {
6671         /*-------- DEBUG LOG --------*/
6672         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6673         {
6674             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6675                                     "handle_client_send(const boost::thread::id thread_id) : "
6676                                     "thread_id = %d.");
6677             formatter % thread_id;
6678             putLogDebug(100222, formatter.str(), __FILE__, __LINE__ );
6679         }
6680         /*------DEBUG LOG END------*/
6681         EVENT_TAG status = FINALIZE;
6682         thread_data_ptr session_data;
6683         session_thread_data_map_it session_thread_it;
6684         recive_data_map_it recive_data_it;
6685
6686         try
6687         {
6688             {
6689                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6690                 //thread_id check
6691                 session_thread_it = session_thread_data_map.find(thread_id);
6692                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
6693                 {
6694                     boost::format formatter("Invalid thread id. thread id : %d.");
6695                     formatter % boost::this_thread::get_id();
6696                     putLogError(100106, formatter.str(), __FILE__, __LINE__ );
6697                     throw -1;
6698                 }
6699                 session_data = session_thread_it->second;
6700             }
6701             //endpoint check
6702             recive_data_it = session_data->recive_data_map.find(session_data->target_endpoint);
6703             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
6704             {
6705                 boost::format formatter("Invalid endpoint. thread id : %d.");
6706                 formatter % boost::this_thread::get_id();
6707                 putLogError(100107, formatter.str(), __FILE__, __LINE__ );
6708                 throw -1;
6709             }
6710
6711             recive_data& recv_data = recive_data_it->second;
6712
6713             send_status_it it = recv_data.send_status_list.begin();
6714             send_status_it it_end = recv_data.send_status_list.end();
6715
6716             //check status list
6717             it = std::adjacent_find(it, it_end, data_send_list_incorrect());
6718             if (unlikely(it != it_end))
6719             {
6720                 boost::format formatter("Sending possible data is invalid. thread id : %d.");
6721                 formatter % boost::this_thread::get_id();
6722                 putLogError(100108, formatter.str(), __FILE__, __LINE__ );
6723                 throw -1;
6724             }
6725             //status list check
6726             it = recv_data.send_status_list.begin();
6727             it = find_if(it, it_end, data_send_ok());
6728             if (unlikely(it == it_end))
6729             {
6730                 boost::format formatter("Sending possible data is not existed. thread id : %d.");
6731                 formatter % boost::this_thread::get_id();
6732                 putLogError(100109, formatter.str(), __FILE__, __LINE__ );
6733                 throw -1;
6734             }
6735
6736             //sending possible data is exist
6737             if (it->send_possible_size > 0)
6738             {
6739                 //status remain SEND_OK
6740                 it->status = SEND_OK;
6741                 //offset recalc
6742                 it->send_offset += it->send_end_size;
6743                 //send_end_size recalc
6744                 it->send_end_size = 0;
6745             }
6746             //sending possible data is not exist
6747             else
6748             {
6749                 //can recive from clent continue
6750                 if (it->send_rest_size > 0)
6751                 {
6752                     //change status from SEND_OK to SEND_CONTINUE
6753                     it->status = SEND_CONTINUE;
6754                 }
6755                 //can not recive from clent continue
6756                 else
6757                 {
6758                     //change status from SEND_OK to SEND_END
6759                     it->status = SEND_END;
6760                 }
6761             }
6762
6763             it = recv_data.send_status_list.begin();
6764             it = find_if(it, it_end, data_send_ok());
6765             //send_ok item is exist
6766             if (it != it_end)
6767             {
6768                 status = CLIENT_CONNECTION_CHECK;
6769             }
6770             //send_ok item is not exist
6771             else
6772             {
6773                 //end flag is on
6774                 if (session_data->end_flag == END_FLAG_ON)
6775                 {
6776                     status = CLIENT_DISCONNECT;
6777                 }
6778                 //end flag is off
6779                 else
6780                 {
6781                     //sorry flag is on
6782                     if (session_data->sorry_flag == SORRY_FLAG_ON)
6783                     {
6784                         status = SORRYSERVER_RECV;
6785                     }
6786                     //sorry flag is off
6787                     else
6788                     {
6789                         status = REALSERVER_RECV;
6790                     }
6791                 }
6792             }
6793         }
6794         catch (int e)
6795         {
6796             /*-------- DEBUG LOG --------*/
6797             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6798             {
6799                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6800                                         "handle_client_send() : catch exception e = %d. thread id : %d.");
6801                 formatter % e % boost::this_thread::get_id();
6802                 putLogDebug(100223, formatter.str(), __FILE__, __LINE__ );
6803             }
6804             /*------DEBUG LOG END------*/
6805             status = FINALIZE;
6806         }
6807         catch (const std::exception& ex)
6808         {
6809             std::cerr << "protocol_module_sessionless::handle_client_send() : exception : error = " << ex.what() << "." << std::endl;
6810             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6811                                     "handle_client_send() : exception : error = %s. thread id : %d.");
6812             formatter % ex.what() % boost::this_thread::get_id();
6813             putLogError(100110, formatter.str(), __FILE__, __LINE__ );
6814             status = FINALIZE;
6815         }
6816         catch (...)
6817         {
6818             std::cerr << "protocol_module_sessionless::handle_client_send() : Unknown exception." << std::endl;
6819             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6820                                     "handle_client_send() : Unknown exception. thread id : %d.");
6821             formatter % boost::this_thread::get_id();
6822             putLogError(100111, formatter.str(), __FILE__, __LINE__ );
6823             status = FINALIZE;
6824         }
6825
6826         /*-------- DEBUG LOG --------*/
6827         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6828         {
6829             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6830                                     "handle_client_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6831             formatter % status % boost::this_thread::get_id();
6832             putLogDebug(100224, formatter.str(), __FILE__, __LINE__ );
6833         }
6834         /*------DEBUG LOG END------*/
6835
6836         return status;
6837     }
6838
6839     //! call from client disconnect event. use upstream thread and downstream thread.
6840     //! @param[in]    upstream and downstream thread id( check! one thread one event! )
6841     //! @return     session use EVENT mode
6842     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_disconnect(
6843         const boost::thread::id thread_id)
6844     {
6845         /*-------- DEBUG LOG --------*/
6846         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6847         {
6848             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6849                                     "handle_client_disconnect(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6850             formatter % FINALIZE % boost::this_thread::get_id();
6851             putLogDebug(100225, formatter.str(), __FILE__, __LINE__ );
6852         }
6853         /*------DEBUG LOG END------*/
6854         return FINALIZE;
6855     }
6856
6857     //! call from sorry mode event. use upstream thread and downstream thread
6858     //! @param[in]    upstream and downstream thread id( check! one thread one event and first time call pattern )
6859     //! @return     session use EVENT mode
6860     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorry_enable(
6861         const boost::thread::id thread_id)
6862     {
6863         /*-------- DEBUG LOG --------*/
6864         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6865         {
6866             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6867                                     "handle_sorry_enable(const boost::thread::id thread_id) : "
6868                                     "thread_id = %d.");
6869             formatter % thread_id;
6870             putLogDebug(100226, formatter.str(), __FILE__, __LINE__ );
6871         }
6872         /*------DEBUG LOG END------*/
6873
6874         EVENT_TAG status = FINALIZE;
6875         boost::asio::ip::tcp::endpoint endpoint;
6876         bool send_possible = false;
6877         bool send_continue = false;
6878         bool send_disable = false;
6879         thread_data_ptr session_data;
6880         session_thread_data_map_it session_thread_it;
6881         recive_data_map_it recive_data_it;
6882
6883         try
6884         {
6885             {
6886                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
6887                 //check thread_id
6888                 session_thread_it = session_thread_data_map.find(thread_id);
6889                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
6890                 {
6891                     boost::format formatter("Invalid thread id. thread id : %d.");
6892                     formatter % boost::this_thread::get_id();
6893                     putLogError(100112, formatter.str(), __FILE__, __LINE__ );
6894                     throw -1;
6895                 }
6896
6897                 session_data = session_thread_it->second;
6898             }
6899             //check endpoint
6900             endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
6901                        : session_data->target_endpoint;
6902             recive_data_it = session_data->recive_data_map.find(endpoint);
6903             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
6904             {
6905                 //must be down thread
6906                 if (unlikely(session_data->thread_division == THREAD_DIVISION_UP_STREAM))
6907                 {
6908                     boost::format formatter("Invalid endpoint. thread id : %d.");
6909                     formatter % boost::this_thread::get_id();
6910                     putLogError(100113, formatter.str(), __FILE__, __LINE__ );
6911                     throw -1;
6912                 }
6913                 session_data->sorry_flag = SORRY_FLAG_ON;
6914                 /*-------- DEBUG LOG --------*/
6915                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6916                 {
6917                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6918                                             "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6919                     formatter % boost::this_thread::get_id();
6920                     putLogDebug(100227, formatter.str(), __FILE__, __LINE__ );
6921                 }
6922                 /*------DEBUG LOG END------*/
6923                 status = SORRYSERVER_RECV;
6924             }
6925             else
6926             {
6927                 recive_data& recv_data = recive_data_it->second;
6928
6929                 //get this thread sending possible data
6930                 send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6931                                             data_send_possible());
6932                 if (it != recv_data.send_status_list.end())
6933                 {
6934                     send_possible = true;
6935                 }
6936
6937                 it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_continue());
6938                 if (it != recv_data.send_status_list.end())
6939                 {
6940                     send_continue = true;
6941                 }
6942
6943                 it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_disable());
6944                 if (it != recv_data.send_status_list.end())
6945                 {
6946                     send_disable = true;
6947                 }
6948
6949                 //up thread
6950                 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM)
6951                 {
6952                     //accept_end_flag is off
6953                     if (session_data->accept_end_flag == ACCEPT_END_FLAG_OFF)
6954                     {
6955                         //set sorry flag on
6956                         session_data->sorry_flag = SORRY_FLAG_ON;
6957                         /*-------- DEBUG LOG --------*/
6958                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6959                         {
6960                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6961                                                     "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6962                             formatter % boost::this_thread::get_id();
6963                             putLogDebug(100228, formatter.str(), __FILE__, __LINE__ );
6964                         }
6965                         /*------DEBUG LOG END------*/
6966                         status = ACCEPT;
6967                     }
6968                     //accept_end_flag is on
6969                     else
6970                     {
6971                         //set sorry flag on
6972                         if (session_data->sorry_flag == SORRY_FLAG_ON)
6973                         {
6974                             if (send_possible)
6975                             {
6976                                 status = SORRYSERVER_CONNECT;
6977                             }
6978                             else
6979                             {
6980                                 status = SORRYSERVER_SELECT;
6981                             }
6982
6983                         }
6984                         //set sorry flag off
6985                         else
6986                         {
6987                             //the data that can be sent continue is exist
6988                             if (send_continue)
6989                             {
6990                                 //set end flag on
6991                                 session_data->end_flag = END_FLAG_ON;
6992                                 /*-------- DEBUG LOG --------*/
6993                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6994                                 {
6995                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6996                                                             "handle_sorry_enable() : END_FLAG_ON. thread id : %d.");
6997                                     formatter % boost::this_thread::get_id();
6998                                     putLogDebug(100229, formatter.str(), __FILE__, __LINE__ );
6999                                 }
7000                                 /*------DEBUG LOG END------*/
7001                                 status = REALSERVER_DISCONNECT;
7002                             }
7003                             //the data that can be sent continue is not exist
7004                             else
7005                             {
7006                                 //set sorryserver_switch_flag on
7007                                 session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_ON;
7008                                 /*-------- DEBUG LOG --------*/
7009                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7010                                 {
7011                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7012                                                             "handle_sorry_enable() : SORRYSERVER_SWITCH_FLAG_ON. thread id : %d.");
7013                                     formatter % boost::this_thread::get_id();
7014                                     putLogDebug(100230, formatter.str(), __FILE__, __LINE__ );
7015                                 }
7016                                 /*------DEBUG LOG END------*/
7017                                 //set sorry_flag on
7018                                 session_data->sorry_flag = SORRY_FLAG_ON;
7019                                 /*-------- DEBUG LOG --------*/
7020                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7021                                 {
7022                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7023                                                             "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
7024                                     formatter % boost::this_thread::get_id();
7025                                     putLogDebug(100231, formatter.str(), __FILE__, __LINE__ );
7026                                 }
7027                                 /*------DEBUG LOG END------*/
7028                                 status = REALSERVER_DISCONNECT;
7029                             }
7030                         }
7031                     }
7032                 }
7033                 //down thread
7034                 else
7035                 {
7036                     //sorry_flag is on
7037                     if (session_data->sorry_flag == SORRY_FLAG_ON)
7038                     {
7039                         //sending possible data is exist
7040                         if (send_possible)
7041                         {
7042                             status = CLIENT_CONNECTION_CHECK;
7043                         }
7044                         //sending possible data is not exist
7045                         else
7046                         {
7047                             status = SORRYSERVER_RECV;
7048                         }
7049                     }
7050                     //sorry_flag is off
7051                     else
7052                     {
7053                         //set sorry_flag on
7054                         session_data->sorry_flag = SORRY_FLAG_ON;
7055                         /*-------- DEBUG LOG --------*/
7056                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7057                         {
7058                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7059                                                     "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
7060                             formatter % boost::this_thread::get_id();
7061                             putLogDebug(100232, formatter.str(), __FILE__, __LINE__ );
7062                         }
7063                         /*------DEBUG LOG END------*/
7064                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_ON;
7065                         /*-------- DEBUG LOG --------*/
7066                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7067                         {
7068                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7069                                                     "handle_sorry_enable() : SORRYSERVER_SWITCH_FLAG_ON. thread id : %d.");
7070                             formatter % boost::this_thread::get_id();
7071                             putLogDebug(100233, formatter.str(), __FILE__, __LINE__ );
7072                         }
7073                         /*------DEBUG LOG END------*/
7074
7075                         //sending ng data is exist or send_rest_size > 0
7076                         if (send_disable)
7077                         {
7078                             //set end flag on
7079                             session_data->end_flag = END_FLAG_ON;
7080                             /*-------- DEBUG LOG --------*/
7081                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7082                             {
7083                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7084                                                         "handle_sorry_enable() : END_FLAG_ON. thread id : %d.");
7085                                 formatter % boost::this_thread::get_id();
7086                                 putLogDebug(100234, formatter.str(), __FILE__, __LINE__ );
7087                             }
7088                             /*------DEBUG LOG END------*/
7089                             status = REALSERVER_DISCONNECT;
7090                         }
7091                         //
7092                         else
7093                         {
7094                             //sending possible data is exist
7095                             if (send_possible)
7096                             {
7097                                 status = CLIENT_CONNECTION_CHECK;
7098                             }
7099                             //sending possible data is not exist
7100                             else
7101                             {
7102                                 status = SORRYSERVER_RECV;
7103                             }
7104                         }
7105                     }
7106                 }
7107             }
7108         }
7109         catch (int e)
7110         {
7111             /*-------- DEBUG LOG --------*/
7112             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7113             {
7114                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7115                                         "handle_sorry_enable() : catch exception e = %d. thread id : %d.");
7116                 formatter % e % boost::this_thread::get_id();
7117                 putLogDebug(100235, formatter.str(), __FILE__, __LINE__ );
7118             }
7119             /*------DEBUG LOG END------*/
7120             status = FINALIZE;
7121         }
7122         catch (const std::exception& ex)
7123         {
7124             std::cerr << "protocol_module_sessionless::handle_sorry_enable() : exception : error = " << ex.what() << "." << std::endl;
7125             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7126                                     "handle_sorry_enable() : exception : error = %s. thread id : %d.");
7127             formatter % ex.what() % boost::this_thread::get_id();
7128             putLogError(100114, formatter.str(), __FILE__, __LINE__ );
7129             status = FINALIZE;
7130         }
7131         catch (...)
7132         {
7133             std::cerr << "protocol_module_sessionless::handle_sorry_enable() : Unknown exception." << std::endl;
7134             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7135                                     "handle_sorry_enable() : Unknown exception. thread id : %d.");
7136             formatter % boost::this_thread::get_id();
7137             putLogError(100115, formatter.str(), __FILE__, __LINE__ );
7138             status = FINALIZE;
7139         }
7140
7141         /*-------- DEBUG LOG --------*/
7142         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7143         {
7144             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7145                                     "handle_sorry_enable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
7146             formatter % status % boost::this_thread::get_id();
7147             putLogDebug(100236, formatter.str(), __FILE__, __LINE__ );
7148         }
7149         /*------DEBUG LOG END------*/
7150
7151         return status;
7152     }
7153
7154     //! call from sorry mode disable. use upstream thread and downstream thread.
7155     //! @param[in]    upstream and downstream thread id( check! one thread one event )
7156     //! @return     session use EVENT mode
7157     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorry_disable(
7158         const boost::thread::id thread_id)
7159     {
7160         /*-------- DEBUG LOG --------*/
7161         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7162         {
7163             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7164                                     "handle_sorry_disable(const boost::thread::id thread_id) : "
7165                                     "thread_id = %d.");
7166             formatter % thread_id;
7167             putLogDebug(100237, formatter.str(), __FILE__, __LINE__ );
7168         }
7169         /*------DEBUG LOG END------*/
7170         EVENT_TAG status = FINALIZE;
7171         boost::asio::ip::tcp::endpoint endpoint;
7172         bool send_possible = false;
7173         bool send_disable = false;
7174         bool send_continue = false;
7175         thread_data_ptr session_data;
7176
7177         try
7178         {
7179             {
7180                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
7181                 //check thread_id
7182                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
7183                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
7184                 {
7185                     boost::format formatter("Invalid thread id. thread id : %d.");
7186                     formatter % boost::this_thread::get_id();
7187                     putLogError(100116, formatter.str(), __FILE__, __LINE__ );
7188                     throw -1;
7189                 }
7190                 //check pointer
7191                 session_data = session_thread_it->second;
7192             }
7193             //check endpoint
7194             endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
7195                        : session_data->target_endpoint;
7196             recive_data_map_it recive_data_it = session_data->recive_data_map.find(endpoint);
7197             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
7198             {
7199                 //must be down thread
7200                 if (unlikely(session_data->thread_division == THREAD_DIVISION_UP_STREAM))
7201                 {
7202                     boost::format formatter("Invalid endpoint. thread id : %d.");
7203                     formatter % boost::this_thread::get_id();
7204                     putLogError(100117, formatter.str(), __FILE__, __LINE__ );
7205                     throw -1;
7206                 }
7207
7208                 session_data->sorry_flag = SORRY_FLAG_OFF;
7209                 /*-------- DEBUG LOG --------*/
7210                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7211                 {
7212                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7213                                             "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
7214                     formatter % boost::this_thread::get_id();
7215                     putLogDebug(100238, formatter.str(), __FILE__, __LINE__ );
7216                 }
7217                 /*------DEBUG LOG END------*/
7218                 status = REALSERVER_RECV;
7219             }
7220             else
7221             {
7222                 recive_data& recv_data = recive_data_it->second;
7223
7224                 //get this thread sending possible data
7225                 send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
7226                                             data_send_possible());
7227                 if (it != recv_data.send_status_list.end())
7228                 {
7229                     send_possible = true;
7230                 }
7231
7232                 //sending ng data is exist or send_rest_size > 0
7233                 it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_disable());
7234                 if (it != recv_data.send_status_list.end())
7235                 {
7236                     send_disable = true;
7237                 }
7238
7239                 //the data that can be sent continue is exist
7240                 it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_continue());
7241                 if (it != recv_data.send_status_list.end())
7242                 {
7243                     send_continue = true;
7244                 }
7245
7246                 //up thread
7247                 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM)
7248                 {
7249                     //accept_end_flag is off
7250                     if (session_data->accept_end_flag == ACCEPT_END_FLAG_OFF)
7251                     {
7252                         //set sorry flag off
7253                         session_data->sorry_flag = SORRY_FLAG_OFF;
7254                         /*-------- DEBUG LOG --------*/
7255                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7256                         {
7257                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7258                                                     "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
7259                             formatter % boost::this_thread::get_id();
7260                             putLogDebug(100239, formatter.str(), __FILE__, __LINE__ );
7261                         }
7262                         /*------DEBUG LOG END------*/
7263
7264                         status = ACCEPT;
7265                     }
7266                     //accept_end_flag is on
7267                     else
7268                     {
7269                         //sorry flag is on
7270                         if (session_data->sorry_flag == SORRY_FLAG_ON)
7271                         {
7272                             //the data that can be sent continue is exist
7273                             if (send_continue)
7274                             {
7275                                 //set end flag on
7276                                 session_data->end_flag = END_FLAG_ON;
7277                                 /*-------- DEBUG LOG --------*/
7278                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7279                                 {
7280                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7281                                                             "handle_sorry_disable() : END_FLAG_ON. thread id : %d.");
7282                                     formatter % boost::this_thread::get_id();
7283                                     putLogDebug(100240, formatter.str(), __FILE__, __LINE__ );
7284                                 }
7285                                 /*------DEBUG LOG END------*/
7286                                 status = SORRYSERVER_DISCONNECT;
7287                             }
7288                             //the data that can be sent continue is not exist
7289                             else
7290                             {
7291                                 //set realserver_switch_flag on
7292                                 session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_ON;
7293                                 /*-------- DEBUG LOG --------*/
7294                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7295                                 {
7296                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7297                                                             "handle_sorry_disable() : REALSERVER_SWITCH_FLAG_ON. thread id : %d.");
7298                                     formatter % boost::this_thread::get_id();
7299                                     putLogDebug(100241, formatter.str(), __FILE__, __LINE__ );
7300                                 }
7301                                 /*------DEBUG LOG END------*/
7302                                 //set sorry_flag off
7303                                 session_data->sorry_flag = SORRY_FLAG_OFF;
7304                                 /*-------- DEBUG LOG --------*/
7305                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
7306                                 {
7307                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7308                                                             "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
7309                                     formatter % boost::this_thread::get_id();
7310                                     putLogDebug(100242, formatter.str(), __FILE__, __LINE__ );
7311                                 }
7312                                 /*------DEBUG LOG END------*/
7313                                 status = SORRYSERVER_DISCONNECT;
7314                             }
7315                         }
7316                         //sorry flag is off
7317                         else
7318                         {
7319                             if (send_possible)
7320                             {
7321                                 status = REALSERVER_CONNECT;
7322                             }
7323                             else
7324                             {
7325                                 status = REALSERVER_SELECT;
7326                             }
7327                         }
7328                     }
7329                 }
7330                 //down thread
7331                 else
7332                 {
7333                     //sorry_flag is on
7334                     if (session_data->sorry_flag == SORRY_FLAG_ON)
7335                     {
7336                         //set sorry_flag off
7337                         session_data->sorry_flag = SORRY_FLAG_OFF;
7338                         /*-------- DEBUG LOG --------*/
7339                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7340                         {
7341                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7342                                                     "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
7343                             formatter % boost::this_thread::get_id();
7344                             putLogDebug(100243, formatter.str(), __FILE__, __LINE__ );
7345                         }
7346                         /*------DEBUG LOG END------*/
7347                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_ON;
7348                         /*-------- DEBUG LOG --------*/
7349                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7350                         {
7351                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7352                                                     "handle_sorry_disable() : REALSERVER_SWITCH_FLAG_ON. thread id : %d.");
7353                             formatter % boost::this_thread::get_id();
7354                             putLogDebug(100244, formatter.str(), __FILE__, __LINE__ );
7355                         }
7356                         /*------DEBUG LOG END------*/
7357                         //sending ng data is exist or send_rest_size > 0
7358                         if (send_disable)
7359                         {
7360                             //set end flag on
7361                             session_data->end_flag = END_FLAG_ON;
7362                             /*-------- DEBUG LOG --------*/
7363                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7364                             {
7365                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7366                                                         "handle_sorry_disable() : END_FLAG_ON. thread id : %d.");
7367                                 formatter % boost::this_thread::get_id();
7368                                 putLogDebug(100245, formatter.str(), __FILE__, __LINE__ );
7369                             }
7370                             /*------DEBUG LOG END------*/
7371                             status = SORRYSERVER_DISCONNECT;
7372                         }
7373                         //
7374                         else
7375                         {
7376                             //sending possible data is exist
7377                             if (send_possible)
7378                             {
7379                                 status = CLIENT_CONNECTION_CHECK;
7380                             }
7381                             //sending possible data is not exist
7382                             else
7383                             {
7384                                 status = REALSERVER_RECV;
7385                             }
7386                         }
7387                     }
7388                     //sorry_flag is off
7389                     else
7390                     {
7391                         //sending possible data is exist
7392                         if (send_possible)
7393                         {
7394                             status = CLIENT_CONNECTION_CHECK;
7395                         }
7396                         //sending possible data is not exist
7397                         else
7398                         {
7399                             status = REALSERVER_RECV;
7400                         }
7401                     }
7402                 }
7403             }
7404
7405
7406         }
7407         catch (int e)
7408         {
7409             /*-------- DEBUG LOG --------*/
7410             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7411             {
7412                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7413                                         "handle_sorry_disable() : catch exception e = %d. thread id : %d.");
7414                 formatter % e % boost::this_thread::get_id();
7415                 putLogDebug(100246, formatter.str(), __FILE__, __LINE__ );
7416             }
7417             /*------DEBUG LOG END------*/
7418             status = FINALIZE;
7419         }
7420         catch (std::exception& ex)
7421         {
7422             std::cerr << "protocol_module_sessionless::handle_sorry_disable() : exception : error = " << ex.what() << "." << std::endl;
7423             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7424                                     "handle_sorry_disable() : exception : error = %s. thread id : %d.");
7425             formatter % ex.what() % boost::this_thread::get_id();
7426             putLogError(100118, formatter.str(), __FILE__, __LINE__ );
7427             status = FINALIZE;
7428         }
7429         catch (...)
7430         {
7431             std::cerr << "protocol_module_sessionless::handle_sorry_disable() : Unknown exception." << std::endl;
7432             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7433                                     "handle_sorry_disable() : Unknown exception. thread id : %d.");
7434             formatter % boost::this_thread::get_id();
7435             putLogError(100119, formatter.str(), __FILE__, __LINE__ );
7436             status = FINALIZE;
7437         }
7438
7439         /*-------- DEBUG LOG --------*/
7440         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7441         {
7442             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7443                                     "handle_sorry_disable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
7444             formatter % status % boost::this_thread::get_id();
7445             putLogDebug(100247, formatter.str(), __FILE__, __LINE__ );
7446         }
7447         /*------DEBUG LOG END------*/
7448
7449         return status;
7450     }
7451
7452     //! call from realserver disconnect. use upstream thread and downstream thread
7453     //! @param[in]    upstream and downstream thread id( check! one thread one event )
7454     //! @param[in]    disconnected realserver endpoint.
7455     //! @return     session use EVENT mode
7456     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_disconnect(
7457         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint)
7458     {
7459         /*-------- DEBUG LOG --------*/
7460         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7461         {
7462             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7463                                     "handle_realserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint) : "
7464                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
7465             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
7466             putLogDebug(100248, formatter.str(), __FILE__, __LINE__ );
7467         }
7468         /*------DEBUG LOG END------*/
7469         EVENT_TAG status = FINALIZE;
7470         bool possible_flag = false;
7471         thread_data_ptr session_data;
7472         boost::asio::ip::tcp::endpoint endpoint;
7473
7474         try
7475         {
7476             {
7477                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
7478
7479                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
7480                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
7481                 {
7482                     boost::format formatter("Invalid thread id. thread id : %d.");
7483                     formatter % boost::this_thread::get_id();
7484                     putLogError(100120, formatter.str(), __FILE__, __LINE__ );
7485                     throw -1;
7486                 }
7487
7488                 session_data = session_thread_it->second;
7489             }
7490
7491             endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
7492                        : session_data->target_endpoint;
7493             recive_data_map_it recive_data_it = session_data->recive_data_map.find(endpoint);
7494             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
7495             {
7496                 boost::format formatter("Invalid endpoint. thread id : %d.");
7497                 formatter % boost::this_thread::get_id();
7498                 putLogError(100121, formatter.str(), __FILE__, __LINE__ );
7499                 throw -1;
7500             }
7501
7502             recive_data& recv_data = recive_data_it->second;
7503
7504             //the data that can be sent possible is exist
7505             send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
7506                                         data_send_possible());
7507             if (it != recv_data.send_status_list.end())
7508             {
7509                 possible_flag = true;
7510             }
7511
7512             //up thread
7513             if (session_data->thread_division == THREAD_DIVISION_UP_STREAM)
7514             {
7515                 //end flag is on
7516                 if (session_data->end_flag == END_FLAG_ON)
7517                 {
7518                     status = CLIENT_RECV;
7519                 }
7520                 //end flag is off
7521                 else
7522                 {
7523                     //sorryserver_switch_flag is on
7524                     if (session_data->sorryserver_switch_flag == SORRYSERVER_SWITCH_FLAG_ON)
7525                     {
7526                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
7527                         /*-------- DEBUG LOG --------*/
7528                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7529                         {
7530                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7531                                                     "handle_realserver_disconnect() : SORRYSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7532                             formatter % boost::this_thread::get_id();
7533                             putLogDebug(100249, formatter.str(), __FILE__, __LINE__ );
7534                         }
7535                         /*------DEBUG LOG END------*/
7536
7537                         status = SORRYSERVER_SELECT;
7538
7539                     }
7540                     //sorryserver_switch_flag is off
7541                     else
7542                     {
7543                         //set end flag on
7544                         session_data->end_flag = END_FLAG_ON;
7545                         /*-------- DEBUG LOG --------*/
7546                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7547                         {
7548                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7549                                                     "handle_realserver_disconnect() : END_FLAG_ON. thread id : %d.");
7550                             formatter % boost::this_thread::get_id();
7551                             putLogDebug(100250, formatter.str(), __FILE__, __LINE__ );
7552                         }
7553                         /*------DEBUG LOG END------*/
7554                         status = CLIENT_RECV;
7555                     }
7556                 }
7557             }
7558             //down thread
7559             else
7560             {
7561                 if (session_data->end_flag == END_FLAG_ON)
7562                 {
7563                     status = CLIENT_DISCONNECT;
7564                 }
7565                 else
7566                 {
7567                     if (session_data->sorryserver_switch_flag == SORRYSERVER_SWITCH_FLAG_ON)
7568                     {
7569                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
7570                         /*-------- DEBUG LOG --------*/
7571                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7572                         {
7573                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7574                                                     "handle_realserver_disconnect() : SORRYSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7575                             formatter % boost::this_thread::get_id();
7576                             putLogDebug(100251, formatter.str(), __FILE__, __LINE__ );
7577                         }
7578                         /*------DEBUG LOG END------*/
7579                         status = SORRYSERVER_RECV;
7580                     }
7581                     else
7582                     {
7583                         //set end flag on
7584                         session_data->end_flag = END_FLAG_ON;
7585                         /*-------- DEBUG LOG --------*/
7586                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7587                         {
7588                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7589                                                     "handle_realserver_disconnect() : END_FLAG_ON. thread id : %d.");
7590                             formatter % boost::this_thread::get_id();
7591                             putLogDebug(100252, formatter.str(), __FILE__, __LINE__ );
7592                         }
7593                         /*------DEBUG LOG END------*/
7594                         status = CLIENT_DISCONNECT;
7595                     }
7596                 }
7597
7598                 //the data that can be sent possible is exist
7599                 if (possible_flag)
7600                 {
7601                     status = CLIENT_CONNECTION_CHECK;
7602                 }
7603             }
7604         }
7605         catch (int e)
7606         {
7607             /*-------- DEBUG LOG --------*/
7608             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7609             {
7610                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7611                                         "handle_realserver_disconnect() : catch exception e = %d. thread id : %d.");
7612                 formatter % e % boost::this_thread::get_id();
7613                 putLogDebug(100253, formatter.str(), __FILE__, __LINE__ );
7614             }
7615             /*------DEBUG LOG END------*/
7616             status = FINALIZE;
7617         }
7618         catch (std::exception& ex)
7619         {
7620             std::cerr << "protocol_module_sessionless::handle_realserver_disconnect() : exception: error = " << ex.what() << "." << std::endl;
7621             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7622                                     "handle_realserver_disconnect() : exception : error = %s. thread id : %d.");
7623             formatter % ex.what() % boost::this_thread::get_id();
7624             putLogError(100122, formatter.str(), __FILE__, __LINE__ );
7625             status = FINALIZE;
7626         }
7627         catch (...)
7628         {
7629             std::cerr << "protocol_module_sessionless::handle_realserver_disconnect() : Unknown exception." << std::endl;
7630             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7631                                     "handle_realserver_disconnect() : Unknown exception. thread id : %d.");
7632             formatter % boost::this_thread::get_id();
7633             putLogError(100123, formatter.str(), __FILE__, __LINE__ );
7634             status = FINALIZE;
7635         }
7636
7637         /*-------- DEBUG LOG --------*/
7638         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7639         {
7640             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7641                                     "handle_realserver_disconnect(const boost::thread::id thread_id, "
7642                                     "const boost::asio::ip::tcp::endpoint & rs_endpoint) : return_value = %d. thread id : %d.");
7643             formatter % status % boost::this_thread::get_id();
7644             putLogDebug(100254, formatter.str(), __FILE__, __LINE__ );
7645         }
7646         /*------DEBUG LOG END------*/
7647
7648         return status;
7649     }
7650
7651     //! call from sorry server disconnect. use upstraem thread and downstream thread
7652     //! @param[in]    upstream and downstream thread id( check! one thread one event )
7653     //! @param[in]    disconnect sorryserver endpoint
7654     //! @return        session use EVENT mode
7655     //! @return        session use EVENT mode
7656     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_disconnect(
7657         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint)
7658     {
7659         /*-------- DEBUG LOG --------*/
7660         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7661         {
7662             boost::format
7663             formatter(
7664                 "in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7665                 "handle_sorryserver_disconnect(const boost::thread::id thread_id, "
7666                 "const boost::asio::ip::tcp::endpoint & sorry_endpoint) : "
7667                 "thread_id = %d, sorry_endpoint = [%s]:%d.");
7668             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port() ;
7669             putLogDebug(100255, formatter.str(), __FILE__, __LINE__ );
7670         }
7671         /*------DEBUG LOG END------*/
7672         EVENT_TAG status = FINALIZE;
7673         bool possible_flag = false;
7674         thread_data_ptr session_data;
7675         boost::asio::ip::tcp::endpoint endpoint;
7676
7677         try
7678         {
7679             {
7680                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
7681
7682                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
7683                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
7684                 {
7685                     boost::format formatter("Invalid thread id. thread id : %d.");
7686                     formatter % boost::this_thread::get_id();
7687                     putLogError(100124, formatter.str(), __FILE__, __LINE__ );
7688                     throw -1;
7689                 }
7690
7691                 session_data = session_thread_it->second;
7692             }
7693
7694             endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
7695                        : session_data->target_endpoint;
7696             recive_data_map_it recive_data_it = session_data->recive_data_map.find(endpoint);
7697             if (unlikely(recive_data_it == session_data->recive_data_map.end()))
7698             {
7699                 boost::format formatter("Invalid endpoint. thread id : %d.");
7700                 formatter % boost::this_thread::get_id();
7701                 putLogError(100125, formatter.str(), __FILE__, __LINE__ );
7702                 throw -1;
7703             }
7704
7705             recive_data& recv_data = recive_data_it->second;
7706
7707             //the data that can be sent possible is exist
7708             send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
7709                                         data_send_possible());
7710             if (it != recv_data.send_status_list.end())
7711             {
7712                 possible_flag = true;
7713             }
7714
7715             //up thread
7716             if (session_data->thread_division == THREAD_DIVISION_UP_STREAM)
7717             {
7718                 //end flag is on
7719                 if (session_data->end_flag == END_FLAG_ON)
7720                 {
7721                     status = CLIENT_RECV;
7722                 }
7723                 //end flag is off
7724                 else
7725                 {
7726                     //realserver_switch_flag is on
7727                     if (session_data->realserver_switch_flag == REALSERVER_SWITCH_FLAG_ON)
7728                     {
7729                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
7730                         /*-------- DEBUG LOG --------*/
7731                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7732                         {
7733                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7734                                                     "handle_sorryserver_disconnect() : REALSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7735                             formatter % boost::this_thread::get_id();
7736                             putLogDebug(100256, formatter.str(), __FILE__, __LINE__ );
7737                         }
7738                         /*------DEBUG LOG END------*/
7739                         status = REALSERVER_SELECT;
7740                     }
7741                     //realserver_switch_flag is off
7742                     else
7743                     {
7744                         //set end flag on
7745                         session_data->end_flag = END_FLAG_ON;
7746                         /*-------- DEBUG LOG --------*/
7747                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7748                         {
7749                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7750                                                     "handle_sorryserver_disconnect() : END_FLAG_ON. thread id : %d.");
7751                             formatter % boost::this_thread::get_id();
7752                             putLogDebug(100257, formatter.str(), __FILE__, __LINE__ );
7753                         }
7754                         /*------DEBUG LOG END------*/
7755                         status = CLIENT_RECV;
7756                     }
7757                 }
7758             }
7759             //down thread
7760             else
7761             {
7762                 if (session_data->end_flag == END_FLAG_ON)
7763                 {
7764                     status = CLIENT_DISCONNECT;
7765                 }
7766                 else
7767                 {
7768                     if (session_data->realserver_switch_flag == REALSERVER_SWITCH_FLAG_ON)
7769                     {
7770                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
7771                         /*-------- DEBUG LOG --------*/
7772                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7773                         {
7774                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7775                                                     "handle_sorryserver_disconnect() : REALSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7776                             formatter % boost::this_thread::get_id();
7777                             putLogDebug(100258, formatter.str(), __FILE__, __LINE__ );
7778                         }
7779                         /*------DEBUG LOG END------*/
7780                         status = REALSERVER_RECV;
7781                     }
7782                     else
7783                     {
7784                         session_data->end_flag = END_FLAG_ON;
7785                         /*-------- DEBUG LOG --------*/
7786                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7787                         {
7788                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7789                                                     "handle_sorryserver_disconnect() : END_FLAG_ON. thread id : %d.");
7790                             formatter % boost::this_thread::get_id();
7791                             putLogDebug(100259, formatter.str(), __FILE__, __LINE__ );
7792                         }
7793                         /*------DEBUG LOG END------*/
7794                         status = CLIENT_DISCONNECT;
7795                     }
7796                 }
7797
7798                 //the data that can be sent possible is exist
7799                 if (possible_flag)
7800                 {
7801                     status = CLIENT_CONNECTION_CHECK;
7802                 }
7803             }
7804         }
7805         catch (int e)
7806         {
7807             /*-------- DEBUG LOG --------*/
7808             if (unlikely(LOG_LV_DEBUG == getloglevel()))
7809             {
7810                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7811                                         "handle_sorryserver_disconnect() : catch exception e = %d. thread id : %d.");
7812                 formatter % e % boost::this_thread::get_id();
7813                 putLogDebug(100260, formatter.str(), __FILE__, __LINE__ );
7814             }
7815             /*------DEBUG LOG END------*/
7816             status = FINALIZE;
7817         }
7818         catch (const std::exception& ex)
7819         {
7820             std::cerr << "protocol_module_sessionless::handle_sorryserver_disconnect() : exception : error = " << ex.what() << "." << std::endl;
7821             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7822                                     "handle_sorryserver_disconnect() : exception : error = %s. thread id : %d.");
7823             formatter % ex.what() % boost::this_thread::get_id();
7824             putLogError(100126, formatter.str(), __FILE__, __LINE__ );
7825             status = FINALIZE;
7826         }
7827         catch (...)
7828         {
7829             std::cerr << "protocol_module_sessionless::handle_sorryserver_disconnect() : Unknown exception." << std::endl;
7830             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7831                                     "handle_sorryserver_disconnect() : Unknown exception. thread id : %d.");
7832             formatter % boost::this_thread::get_id();
7833             putLogError(100127, formatter.str(), __FILE__, __LINE__ );
7834             status = FINALIZE;
7835         }
7836
7837         /*-------- DEBUG LOG --------*/
7838         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7839         {
7840             boost::format
7841             formatter(
7842                 "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7843                 "handle_sorryserver_disconnect(const boost::thread::id thread_id, "
7844                 "const boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d. thread id : %d.");
7845             formatter % status % boost::this_thread::get_id();
7846             putLogDebug(100261, formatter.str(), __FILE__, __LINE__ );
7847         }
7848         /*------DEBUG LOG END------*/
7849
7850         return status;
7851     }
7852
7853     //! call from realserver disconnect. use upstream thread and downstream thread.
7854     //! @param[in]    upstream and downstream thread id( check! one thread one event )
7855     //! @param[in]    disconnect realserver endpoint
7856     //! @return        session use EVENT mode.
7857     protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_close(
7858         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint & rs_endpoint)
7859     {
7860         /*-------- DEBUG LOG --------*/
7861         if (unlikely(LOG_LV_DEBUG == getloglevel()))
7862         {
7863             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7864                                     "handle_realserver_close(const boost::thread::id thread_id, "
7865                                     "const boost::asio::ip::udp::endpoint & rs_endpoint) : "
7866                                     "return_value = %d. thread id : %d.");
7867             formatter % STOP % boost::this_thread::get_id();
7868             putLogDebug(100262, formatter.str(), __FILE__, __LINE__ );
7869         }
7870         /*------DEBUG LOG END------*/
7871         return STOP;
7872     }
7873
7874 }
7875
7876 extern "C" l7vs::protocol_module_base*
7877     create_module()
7878 {
7879     return dynamic_cast<l7vs::protocol_module_base*>(new l7vs::protocol_module_sessionless());
7880 }
7881
7882 extern "C" void
7883     destroy_module( l7vs::protocol_module_base* in )
7884 {
7885     delete in;
7886 }