-class connection : public aio_callback {
- public:
- struct charbuf {
- charbuf(): buf(NULL), sz(0), solong(0) {}
- charbuf (char *b, int s) : buf(b), sz(s), solong(0){}
- char *buf;
- int sz;
- int solong; //amount of bytes written or read so far
- };
-
- connection(chanmgr *m1, int f1, int lossytest=0);
- ~connection();
-
- int channo() { return fd_; }
- bool isdead();
- void closeconn();
-
- bool send(char *b, int sz);
- void write_cb(int s);
- void read_cb(int s);
-
- void incref();
- void decref();
- int ref();
-
- int compare(connection *another);
- private:
-
- bool readpdu();
- bool writepdu();
-
- chanmgr *mgr_;
- const int fd_;
- bool dead_;
-
- charbuf wpdu_;
- charbuf rpdu_;
-
- struct timeval create_time_;
-
- int waiters_;
- int refno_;
- const int lossy_;
-
- pthread_mutex_t m_;
- pthread_mutex_t ref_m_;
- pthread_cond_t send_complete_;
- pthread_cond_t send_wait_;
+class connection : public aio_callback, public enable_shared_from_this<connection> {
+ public:
+ struct charbuf {
+ string buf;
+ size_t solong = 0; // number of bytes written or read so far
+ };
+
+ connection(chanmgr *m1, int f1, int lossytest=0);
+ ~connection();
+
+ int channo() { return fd_; }
+ bool isdead() { lock ml(m_); return dead_; }
+ void closeconn();
+
+ bool send(const string & b);
+ void write_cb(int s);
+ void read_cb(int s);
+
+ time_point<steady_clock> create_time() const { return create_time_; }
+
+ private:
+
+ bool readpdu();
+ bool writepdu();
+
+ chanmgr *mgr_;
+ const file_t fd_;
+ bool dead_ = false;
+
+ charbuf wpdu_;
+ charbuf rpdu_;
+
+ time_point<steady_clock> create_time_;
+
+ int waiters_ = 0;
+ int lossy_ = 0;
+
+ mutex m_;
+ cond send_complete_;
+ cond send_wait_;