OSDN Git Service

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