Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_BGP_BGP_SESSION_H_ 6 : #define SRC_BGP_BGP_SESSION_H_ 7 : 8 : #include <boost/scoped_ptr.hpp> 9 : 10 : #include <string> 11 : 12 : #include "bgp/bgp_proto.h" 13 : #include "io/tcp_session.h" 14 : 15 : class BgpPeer; 16 : class BgpSessionManager; 17 : 18 : class BgpMessageReader : public TcpMessageReader { 19 : public: 20 : BgpMessageReader(TcpSession *session, ReceiveCallback callback); 21 : virtual ~BgpMessageReader(); 22 : 23 : protected: 24 : virtual int MsgLength(Buffer buffer, int offset); 25 : 26 99154 : virtual const int GetHeaderLenSize() { 27 99154 : return kHeaderLenSize; 28 : } 29 : 30 2 : virtual const int GetMaxMessageSize() { 31 2 : return kMaxMessageSize; 32 : } 33 : 34 : private: 35 : static const int kHeaderLenSize = 18; 36 : static const int kMaxMessageSize = 4096; 37 : 38 : DISALLOW_COPY_AND_ASSIGN(BgpMessageReader); 39 : }; 40 : 41 : class BgpSession : public TcpSession { 42 : public: 43 : BgpSession(BgpSessionManager *session_mgr, Socket *socket); 44 : virtual ~BgpSession(); 45 : 46 : void SendNotification(int code, int subcode, 47 : const std::string &data = std::string()); 48 100195 : virtual int GetSessionInstance() const { return task_instance_; } 49 : void ProcessWriteReady(); 50 : 51 : void set_peer(BgpPeer *peer); 52 : void clear_peer(); 53 211019 : BgpPeer *peer() { return peer_; } 54 : void LogNotification(int code, int subcode, const std::string &direction, 55 : const std::string &peer_key, 56 : const BgpProto::Notification &msg) const; 57 : 58 : protected: 59 99154 : virtual void OnRead(Buffer buffer) { 60 99154 : reader_->OnRead(buffer); 61 99155 : } 62 : 63 : private: 64 : bool ReceiveMsg(const u_int8_t *msg, size_t size); 65 : virtual void WriteReady(const boost::system::error_code &error); 66 : 67 : BgpSessionManager *session_mgr_; 68 : BgpPeer *peer_; 69 : int task_instance_; 70 : boost::scoped_ptr<BgpMessageReader> reader_; 71 : 72 : DISALLOW_COPY_AND_ASSIGN(BgpSession); 73 : }; 74 : 75 : #endif // SRC_BGP_BGP_SESSION_H_