Changeset 538

Show
Ignore:
Timestamp:
02/22/10 22:37:05 (5 months ago)
Author:
gaul@…
Message:
  • NASTY async socket bug resolved
Location:
branches/cpp-ode/src
Files:
1 added
7 modified

Legend:

Unmodified
Added
Removed
  • branches/cpp-ode/src/libmmpong/netcon.h

    r508 r538  
    2323                void write(NetMsg::pointer msg) { 
    2424                        LOGDBG("enter"); 
    25                         skt_.get_io_service().post(boost::bind(&NetCon::do_write, this, msg)); 
     25                        skt_.get_io_service().post(boost::bind(&NetCon::do_write, shared_from_this(), msg)); 
    2626                }; 
    2727 
     
    3333 
    3434                void stop() { 
     35                        LOGDBG("enter"); 
     36                        closed=true; 
    3537                        skt_.close(); 
    3638                }; 
     
    3941                        return skt_; 
    4042                }; 
     43 
     44                ~NetCon() { 
     45                        LOGDBG("destroy NetCon"); 
     46                } 
    4147        private: 
    4248                explicit NetCon(boost::asio::io_service &svc, boost::function<void(pointer)> onError,   boost::function<void(pointer, NetMsg::pointer)> onRead) : skt_(svc), onRead_(onRead), onError_(onError) { 
    4349                        rbuf = NetMsg::create(); 
     50                        closed=false; 
    4451                }; 
    4552 
     
    4855                                        skt_, 
    4956                                        rbuf->recv_hdr(), 
    50                                         boost::bind(&NetCon::handle_read_hdr, this, boost::asio::placeholders::error) 
     57                                        boost::bind(&NetCon::handle_read_hdr, shared_from_this(), boost::asio::placeholders::error) 
    5158                                ); 
    5259                }; 
     
    5764                                read_body(); 
    5865                        } else { 
    59                                 do_close(); 
    6066                                LOGDBG("read operation failed."); 
    61                                 onError_(shared_from_this()); 
     67                                if (!closed) { 
     68                                        stop(); 
     69                                        onError_(shared_from_this()); 
     70                                } 
    6271                        } 
    6372                }; 
     
    6776                                        skt_, 
    6877                                        rbuf->recv_body(), 
    69                                         boost::bind(&NetCon::handle_read_body, this, boost::asio::placeholders::error) 
     78                                        boost::bind(&NetCon::handle_read_body, shared_from_this(), boost::asio::placeholders::error) 
    7079                                ); 
    7180                }; 
     
    7584                        if (!err) { 
    7685                                rbuf->recv_finish(); 
    77                                 onRead_(shared_from_this(), rbuf); 
     86                                //onRead_(shared_from_this(), rbuf); 
     87                                skt_.get_io_service().post(boost::bind(onRead_, shared_from_this(), rbuf)); 
     88                                //onRead_(shared_from_this(), rbuf); 
    7889 
     90                                LOGDBG("read next one"); 
    7991                                //read next one 
    8092                                rbuf = NetMsg::create(); 
    8193                                read_hdr(); 
    8294                        } else { 
    83                                 do_close(); 
    84                                 LOGDBG("read operation failed."); 
    85                                 onError_(shared_from_this()); 
     95                                LOGDBG("read operation failed." << err); 
     96                                if (!closed) { 
     97                                        stop(); 
     98                                        onError_(shared_from_this()); 
     99                                } 
    86100                        } 
    87101                }; 
     
    95109                                                skt_,  
    96110                                                msg->send(), 
    97                                                 boost::bind(&NetCon::handle_write, this, boost::asio::placeholders::error) 
     111                                                boost::bind(&NetCon::handle_write, shared_from_this(), boost::asio::placeholders::error) 
    98112                                        ); 
    99113                        } 
     
    108122                                                        skt_,  
    109123                                                        wq.front()->send(), 
    110                                                         boost::bind(&NetCon::handle_write, this, boost::asio::placeholders::error) 
     124                                                        boost::bind(&NetCon::handle_write, shared_from_this(), boost::asio::placeholders::error) 
    111125                                                ); 
    112126                                } 
    113127                        }       else { 
    114                                 do_close(); 
    115                                 LOGDBG("write operation failed."); 
    116                                 onError_(shared_from_this()); 
     128                                LOGDBG("write operation failed." << err); 
     129                                if (!closed) { 
     130                                        stop(); 
     131                                        onError_(shared_from_this()); 
     132                                } 
    117133                        } 
    118                 }; 
    119  
    120                 void do_close() { 
    121                         skt_.close(); 
    122134                }; 
    123135 
     
    127139                std::deque<NetMsg::pointer> wq; //write queue 
    128140                NetMsg::pointer rbuf; //read buffer 
     141                bool closed; 
    129142}; 
    130143 
  • branches/cpp-ode/src/libmmpong/netgame.h

    r528 r538  
    113113                const std::map<uint16_t, NetEnt::pointer>& get_ents() { return ents; }; 
    114114                NetEnt::pointer get_ent(uint16_t entid) { return ents[entid]; }; 
     115                template <class CLASS> boost::shared_ptr<CLASS> get_ent(uint16_t entid) { 
     116                        return boost::dynamic_pointer_cast<CLASS>(get_ent(entid)); 
     117                }; 
    115118 
    116119                //only server creates NetEnts 
  • branches/cpp-ode/src/libmmpong/netgame_ode_pong.cc

    r532 r538  
    263263}; 
    264264 
     265void NetGameODEPong::cln_set_paddlepos(float pos) { 
     266        if (pos>1.0) 
     267                pos=1.0; 
     268        if (pos<0.0) 
     269                pos=0.0; 
     270        if (pos!=mypadpos) { 
     271                mypadpos=pos; 
     272                boost::shared_ptr<ODEBox> pad=get_ent<ODEBox>(mypadid); 
     273                double ang=M_PI*(1.0+2.0*myteamid/srv_teamid_cur); 
     274                pad->setPosition(cos(ang)*1.05, -1.0 + 2.0 * mypadpos, 0.0); 
     275                NetMsg::pointer msg=NetMsg::create(NetMsg::CmdGameCustom); 
     276                msg->pack_begin(); 
     277                uint16_t customcmd=PlayerPadPos; 
     278                msg->pack(customcmd); 
     279                msg->pack(mypadpos); 
     280                msg->pack_finish(); 
     281                cln_con->write(msg); 
     282        } 
     283}; 
     284 
    265285void NetGameODEPong::print_playerlist() { 
    266286        for (std::map<uint16_t, std::set<uint16_t> >::iterator itteam=team2players.begin(); itteam!=team2players.end(); itteam++) { 
  • branches/cpp-ode/src/libmmpong/netgame_ode_pong.h

    r536 r538  
    2929                        team2players[srv_teamid_cur++]=std::set<uint16_t>(); 
    3030                         
     31                        mypadpos=0.5; 
     32                         
    3133                        //ATTENTION: do not perform server-specific actions here 
    3234                        //           => use srv_init() instead 
     
    3739                        PlayerId, 
    3840                        PlayerLeave, 
     41                        PlayerPadPos 
    3942                } CmdCustom; 
    4043                 
     
    4245                virtual NetEnt::pointer cln_ent_update(NetMsg::pointer msg); 
    4346                virtual void cln_msg_custom(NetMsg::pointer msg); 
     47                void cln_set_paddlepos(float pos); //0.0 <= pos <= 1.0 
    4448 
    4549                //server 
     
    7579                //client stuff 
    7680                uint16_t myplayerid, myteamid, mypadid; 
     81                float mypadpos; 
    7782}; 
    7883 
  • branches/cpp-ode/src/mmpong-gl/client_gl.cc

    r537 r538  
    11#include "client_gl.h" 
     2#include "hid.h" 
    23 
    34using namespace mmp; 
     
    101102                        renderer.change_view(mouse_x*CAM_SPEED, mouse_y*CAM_SPEED); 
    102103                } 
    103                 else if (mouse_x != 0 || mouse_y != 0) { 
    104                         //paddle control here! 
     104                else { 
     105                        boost::shared_ptr<HID> hid=boost::dynamic_pointer_cast<HID>(game); 
     106                        if (hid) 
     107                                hid->process(renderer, key_state, mouse_state, mouse_x, mouse_y); 
    105108                } 
    106109 
  • branches/cpp-ode/src/mmpong-gl/netgame_ode_pong_gl.h

    r535 r538  
    66#include "ode_box_gl.h" 
    77#include "ode_sphere_gl.h" 
     8#include "hid.h" 
    89 
    910namespace mmp { 
    1011 
    11 class NetGameODEPongGL : public NetGameODEPong { 
     12class NetGameODEPongGL : public NetGameODEPong, public HID { 
    1213        public: 
    1314                static pointer create(NetCon::pointer cln_con_, const std::string& _playername, const std::string& _resdir) { 
     
    3536                         
    3637                }; 
     38                 
     39                virtual void process(Renderer& renderer, uint8_t *key_state, uint32_t mouse_state, int mouse_x, int mouse_y) {  
     40                        if (mouse_x != 0 || mouse_y != 0) { 
     41                                cln_set_paddlepos(mypadpos + (1.0/100)*mouse_y); 
     42                                 
     43                        } 
     44                }; 
    3745        private: 
    3846                std::string playername; 
  • branches/cpp-ode/src/mmpongd/server.cc

    r528 r538  
    8787                                bool result=game->srv_msg_process(con, msg); 
    8888                                if (!result) { 
    89                                         LOG("Kicking client") 
     89                                        LOGDBG("Kicking client") 
    9090                                        game->srv_con_end(con); 
    9191                                        game->srv_con_remove(con); 
     92                                        man_.stop(con); 
    9293                                } 
    9394                        }