-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
594 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "HttpConnection.h" | ||
|
||
using namespace xop; | ||
|
||
HttpConnection::HttpConnection(mg_connection* mg_conn) | ||
: mg_conn_(mg_conn) | ||
, max_queue_length_(kMaxQueueLength) | ||
, buffer_(new std::queue<Packet>) | ||
{ | ||
|
||
} | ||
|
||
HttpConnection::~HttpConnection() | ||
{ | ||
|
||
} | ||
|
||
void HttpConnection::Poll() | ||
{ | ||
mutex_.lock(); | ||
if (!buffer_->empty()) { | ||
OnSend(); | ||
} | ||
mutex_.unlock(); | ||
} | ||
|
||
SOCKET HttpConnection::GetSocket() | ||
{ | ||
return (SOCKET)mg_conn_->sock; | ||
} | ||
|
||
void HttpConnection::Send(const char* data, uint32_t data_size) | ||
{ | ||
std::lock_guard<std::mutex> locker(mutex_); | ||
|
||
if (!mg_conn_) { | ||
return; | ||
} | ||
|
||
if (buffer_->size() < max_queue_length_) { | ||
Packet pkt; | ||
pkt.data.reset(new char[data_size + 512]); | ||
memcpy(pkt.data.get(), data, data_size); | ||
pkt.size = data_size; | ||
pkt.write_index = 0; | ||
buffer_->emplace(std::move(pkt)); | ||
} | ||
} | ||
|
||
void HttpConnection::OnSend() | ||
{ | ||
if (!buffer_->empty()) { | ||
//printf("send_queue_len: %llu, send_buf_len: %llu, send_buf_size: %llu \n", \ | ||
// buffer_->size(), mg_conn_->send_mbuf.len, mg_conn_->send_mbuf.size); | ||
|
||
if (mg_conn_->send_mbuf.len > kMaxBufferLen) { | ||
//buffer_->pop(); | ||
return; | ||
} | ||
|
||
Packet &pkt = buffer_->front(); | ||
mg_send(mg_conn_, pkt.data.get(), pkt.size); | ||
buffer_->pop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef HTTP_CONNECTION_H | ||
#define HTTP_CONNECTION_H | ||
|
||
extern "C" { | ||
#include "mongoose/mongoose.h" | ||
} | ||
#include "net/Socket.h" | ||
#include <cstdint> | ||
#include <memory> | ||
#include <mutex> | ||
#include <queue> | ||
|
||
namespace xop { | ||
|
||
class HttpConnection | ||
{ | ||
public: | ||
HttpConnection(mg_connection* mg_conn); | ||
virtual ~HttpConnection(); | ||
|
||
virtual void Poll(); | ||
|
||
virtual void Send(const char* data, uint32_t data_size); | ||
virtual SOCKET GetSocket(); | ||
|
||
protected: | ||
void OnSend(); | ||
|
||
mg_connection* mg_conn_; | ||
|
||
std::mutex mutex_; | ||
|
||
typedef struct { | ||
std::shared_ptr<char> data; | ||
uint32_t size; | ||
uint32_t write_index; | ||
} Packet; | ||
|
||
std::unique_ptr<std::queue<Packet>> buffer_; | ||
size_t max_queue_length_ = 0; | ||
|
||
static const size_t kMaxQueueLength = 1024; | ||
static const size_t kMaxBufferLen = 1024 * 1024; | ||
}; | ||
|
||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.