OSDN Git Service

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