添加 assert(Port, "Port为空,不能发送数据") 提示

This commit is contained in:
cdyong 2016-07-16 04:00:14 +00:00
parent 084903a9e6
commit 49181738f1
1 changed files with 22 additions and 21 deletions

View File

@ -3,9 +3,9 @@
//#define MSG_DEBUG DEBUG //#define MSG_DEBUG DEBUG
#define MSG_DEBUG 0 #define MSG_DEBUG 0
#if MSG_DEBUG #if MSG_DEBUG
#define msg_printf debug_printf #define msg_printf debug_printf
#else #else
#define msg_printf(format, ...) #define msg_printf(format, ...)
#endif #endif
// 构造控制器 // 构造控制器
@ -23,14 +23,14 @@ Controller::~Controller()
{ {
Close(); Close();
if(Port) delete Port; if (Port) delete Port;
debug_printf("TinyNet::UnInit\r\n"); debug_printf("TinyNet::UnInit\r\n");
} }
void Controller::Open() void Controller::Open()
{ {
if(Opened) return; if (Opened) return;
assert(Port, "还没有传输口呢"); assert(Port, "还没有传输口呢");
@ -44,7 +44,7 @@ void Controller::Open()
void Controller::Close() void Controller::Close()
{ {
if(!Opened) return; if (!Opened) return;
Port->Close(); Port->Close();
@ -69,7 +69,7 @@ uint Controller::Dispatch(ITransport* port, Buffer& bs, void* param, void* param
// 这里使用数据流,可能多个消息粘包在一起 // 这里使用数据流,可能多个消息粘包在一起
// 注意此时指针位于0而内容长度为缓冲区长度 // 注意此时指针位于0而内容长度为缓冲区长度
Stream ms((const void*)buf, len); Stream ms((const void*)buf, len);
while(ms.Remain() >= control->MinSize) while (ms.Remain() >= control->MinSize)
{ {
#if MSG_DEBUG #if MSG_DEBUG
uint p = ms.Position(); uint p = ms.Position();
@ -77,7 +77,7 @@ uint Controller::Dispatch(ITransport* port, Buffer& bs, void* param, void* param
len = ms.Remain(); len = ms.Remain();
#endif #endif
// 如果不是有效数据包,则直接退出,避免产生死循环。当然,也可以逐字节移动测试,不过那样性能太差 // 如果不是有效数据包,则直接退出,避免产生死循环。当然,也可以逐字节移动测试,不过那样性能太差
if(!control->Dispatch(ms, nullptr, param2)) if (!control->Dispatch(ms, nullptr, param2))
{ {
#if MSG_DEBUG #if MSG_DEBUG
msg_printf("Controller::Error[%d] ", len); msg_printf("Controller::Error[%d] ", len);
@ -101,12 +101,12 @@ bool Controller::Dispatch(Stream& ms, Message* pmsg, void* param)
auto& msg = *pmsg; auto& msg = *pmsg;
msg.State = param; msg.State = param;
if(!msg.Read(ms)) return false; if (!msg.Read(ms)) return false;
// 校验 // 校验
if(!msg.Valid()) return true; if (!msg.Valid()) return true;
if(!Valid(msg)) if (!Valid(msg))
{ {
/*debug_printf("消息校验未通过\r\n"); /*debug_printf("消息校验未通过\r\n");
msg.Show(); msg.Show();
@ -154,9 +154,8 @@ bool Controller::Reply(Message& msg)
bool Controller::SendInternal(const Message& msg) bool Controller::SendInternal(const Message& msg)
{ {
TS("Controller::SendInternal"); TS("Controller::SendInternal");
// 如果没有传输口处于打开状态,则发送失败 // 如果没有传输口处于打开状态,则发送失败
if(!Port->Open()) return false; if (!Port->Open()) return false;
MemoryStream ms; MemoryStream ms;
// 带有负载数据,需要合并成为一段连续的内存 // 带有负载数据,需要合并成为一段连续的内存
@ -169,6 +168,8 @@ bool Controller::SendInternal(const Message& msg)
bool Controller::SendInternal(const Buffer& bs, const void* state) bool Controller::SendInternal(const Buffer& bs, const void* state)
{ {
assert(Port, "Port为空,不能发送数据");
if (state == nullptr) if (state == nullptr)
return Port->Write(bs); return Port->Write(bs);
else else