Skip to content

Commit cd98937

Browse files
author
kbengine
committed
git-svn-id: https://kbengine.svn.sourceforge.net/svnroot/kbengine@164 d26bdfbe-e24b-4f15-971f-8e0596e1bc08
1 parent 1489d32 commit cd98937

15 files changed

Lines changed: 81 additions & 66 deletions

‎kbe/src/lib/cstdkbe/platform.hpp‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ same license as the rest of the engine.
6767
#include <net/if.h>
6868
#endif
6969

70+
#ifndef TCHAR
71+
#ifdef _UNICODE
72+
typedef wchar_t TCHAR;
73+
#else
74+
typedef char TCHAR;
75+
#endif
76+
#endif
77+
7078
/** 定义引擎名字空间 */
7179
namespace KBEngine{
7280
/** 定义引擎字节序 */
@@ -159,10 +167,10 @@ namespace KBEngine{
159167
/*---------------------------------------------------------------------------------
160168
类型定义
161169
---------------------------------------------------------------------------------*/
162-
typedef unsigned char uchar;
163-
typedef unsigned short ushort;
164-
typedef unsigned int uint;
165-
typedef unsigned long ulong;
170+
typedef unsigned char uchar;
171+
typedef unsigned short ushort;
172+
typedef unsigned int uint;
173+
typedef unsigned long ulong;
166174

167175
/* Use correct types for x64 platforms, too */
168176
#if KBE_COMPILER != COMPILER_GNU
@@ -590,29 +598,21 @@ static inline int long2int32(const double value)
590598
#endif
591599

592600

593-
inline char* wchar2char(TCHAR* ts)
601+
inline char* wchar2char(wchar_t* ts)
594602
{
595-
596-
#ifdef _UNICODE
597603
int len = (wcslen(ts) + 1) * 2;
598604
char* ccattr =(char *)malloc(len);
599605
wcstombs(ccattr, ts, len);
600606
return ccattr;
601-
#else
602-
return ts;
603-
#endif
604607
};
605608

606-
inline TCHAR* char2wchar(char* cs)
609+
inline wchar_t* char2wchar(char* cs)
607610
{
608-
#ifdef _UNICODE
609611
int len = (strlen(cs) + 1) * 2;
610-
TCHAR* ccattr =(TCHAR *)malloc(len);
612+
wchar_t* ccattr =(wchar_t *)malloc(len);
611613
mbstowcs(ccattr, cs, len);
612614
return ccattr;
613-
#else
614-
return cs;
615-
#endif
616615
};
616+
617617
}
618618
#endif

‎kbe/src/lib/network/bundle.cpp‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@ namespace Mercury
1616
Bundle::Bundle(Channel * pChannel, PacketType pt):
1717
pChannel_(pChannel),
1818
pCurrPacket_(NULL),
19+
currMsgID_(0),
20+
currMsgPacketCount_(0),
21+
currMsgLength_(0),
22+
currMsgHandlerLength_(0),
1923
packets_(),
2024
isTCPPacket_(pt == TCP_PACKET)
2125
{
2226
newPacket();
2327
}
2428

25-
//-------------------------------------------------------------------------------------
26-
Bundle::Bundle(Packet * p, PacketType pt):
27-
pChannel_(NULL),
28-
pCurrPacket_(NULL),
29-
packets_(),
30-
isTCPPacket_(pt == TCP_PACKET)
31-
{
32-
pCurrPacket_ = p;
33-
}
34-
3529
//-------------------------------------------------------------------------------------
3630
Bundle::~Bundle()
3731
{
@@ -53,8 +47,17 @@ Packet* Bundle::newPacket()
5347
//-------------------------------------------------------------------------------------
5448
void Bundle::finish(void)
5549
{
50+
currMsgLength_ += pCurrPacket_->size();
5651
packets_.push_back(pCurrPacket_);
52+
53+
// 此处对于非固定长度的消息来说需要设置它的最终长度信息
54+
if(currMsgHandlerLength_ == 0)
55+
packets_[packets_.size() - currMsgPacketCount_]->setPacketLength(currMsgLength_);
56+
5757
pCurrPacket_ = NULL;
58+
currMsgID_ = 0;
59+
currMsgPacketCount_ = 0;
60+
currMsgLength_ = 0;
5861
}
5962

6063
//-------------------------------------------------------------------------------------
@@ -66,6 +69,9 @@ void Bundle::clear()
6669

6770
packets_.clear();
6871
SAFE_RELEASE(pCurrPacket_);
72+
73+
currMsgID_ = 0;
74+
currMsgPacketCount_ = 0;
6975
}
7076

7177
//-------------------------------------------------------------------------------------
@@ -95,9 +101,13 @@ void Bundle::send(EndPoint& ep)
95101
void Bundle::newMessage(const MessageHandler& msgHandler)
96102
{
97103
KBE_ASSERT(pCurrPacket_ != NULL);
98-
99-
(*pCurrPacket_) << msgHandler.msgID;
104+
105+
pCurrPacket_->messageID(msgHandler.msgID);
106+
100107
numMessages_++;
108+
currMsgID_ = msgHandler.msgID;
109+
currMsgPacketCount_ = 1;
110+
currMsgHandlerLength_ = msgHandler.msgLen;
101111
}
102112

103113
//-------------------------------------------------------------------------------------

‎kbe/src/lib/network/bundle.hpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class Bundle
3838
typedef std::vector<Packet*> Packets;
3939

4040
Bundle(Channel * pChannel = NULL, PacketType pt = TCP_PACKET);
41-
42-
Bundle(Packet * packetChain, PacketType pt = TCP_PACKET);
4341
virtual ~Bundle();
4442

4543
void newMessage(const MessageHandler& msgHandler);
@@ -68,6 +66,8 @@ class Bundle
6866
if(pCurrPacket_->size() + size > packetmaxsize)
6967
{
7068
packets_.push_back(pCurrPacket_);
69+
currMsgPacketCount_++;
70+
currMsgLength_ += pCurrPacket_->size();
7171
newPacket();
7272
}
7373
}
@@ -232,7 +232,11 @@ class Bundle
232232
int numMessages_;
233233

234234
Packet* pCurrPacket_;
235-
235+
MessageID currMsgID_;
236+
uint8 currMsgPacketCount_;
237+
MessageLength currMsgLength_;
238+
MessageLength currMsgHandlerLength_;
239+
236240
Packets packets_;
237241

238242
bool isTCPPacket_;

‎kbe/src/lib/network/common.hpp‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace Mercury
2525

2626
typedef uint8 MessageID;
2727
typedef uint8 MessageLength;
28-
typedef uint16 PacketHeaderType;
2928

3029
typedef int32 ChannelID;
3130
const ChannelID CHANNEL_ID_NULL = 0;
@@ -40,23 +39,8 @@ namespace tcp{
4039
#define PACKET_MAX_SIZE_TCP 1460
4140
#define PACKET_MAX_SIZE_UDP 1472
4241

43-
#define PACKET_HEADER_SIZE sizeof(PacketHeaderType)
4442
#define MESSAGE_ID_SIZE sizeof(MessageID)
4543

46-
enum PacketHeaders
47-
{
48-
PACKET_HEADER_UNKOWN, ///< 灌列.
49-
PACKET_HEADER_WORLD, ///< 各썹쭉서.
50-
PACKET_HEADER_LOGIN, ///< 되쩌.
51-
PACKET_HEADER_LOGOUT, ///< 잼窟.
52-
};
53-
54-
enum MessageTypes
55-
{
56-
MESSAGE_TYPE_UNKNOWN,
57-
MESSAGE_TYPE_POSITION_ROLL_YAW_PITCH, ///< 谿꼍貫零.
58-
};
59-
6044
enum Reason
6145
{
6246
REASON_SUCCESS = 0, ///< No reason.

‎kbe/src/lib/network/interface_defs.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ namespace Mercury
4848
#undef SET_MESSAGE_FUNC
4949
#endif
5050

51+
52+
5153
#ifdef DEFINE_IN_INTERFACE
5254
//#undef DEFINE_IN_INTERFACE
5355

‎kbe/src/lib/network/message_handler.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace KBEngine {
88
namespace Mercury
99
{
10+
Mercury::MessageHandlers* MessageHandlers::pMainMessageHandlers = 0;
11+
1012
//-------------------------------------------------------------------------------------
1113
MessageHandlers::MessageHandlers():
1214
msgHandlers_(),

‎kbe/src/lib/network/message_handler.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class MessageHandler
5151
class MessageHandlers
5252
{
5353
public:
54+
static Mercury::MessageHandlers* pMainMessageHandlers;
5455
typedef std::map<MessageID, MessageHandler*> MessageHandlerMap;
5556
MessageHandlers();
5657
~MessageHandlers();

‎kbe/src/lib/network/packet.hpp‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Packet : public MemoryStream, public RefCountable
3535
{
3636
public:
3737

38-
Packet(PacketHeaders ph = PACKET_HEADER_UNKOWN, size_t res = 200):
38+
Packet(MessageID msgID = 0, size_t res = 200):
3939
MemoryStream(res),
40-
headerFlags_(PACKET_HEADER_UNKOWN)
40+
msgID_(msgID)
4141
{
4242
};
4343

@@ -49,8 +49,19 @@ class Packet : public MemoryStream, public RefCountable
4949

5050
virtual size_t size() const { return wpos(); }
5151
virtual bool empty() const { return size() > 0; }
52+
53+
inline void messageID(MessageID msgID) {
54+
msgID_ = msgID;
55+
(*this) << msgID;
56+
}
57+
58+
inline MessageID messageID() const { return msgID_; }
59+
60+
virtual void setPacketLength(MessageLength length)
61+
{
62+
}
5263
protected:
53-
PacketHeaders headerFlags_;
64+
MessageID msgID_;
5465

5566
};
5667

‎kbe/src/lib/network/tcp_packet.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
#include "network/bundle.hpp"
66
#include "network/endpoint.hpp"
77
#include "network/network_interface.hpp"
8+
#include "network/message_handler.hpp"
89

910
namespace KBEngine {
1011
namespace Mercury
1112
{
1213
//-------------------------------------------------------------------------------------
13-
TCPPacket::TCPPacket(PacketHeaders ph, size_t res):
14-
Packet(ph, res)
14+
TCPPacket::TCPPacket(MessageID msgID, size_t res):
15+
Packet(msgID, res)
1516
{
1617
resize(PACKET_MAX_SIZE_TCP);
1718
}
@@ -24,6 +25,8 @@ TCPPacket::~TCPPacket(void)
2425
//-------------------------------------------------------------------------------------
2526
int TCPPacket::recvFromEndPoint(EndPoint & ep)
2627
{
28+
//KBE_ASSERT(MessageHandlers::pMainMessageHandlers != NULL && "Must set up a MainMessageHandlers!\n");
29+
2730
int len = ep.recv(data(), PACKET_MAX_SIZE_TCP);
2831
wpos(len);
2932
return len;

‎kbe/src/lib/network/tcp_packet.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Mercury
2828
class TCPPacket : public Packet
2929
{
3030
public:
31-
TCPPacket(PacketHeaders ph = PACKET_HEADER_UNKOWN, size_t res = 0);
31+
TCPPacket(MessageID msgID = 0, size_t res = 0);
3232
virtual ~TCPPacket(void);
3333

3434
int recvFromEndPoint(EndPoint & ep);

0 commit comments

Comments
 (0)