[go: nahoru, domu]

Skip to content

Commit

Permalink
feat: 添加新的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
tangruo committed Sep 20, 2016
1 parent f6f718a commit 8285eae
Show file tree
Hide file tree
Showing 94 changed files with 5,990 additions and 14 deletions.
Empty file.
Empty file.
75 changes: 73 additions & 2 deletions Ballet/Network/BalTcpClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,49 @@
using namespace Ballet;
using namespace Ballet::Network;

BalTcpClient::BalTcpClient(BalHandle<BalEventLoop> eventLoop)
BalTcpClient::BalTcpClient(bool v6,
BalHandle<BalEventLoop> eventLoop,
BalHandle<IBalProtocol> protocol, uint32_t maxPackage,
BalHandle<IBalTcpCallback> callback,
uint32_t maxReadBufferSize, uint32_t maxWriteBufferSize)
:BalTcpSocket(v6)
{
if (!eventLoop)
if (!eventLoop || !protocol || !callback || !callback->IsCallable())
{
throw std::runtime_error("BalTcpClient Construct Failed!");
}

eventLoop_ = eventLoop;
tcpCallback_ = callback;
maxPackage_ = maxPackage;
lastSendBufferTime_ = 0;
maxReadBufferSize_ = maxReadBufferSize;
maxWriteBufferSize_ = maxWriteBufferSize;
}

bool BalTcpClient::IsV6()
{
return BalTcpSocket::IsV6Socket();
}

bool BalTcpClient::Close(bool now)
{
return true;
}

bool BalTcpClient::ShutdownWrite()
{
return true;
}

bool BalTcpClient::WriteBuffer(const char* buffer, uint32_t len)
{
return true;
}

bool BalTcpClient::WriteRawBuffer(const char* buffer, uint32_t len)
{
return true;
}

bool BalTcpClient::Connect(BalHandle<BalInetAddress> addr) throw()
Expand All @@ -20,3 +56,38 @@ bool BalTcpClient::BindAddress(BalHandle<BalInetAddress> addr) throw()
{
return true;
}

uint32_t BalTcpClient::GetMaxPackageSize() const
{
return maxPackage_;
}

uint32_t BalTcpClient::GetMaxReadBufferSize() const
{
return maxPackage_;
}

uint32_t BalTcpClient::GetMaxWriteBufferSize() const
{
return maxPackage_;
}

uint32_t BalTcpClient::GetLastSendBufferTime() const
{
return maxPackage_;
}

BalHandle<BalEventLoop> BalTcpClient::GetEventLoop() const
{
return eventLoop_;
}

BalHandle<IBalProtocol> BalTcpClient::GetProtocol() const
{
return tcpProtocol_;
}

BalHandle<IBalTcpCallback> BalTcpClient::GetCallback() const
{
return tcpCallback_;
}
30 changes: 28 additions & 2 deletions Ballet/Network/BalTcpClient.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
#ifndef Ballet_Network_BalTcpClient_H
#define Ballet_Network_BalTcpClient_H
#include "Common/BalInct.h"
#include "BootUtil/BalTimeStamp.h"
#include "BalNetworkInct.h"
#include "BalTcpSocket.h"
#include "BalTcpCallback.h"
#include "BalInetAddress.h"
#include "BalEventLoop.h"
#include "BalProtocol.h"
#include "BalChannel.h"
#include "BalBufferStream.h"

namespace Ballet
{
namespace Network
{
class BalTcpClient :public BalElement, public BalNoCoable
class BalTcpClient :public BalElement,
public BalTcpSocket, public BalChannel, public BalShareThis
{
public:
BalTcpClient(BalHandle<BalEventLoop> eventLoop);
BalTcpClient(bool v6, BalHandle<BalEventLoop> eventLoop,
BalHandle<IBalProtocol> protocol, uint32_t maxPackage,
BalHandle<IBalTcpCallback> callback,
uint32_t maxReadBufferSize, uint32_t maxWriteBufferSize);

public:
bool IsV6();
bool Close(bool now);
bool ShutdownWrite();
bool WriteBuffer(const char* buffer, uint32_t len);
bool WriteRawBuffer(const char* buffer, uint32_t len);
bool Connect(BalHandle<BalInetAddress> addr) throw();
bool BindAddress(BalHandle<BalInetAddress> addr) throw();
uint32_t GetMaxPackageSize() const;
uint32_t GetMaxReadBufferSize() const;
uint32_t GetMaxWriteBufferSize() const;
uint32_t GetLastSendBufferTime() const;
BalHandle<BalEventLoop> GetEventLoop() const;
BalHandle<IBalProtocol> GetProtocol() const;
BalHandle<IBalTcpCallback> GetCallback() const;

protected:
BalHandle<IBalProtocol> tcpProtocol_;
BalWeakHandle<BalEventLoop> eventLoop_;
BalHandle<IBalTcpCallback> tcpCallback_;
uint32_t maxPackage_, lastSendBufferTime_;
uint32_t maxReadBufferSize_, maxWriteBufferSize_;
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion Ballet/Network/BalTcpConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ BalTcpConnection::BalTcpConnection(int id, BalHandle<BalTcpServer> server)
else
{
uint32_t timeout = (uint32_t)(tcpServer_->GetTimeout());
eventLoop->SetTimerOut(0, timerCallbackPtr_, timeout);
if (timeout > 0)
{
eventLoop->SetTimerOut(0, timerCallbackPtr_, timeout);
}
eventLoop->SetEventListener(GetFd(), EventReadWrite, eventCallbackPtr_);
}
}
Expand Down
6 changes: 2 additions & 4 deletions Ballet/Network/BalTcpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Ballet
public:
BalTcpConnection(int id, BalHandle<BalTcpServer> server);
virtual ~BalTcpConnection();

public:
bool IsV6();
bool Close(bool now);
Expand All @@ -45,7 +45,7 @@ namespace Ballet
BalHandle<BalInetAddress> GetPeer() const;
BalHandle<BalInetAddress> GetLocal() const;

private:
protected:
bool DoCloseProcedure(bool accord, bool delEvent);
bool OnReceiveBuffer(const char* buffer, uint32_t len);
void OnTime(uint32_t id, BalHandle<BalTimer> timer);
Expand All @@ -56,8 +56,6 @@ namespace Ballet

protected:
BalConnStatusEnum status_;
BalWeakHandle<BalEventLoop> eventLoop_;
BalHandle<IBalTcpCallback> tcpCallback_;
BalWeakHandle<BalTcpServer> tcpServer_;
CBalTimerCallbackPtr<BalTcpConnection> timerCallbackPtr_;
CBalEventCallbackPtr<BalTcpConnection> eventCallbackPtr_;
Expand Down
2 changes: 1 addition & 1 deletion Ballet/Network/BalTcpServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BalTcpServer::BalTcpServer(bool v6, BalHandle<BalEventLoop> eventLoop,
maxReadBufferSize_ = maxReadBufferSize;
maxWriteBufferSize_ = maxWriteBufferSize;

if (!callback || !callback->IsCallable() || !timeout || !protocol_)
if (!callback || !callback->IsCallable() || !protocol_)
{
throw std::runtime_error("BalTcpServer Construct Failed");
}
Expand Down
46 changes: 46 additions & 0 deletions Ballet/Protocol/Http/BalHttpCallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef Ballet_Protocol_Http_BalHttpCallback_H
#define Ballet_Protocol_Http_BalHttpCallback_H
#include "Common/BalInct.h"

namespace Ballet
{
namespace Protocol
{
enum BalHttpCallbackErrorEnum
{
HttpErrorNone = 0,
HttpErrorDecode = 1,
HttpErrorEncode = 2,
HttpErrorExceed = 3,
};

class BalHttpConnection;
typedef BalHandle<BalHttpConnection> BalHttpConnectionPtr;
struct IBalHttpCallback :public BalCallback
{
virtual bool OnConnect(BalHttpConnectionPtr conn, bool success) =0;
virtual bool OnHttpBegin(BalHttpConnectionPtr conn) =0;
virtual bool OnHttpHeader(BalHttpConnectionPtr conn) =0;
virtual bool OnHttpBody(BalHttpConnectionPtr conn, const char* buffer, uint32_t) =0;
virtual bool OnHttpComplete(BalHttpConnectionPtr conn) =0;
virtual bool OnHttpError(BalHttpConnectionPtr conn, BalHttpCallbackErrorEnum error) =0;
virtual bool OnHttpClose(BalHttpConnectionPtr conn, bool accord) =0;
virtual void OnWriteBufferFull(BalHttpConnectionPtr conn) =0;
virtual void OnWriteBufferDrain(BalHttpConnectionPtr conn) =0;
};

BalCallbackSinkBegin(CBalHttpCallback)
BalCallbackSink(void, OnConnect, (BalHttpConnectionPtr conn, bool success), (conn, success))
BalCallbackSink(void, OnHttpBegin, (BalHttpConnectionPtr conn), (conn))
BalCallbackSink(void, OnHttpHeader, (BalHttpConnectionPtr conn), (conn))
BalCallbackSink(void, OnHttpBody, (BalHttpConnectionPtr conn, const char* buffer, uint32_t len), (conn, buffer, len))
BalCallbackSink(void, OnHttpComplete, (BalHttpConnectionPtr conn), (conn))
BalCallbackSink(void, OnHttpError, (BalHttpConnectionPtr conn, BalHttpCallbackErrorEnum error), (conn, error))
BalCallbackSink(void, OnHttpClose, (BalHttpConnectionPtr conn, bool accord), (conn, accord))
BalCallbackSink(void, OnWriteBufferFull, (BalHttpConnectionPtr conn), (conn))
BalCallbackSink(void, OnWriteBufferDrain, (BalHttpConnectionPtr conn), (conn))
BalCallbackSinkComplete()
BalCallbackSinkPtrDefine(CBalHttpCallback, IBalHttpCallback)
}
}
#endif//Ballet_Protocol_Http_BalHttpCallback_H
Empty file.
Empty file.
Loading

0 comments on commit 8285eae

Please sign in to comment.