OSDN Git Service

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