Skip to content

Commit 586d7e4

Browse files
author
unknown
committed
抽离一个公共的发送队列
1 parent 658f53a commit 586d7e4

9 files changed

+134
-171
lines changed

src/network/BasePacket.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ bool BasePacket::isHeadFull()
106106
// send msg call
107107
int32 BasePacket::sendSize()
108108
{
109-
return 0;
109+
return wpos();
110110
}
111111

112112
char * BasePacket::sendStream()
113113
{
114-
return NULL;
114+
return (char *)contents();
115115
}
116116

117117
int BasePacket::readPos()

src/network/HttpConnect.cpp

+3-40
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ HttpConnect::~HttpConnect()
5454
delete m_readPacket;
5555
free(m_parser);
5656
free(m_url);
57-
release();
5857
}
5958

6059
void HttpConnect::zero()
@@ -65,21 +64,11 @@ void HttpConnect::zero()
6564
m_content = NULL;
6665
}
6766

68-
void HttpConnect::release()
69-
{
70-
while (!m_writePackets.empty())
71-
{
72-
recyclePacket(m_writePackets.front());
73-
m_writePackets.pop();
74-
}
75-
}
76-
7767
void HttpConnect::sendMsg(const char* msg, int32 len)
7868
{
7969
BasePacket* pack = createPacket();
8070
pack->append(msg, len);
81-
m_writePackets.push(pack);
82-
send_top_msg();
71+
TcpSocket::write(pack);
8372
}
8473
void HttpConnect::sendData(std::string_view sv)
8574
{
@@ -93,8 +82,7 @@ void HttpConnect::autoMsg(std::string_view sv, enum http_content_type type)
9382
BasePacket * pack = createPacket();
9483
pack->append(buff, strlen(buff));
9584
pack->append((const uint8*)sv.data(), sv.size());
96-
m_writePackets.push(pack);
97-
send_top_msg();
85+
TcpSocket::write(pack);
9886
}
9987

10088
const char * HttpConnect::getContentTypeStr(enum http_content_type type)
@@ -116,15 +104,6 @@ const char * HttpConnect::getContentTypeStr(enum http_content_type type)
116104
return ctype;
117105
}
118106

119-
BasePacket * HttpConnect::createPacket()
120-
{
121-
return new BasePacket;
122-
}
123-
void HttpConnect::recyclePacket(BasePacket * pack)
124-
{
125-
delete pack;
126-
}
127-
128107
int HttpConnect::on_url(http_parser* _, const char *at, size_t length)
129108
{
130109
HttpConnect * conn = (HttpConnect *)(_->data);
@@ -242,22 +221,6 @@ void HttpConnect::on_writecomplete()
242221
{
243222
this->close();
244223
}
245-
else
246-
{
247-
if (m_writePackets.empty())
248-
return;
249-
250-
recyclePacket(m_writePackets.front());
251-
m_writePackets.pop();
252-
send_top_msg();
253-
}
254-
}
255-
256-
void HttpConnect::send_top_msg()
257-
{
258-
if (m_writePackets.empty())
259-
return;
260224

261-
BasePacket *pack = m_writePackets.front();
262-
write((char *)pack->contents(), pack->wpos());
225+
TcpSocket::on_writecomplete();
263226
}

src/network/HttpConnect.h

-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "TcpSocket.h"
1111
#include "HttpEvent.h"
1212
#include "PoolObject.h"
13-
#include <queue>
1413

1514
enum http_content_type
1615
{
@@ -35,7 +34,6 @@ class HttpConnect : public TcpSocket
3534
~HttpConnect();
3635

3736
void zero();
38-
void release();
3937
void setEvent(HttpEvent * e) { m_event = e; }
4038
void sendMsg(const char* msg, int32 len);
4139
// lua call
@@ -53,15 +51,11 @@ class HttpConnect : public TcpSocket
5351
bool parser(const char *, int);
5452
void complete();
5553

56-
void send_top_msg();
57-
BasePacket * createPacket();
58-
void recyclePacket(BasePacket * pack);
5954
private:
6055
HttpEvent * m_event = NULL;
6156
http_parser * m_parser;
6257
http_parser_url * m_url;
6358
BasePacket * m_readPacket;
64-
std::queue<BasePacket *> m_writePackets;
6559
const char * m_urlp; //url指针
6660
const char * m_content; //post数据指针
6761
int m_residue;

src/network/NetConnect.cpp

+3-44
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ NetConnect::~NetConnect()
1717
recyclePacket(mReadPacket);
1818
mReadPacket = NULL;
1919
}
20-
21-
release();
2220
}
2321

2422
void NetConnect::on_msgbuffer(MessageBuffer * buffer)
@@ -77,9 +75,7 @@ void NetConnect::sendMsg(uint32 msgtype, NetPacket * pack)
7775
packet->moveData(pack);
7876
packet->writeHead(msgtype);
7977

80-
mSendPackets.push(packet);
81-
82-
send_top_msg();
78+
TcpSocket::write(packet);
8379
}
8480

8581
void NetConnect::sendMsg(uint32 msgtype, const char* msg, uint32 len)
@@ -90,48 +86,11 @@ void NetConnect::sendMsg(uint32 msgtype, const char* msg, uint32 len)
9086
pack->append(msg, len);
9187
pack->writeHead(msgtype);
9288

93-
mSendPackets.push(pack);
94-
95-
send_top_msg();
89+
TcpSocket::write(pack);
9690
}
9791

9892

99-
NetPacket * NetConnect::createPacket()
93+
NetPacket* NetConnect::createPacket()
10094
{
10195
return new NetPacket;
102-
}
103-
104-
void NetConnect::recyclePacket(NetPacket * pack)
105-
{
106-
delete pack;
107-
}
108-
109-
void NetConnect::on_writecomplete()
110-
{
111-
if (mSendPackets.empty())
112-
return;
113-
114-
//write complete
115-
recyclePacket(mSendPackets.front());
116-
mSendPackets.pop();
117-
118-
send_top_msg();
119-
}
120-
121-
void NetConnect::send_top_msg()
122-
{
123-
if (mSendPackets.empty())
124-
return;
125-
126-
NetPacket *tp = mSendPackets.front();
127-
write(tp->sendStream(), tp->sendSize());
128-
}
129-
130-
void NetConnect::release()
131-
{
132-
while (!mSendPackets.empty())
133-
{
134-
recyclePacket(mSendPackets.front());
135-
mSendPackets.pop();
136-
}
13796
}

src/network/NetConnect.h

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define NET_CONNECT_H
33

44
#include "TcpSocket.h"
5-
#include <queue>
65

76
class NetPacket;
87
class NetEvent;
@@ -22,20 +21,14 @@ class NetConnect : public TcpSocket
2221
void sendPacket(uint32 msgtype, NetPacket * pack) { sendMsg(msgtype, pack);}
2322
void sendData(uint32 msgtype, std::string_view view) { sendMsg(msgtype, view.data(), view.size()); }
2423

25-
static NetPacket * createPacket();
26-
static void recyclePacket(NetPacket * pack);
2724
protected:
25+
NetPacket* createPacket();
26+
//virtual void recyclePacket(BasePacket* pack);
2827

2928
virtual void on_msgbuffer(MessageBuffer * buffer);
3029
virtual void on_clsesocket();
31-
virtual void on_writecomplete();
32-
33-
private:
34-
void send_top_msg();
35-
void release();
3630
private:
3731
NetPacket * mReadPacket;
38-
std::queue<NetPacket *> mSendPackets;
3932
NetEvent * _netevent;
4033
};
4134

0 commit comments

Comments
 (0)