Skip to content

Commit 422d620

Browse files
author
kbengine
committed
统一变量命名格式。
git-svn-id: https://kbengine.svn.sourceforge.net/svnroot/kbengine@143 d26bdfbe-e24b-4f15-971f-8e0596e1bc08
1 parent fa2c9c7 commit 422d620

37 files changed

Lines changed: 933 additions & 385 deletions

‎kbe/src/examples/Makefile‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
all:
2-
cd testnetwork && $(MAKE)
1+
ifndef KBE_ROOT
2+
export KBE_ROOT := $(subst /kbe/src/examples,,$(CURDIR))
3+
endif
4+
5+
all clean realclean all_config install unit_tests unit_tests_build unit_tests_run unit_tests_clean::
6+
7+
# Check with and without the trailing slash
8+
ifneq ($(CURDIR),$(KBE_ROOT)/kbe/src/examples)
9+
ifneq ($(CURDIR),$(KBE_ROOT)kbe/src/examples)
10+
@echo 'Error: KBE_ROOT=$(KBE_ROOT)'
11+
@echo ' is not the root of $(CURDIR)'
12+
@echo 'Remove the KBE_ROOT environment variable.'
13+
@false
14+
endif
15+
endif
16+
17+
$(MAKE) -C ../lib $@
18+
$(MAKE) -C testnetwork $@
19+
20+
ifdef KBE_CONFIG
21+
@echo completed $@ \(KBE_CONFIG = $(KBE_CONFIG)\)
22+
else
23+
@echo completed $@
24+
endif
25+
$(MAKE) done
26+
27+
done:
28+
ifdef DO_NOT_BELL
29+
else
30+
echo -n 
31+
endif
32+
33+
server:
34+
$(MAKE) all

‎kbe/src/examples/testnetwork/main.cpp‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ same license as the rest of the engine.
1010
*/
1111
#include "cstdkbe/cstdkbe.hpp"
1212
#include "network/address.hpp"
13-
#include "network/socket.hpp"
13+
#include "network/endpoint.hpp"
1414
#include "network/event_poller.hpp"
1515
#include "helper/debug_helper.hpp"
1616
#include "network/event_dispatcher.hpp"
1717
#include "network/interfaces.hpp"
18-
#include "network/Packet.hpp"
18+
#include "network/tcp_packet.hpp"
1919

2020
using namespace KBEngine;
2121
using namespace KBEngine::Mercury;
2222
Address address;
23-
Socket mysocket;
23+
EndPoint mysocket;
2424
EventDispatcher gdispatcher;
2525

2626

2727
class MyPacketReceiver : public InputNotificationHandler
2828
{
2929
public:
30-
MyPacketReceiver(Socket & mysocket):
30+
MyPacketReceiver(EndPoint & mysocket):
3131
socket_(mysocket),
32-
pNextPacket_(new Packet())
32+
pNextPacket_(new TCPPacket())
3333
{
3434
}
3535

@@ -61,7 +61,7 @@ class MyPacketReceiver : public InputNotificationHandler
6161
}
6262

6363
PacketPtr curPacket = pNextPacket_;
64-
pNextPacket_ = new Packet();
64+
pNextPacket_ = new TCPPacket();
6565
Address srcAddr = socket_.getRemoteAddress();
6666
Reason ret = this->processPacket(srcAddr, curPacket.get());
6767

@@ -168,7 +168,7 @@ class MyPacketReceiver : public InputNotificationHandler
168168
return true;
169169
}
170170
private:
171-
Socket & socket_;
171+
EndPoint & socket_;
172172
PacketPtr pNextPacket_;
173173
};
174174

@@ -199,6 +199,7 @@ void init_network(void)
199199
if(mysocket.connect(htons(port), address) == -1)
200200
{
201201
ERROR_MSG("NetworkInterface::recreateListeningSocket: connect server is error(%s)!\n", strerror(errno));
202+
port = 0;
202203
continue;
203204
}
204205

‎kbe/src/lib/entitydef/entitydef.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ bool EntityDef::loadDetailLevelInfo(std::string& defFilePath, std::string& modul
462462
return false;
463463
}
464464

465-
dlInfo.level[DETAIL_LEVEL_NEAR]->radius = defxml->getValFloat(radiusNode);
466-
dlInfo.level[DETAIL_LEVEL_NEAR]->hyst = defxml->getValFloat(hystNode);
465+
dlInfo.level[DETAIL_LEVEL_NEAR]->radius = (float)defxml->getValFloat(radiusNode);
466+
dlInfo.level[DETAIL_LEVEL_NEAR]->hyst = (float)defxml->getValFloat(hystNode);
467467

468468
node = defxml->enterNode(detailLevelNode, "MEDIUM");
469469
radiusNode = defxml->enterNode(node, "radius");
@@ -474,9 +474,9 @@ bool EntityDef::loadDetailLevelInfo(std::string& defFilePath, std::string& modul
474474
return false;
475475
}
476476

477-
dlInfo.level[DETAIL_LEVEL_MEDIUM]->radius = defxml->getValFloat(radiusNode);
477+
dlInfo.level[DETAIL_LEVEL_MEDIUM]->radius = (float)defxml->getValFloat(radiusNode);
478478
dlInfo.level[DETAIL_LEVEL_MEDIUM]->radius += dlInfo.level[DETAIL_LEVEL_NEAR]->radius + dlInfo.level[DETAIL_LEVEL_NEAR]->hyst;
479-
dlInfo.level[DETAIL_LEVEL_MEDIUM]->hyst = defxml->getValFloat(hystNode);
479+
dlInfo.level[DETAIL_LEVEL_MEDIUM]->hyst = (float)defxml->getValFloat(hystNode);
480480

481481
node = defxml->enterNode(detailLevelNode, "FAR");
482482
radiusNode = defxml->enterNode(node, "radius");
@@ -487,9 +487,9 @@ bool EntityDef::loadDetailLevelInfo(std::string& defFilePath, std::string& modul
487487
return false;
488488
}
489489

490-
dlInfo.level[DETAIL_LEVEL_FAR]->radius = defxml->getValFloat(radiusNode);
490+
dlInfo.level[DETAIL_LEVEL_FAR]->radius = (float)defxml->getValFloat(radiusNode);
491491
dlInfo.level[DETAIL_LEVEL_FAR]->radius += dlInfo.level[DETAIL_LEVEL_MEDIUM]->radius + dlInfo.level[DETAIL_LEVEL_MEDIUM]->hyst;
492-
dlInfo.level[DETAIL_LEVEL_FAR]->hyst = defxml->getValFloat(hystNode);
492+
dlInfo.level[DETAIL_LEVEL_FAR]->hyst = (float)defxml->getValFloat(hystNode);
493493
return true;
494494

495495
}

‎kbe/src/lib/network/Makefile‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ SRCS = \
99
event_poller \
1010
listener_receiver \
1111
network_interface \
12-
packet \
1312
packet_filter \
1413
packet_receiver \
15-
socket \
16-
14+
endpoint \
15+
tcp_packet \
16+
tcp_packet_receiver \
17+
udp_packet \
18+
udp_packet_receiver
1719

1820
ifndef KBE_ROOT
1921
export KBE_ROOT := $(subst /kbe/src/lib/$(LIB),,$(CURDIR))

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ class Address
3030
int writeToString(char * str, int length) const;
3131

3232
operator char*() const { return this->c_str(); }
33-
bool operator==(const Address & b);
34-
bool operator!=(const Address & b);
35-
33+
3634
char * c_str() const;
3735
const char * ipAsString() const;
3836
bool isNone() const { return this->ip == 0; }
@@ -52,14 +50,26 @@ inline Address::Address(uint32 ipArg, uint16 portArg) :
5250
{
5351
}
5452

55-
inline bool Address::operator==(const Address & b)
53+
inline bool operator==(const Mercury::Address & a, const Mercury::Address & b)
54+
{
55+
return (a.ip == b.ip) && (a.port == b.port);
56+
}
57+
58+
59+
inline bool operator!=(const Mercury::Address & a, const Mercury::Address & b)
5660
{
57-
return (ip == b.ip) && (port == b.port);
61+
return (a.ip != b.ip) || (a.port != b.port);
5862
}
5963

60-
inline bool Address::operator!=(const Address & b)
64+
/**
65+
* This operator compares two addresses. It is needed
66+
* for using an address as a key in an STL map.
67+
*
68+
* @return true if a is less than b, false otherwise.
69+
*/
70+
inline bool operator<(const Mercury::Address & a, const Mercury::Address & b)
6171
{
62-
return (ip != b.ip) || (port != b.port);
72+
return (a.ip < b.ip) || (a.ip == b.ip && (a.port < b.port));
6373
}
6474

6575

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ same license as the rest of the engine.
1616
#include "helper/debug_helper.hpp"
1717
#include "network/address.hpp"
1818
#include "network/event_dispatcher.hpp"
19-
#include "network/socket.hpp"
19+
#include "network/endpoint.hpp"
2020
#include "network/common.hpp"
2121
#include "network/packet.hpp"
2222

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#endif
55
#include "network/bundle.hpp"
66
#include "network/network_interface.hpp"
7+
#include "network/tcp_packet_receiver.hpp"
78

89
namespace KBEngine {
910
namespace Mercury
@@ -14,7 +15,7 @@ const int INDEXED_CHANNEL_SIZE = 512;
1415

1516

1617
Channel::Channel(NetworkInterface & networkInterface,
17-
const Socket * socket, Traits traits,
18+
const EndPoint * endpoint, Traits traits,
1819
PacketFilterPtr pFilter, ChannelID id):
1920
pNetworkInterface_(&networkInterface),
2021
traits_(traits),
@@ -38,7 +39,7 @@ Channel::Channel(NetworkInterface & networkInterface,
3839
numBytesSent_(0),
3940
numBytesReceived_(0),
4041
pFilter_(pFilter),
41-
pSocket_(NULL),
42+
pEndPoint_(NULL),
4243
pPacketReceiver_(NULL)
4344
{
4445
// This corresponds to the decRef in Channel::destroy.
@@ -52,45 +53,47 @@ Channel::Channel(NetworkInterface & networkInterface,
5253
}
5354

5455
this->clearBundle();
55-
this->socket(socket);
56+
this->endpoint(endpoint);
5657

57-
pPacketReceiver_ = new PacketReceiver(*pSocket_, networkInterface);
58+
pPacketReceiver_ = new TCPPacketReceiver(*pEndPoint_, networkInterface);
59+
pNetworkInterface_->dispatcher().registerFileDescriptor(*pEndPoint_, pPacketReceiver_);
5860
}
5961

6062
//-------------------------------------------------------------------------------------
6163
Channel * Channel::get(NetworkInterface & networkInterface,
62-
KBESOCKET s)
64+
const Address& addr)
6365
{
64-
return networkInterface.findChannel(s);
66+
return networkInterface.findChannel(addr);
6567
}
6668

6769
//-------------------------------------------------------------------------------------
6870
Channel * get(NetworkInterface & networkInterface,
69-
const Socket* pSocket)
71+
const EndPoint* pSocket)
7072
{
71-
return networkInterface.findChannel(*pSocket);
73+
return networkInterface.findChannel(pSocket->addr());
7274
}
7375

7476
//-------------------------------------------------------------------------------------
75-
void Channel::socket(const Socket* socket)
77+
void Channel::endpoint(const EndPoint* endpoint)
7678
{
77-
if (socket && pSocket_ != socket)
79+
if (endpoint && pEndPoint_ != endpoint)
7880
{
7981
lastReceivedTime_ = timestamp();
80-
pSocket_ = const_cast<Socket*>(socket);
82+
pEndPoint_ = const_cast<EndPoint*>(endpoint);
8183
}
8284
}
8385

8486
//-------------------------------------------------------------------------------------
8587
Channel::~Channel()
8688
{
8789
pNetworkInterface_->onChannelGone(this);
88-
pSocket_->close();
89-
pSocket_->detach();
90+
pNetworkInterface_->dispatcher().deregisterFileDescriptor(*pEndPoint_);
91+
pEndPoint_->close();
92+
pEndPoint_->detach();
9093

9194
SAFE_RELEASE(pPacketReceiver_);
9295
SAFE_RELEASE(pBundle_);
93-
SAFE_RELEASE(pSocket_);
96+
SAFE_RELEASE(pEndPoint_);
9497
}
9598

9699
//-------------------------------------------------------------------------------------
@@ -161,7 +164,7 @@ const char * Channel::c_str() const
161164
{
162165
static char dodgyString[ 40 ];
163166

164-
int length = pSocket_->addr().writeToString(dodgyString, sizeof(dodgyString));
167+
int length = pEndPoint_->addr().writeToString(dodgyString, sizeof(dodgyString));
165168

166169
length += kbe_snprintf(dodgyString + length,
167170
sizeof(dodgyString) - length, "/%d", id_);
@@ -216,17 +219,17 @@ void Channel::handleTimeout(TimerHandle, void * arg)
216219
}
217220

218221
//-------------------------------------------------------------------------------------
219-
void Channel::reset(const Socket* socket, bool warnOnDiscard)
222+
void Channel::reset(const EndPoint* endpoint, bool warnOnDiscard)
220223
{
221224
// Don't do anything if the address hasn't changed.
222-
if (socket == pSocket_)
225+
if (endpoint == pEndPoint_)
223226
{
224227
return;
225228
}
226229

227230
// This handles registering this channel (deregistering done in
228231
// clearState above).
229-
this->socket(socket);
232+
this->endpoint(endpoint);
230233

231234
// If we're establishing this channel, call the bundle primer, since
232235
// we just cleared the bundle.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ same license as the rest of the engine.
1616
#include "helper/debug_helper.hpp"
1717
#include "network/address.hpp"
1818
#include "network/event_dispatcher.hpp"
19-
#include "network/socket.hpp"
19+
#include "network/endpoint.hpp"
2020
#include "network/packet.hpp"
2121
#include "network/common.hpp"
2222
#include "network/bundle.hpp"
@@ -46,15 +46,15 @@ class Channel : public TimerHandler, public RefCountable
4646
EXTERNAL = 1,
4747
};
4848
public:
49-
Channel(NetworkInterface & networkInterface, const Socket * socket, Traits traits, PacketFilterPtr pFilter = NULL, ChannelID id = CHANNEL_ID_NULL);
49+
Channel(NetworkInterface & networkInterface, const EndPoint * endpoint, Traits traits, PacketFilterPtr pFilter = NULL, ChannelID id = CHANNEL_ID_NULL);
5050

5151
virtual ~Channel();
5252

5353
static Channel * get(NetworkInterface & networkInterface,
54-
KBESOCKET s);
54+
const Address& addr);
5555

5656
static Channel * get(NetworkInterface & networkInterface,
57-
const Socket* pSocket);
57+
const EndPoint* pSocket);
5858

5959
PacketFilterPtr pFilter() const { return pFilter_; }
6060
void pFilter(PacketFilterPtr pFilter) { pFilter_ = pFilter; }
@@ -73,8 +73,8 @@ class Channel : public TimerHandler, public RefCountable
7373
NetworkInterface & networkInterface() { return *pNetworkInterface_; }
7474

7575
INLINE const Address& addr() const;
76-
void socket(const Socket* socket);
77-
INLINE const Socket * socket() const;
76+
void endpoint(const EndPoint* endpoint);
77+
INLINE const EndPoint * endpoint() const;
7878
Bundle & bundle();
7979
const Bundle & bundle() const;
8080
bool hasUnsentData() const;
@@ -84,7 +84,7 @@ class Channel : public TimerHandler, public RefCountable
8484
void send(Bundle * pBundle = NULL);
8585
void delayedSend();
8686

87-
void reset(const Socket* socket, bool warnOnDiscard = true);
87+
void reset(const EndPoint* endpoint, bool warnOnDiscard = true);
8888

8989
void dropNextSend() { shouldDropNextSend_ = true; }
9090

@@ -144,7 +144,7 @@ class Channel : public TimerHandler, public RefCountable
144144

145145
PacketFilterPtr pFilter_;
146146

147-
Socket * pSocket_;
147+
EndPoint * pEndPoint_;
148148
PacketReceiver* pPacketReceiver_;
149149
};
150150

‎kbe/src/lib/network/channel.ipp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace Mercury
44

55
INLINE const Address & Channel::addr() const
66
{
7-
return pSocket_->addr();
7+
return pEndPoint_->addr();
88
}
99

10-
INLINE const Socket * Channel::socket() const
10+
INLINE const EndPoint * Channel::endpoint() const
1111
{
12-
return pSocket_;
12+
return pEndPoint_;
1313
}
1414

1515
INLINE int Channel::windowSize() const

0 commit comments

Comments
 (0)