Skip to content

Commit

Permalink
begin
Browse files Browse the repository at this point in the history
  • Loading branch information
Rootive authored Jun 29, 2022
1 parent c82c67e commit cf6cfb8
Show file tree
Hide file tree
Showing 58 changed files with 4,639 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Basics/Noncopyable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ROOTIVE_NONCOPYABLE_H
#define ROOTIVE_NONCOPYABLE_H

namespace Rootive
{

class Noncopyable
{
public:
Noncopyable() = default;
~Noncopyable() = default;
Noncopyable(const Noncopyable &) = delete;
Noncopyable &operator=(const Noncopyable &) = delete;
};

}

#endif
187 changes: 187 additions & 0 deletions Basics/StringView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#ifndef ROOTIVE_STRINGVIEW_H
#define ROOTIVE_STRINGVIEW_H

#include <cstring>
#include <cctype>
#include <string>
#include <vector>

namespace Rootive
{

class StringView
{
protected:
const char *ptr_;
int length_;
public:
StringView() : ptr_(nullptr), length_(0) {}
StringView(const char *str) : ptr_(str), length_(static_cast<int>(strlen(str))) {}
StringView(const char *offset, int len) : ptr_(offset), length_(len) {}
StringView(const std::string &str) : ptr_(str.data()), length_(str.size()) {}

inline bool operator==(const StringView &another) const
{
return (length_ == another.length_) && !memcmp(ptr_, another.ptr_, length_);
}
inline bool operator!=(const StringView &another) const { return !(*this == another); }
#define STRINGVIEW_BINARY_PREDICATE(cmp,auxcmp) \
bool operator cmp (const StringView& another) const \
{ \
int ret = memcmp(ptr_, another.ptr_, length_ < another.length_ ? length_ : another.length_);\
return (ret auxcmp 0) || (ret == 0) && (length_ cmp another.length_); \
}
STRINGVIEW_BINARY_PREDICATE(<, <);
STRINGVIEW_BINARY_PREDICATE(<=, <);
STRINGVIEW_BINARY_PREDICATE(>=, >);
STRINGVIEW_BINARY_PREDICATE(>, >);
#undef STRINGVIEW_BINARY_PREDICATE

int compare(const StringView& another) const
{
int ret = memcmp(ptr_, another.ptr_, length_ < another.length_ ? length_ : another.length_);
if (!ret) { if (length_ < another.length_) { ret = -1; } else if (length_ > another.length_) { ret = 1; } }
return ret;
}
inline const char *begin() const { return ptr_; }
inline int getLength() const { return length_; }
inline bool bEmpty() const { return 0 == length_; }
inline const char *end() const { return ptr_ + length_; }

inline void clear() { ptr_ = nullptr, length_ = 0; }
inline void set(const char *str) { ptr_ = str; length_ = static_cast<int>(strlen(str)); }
inline void set(const char *offset, int len) { ptr_ = offset; length_ = len; }
inline char operator[](int index) const { return ptr_[index]; }

inline void append(int length) { length_ += length; }

std::string toString() const { return std::string(ptr_, length_); }

inline StringView mid(int left, int right) const { return StringView(ptr_ + left, right - left); }
inline StringView mid(int left) const { return mid(left, length_); }

inline void removePrefix(int length) { ptr_ += length; length_ -= length; }
inline void removePrefix(char ch) { while (length_ && ptr_[0] == ch) { removePrefix(1); } }
inline void removeSuffix(int length) { length_ -= length; }
inline void removeSuffix(char ch) { while (length_ && ptr_[length_ - 1] == ch) { removeSuffix(1); } }

inline bool bPrefix(const StringView &arg) const { return (length_ >= arg.length_) && !memcmp(ptr_, arg.ptr_, arg.length_); }

inline int indexOf(char ch, const int index = 0) const
{
int ret;
for (ret = index; ret < length_; ++ret)
{
if (ptr_[ret] == ch) { break; }
}
return ret;
}
inline int indexOfNot(char ch, const int index = 0) const
{
int ret;
for (ret = index; ret < length_; ++ret)
{
if (ptr_[ret] != ch) { break; }
}
return ret;
}
inline int indexOfCRLF() const
{
int ret;
for (ret = 1; ret < length_; ++ret)
{
if (ptr_[ret - 1] == '\r' && ptr_[ret] == '\n')
{ ret = ret - 1; break; }
}
return ret;
}

inline int lastIndexOf(char ch) const
{
int ret;
for (ret = length_ - 1; ret >= 0; --ret)
{
if (ptr_[ret] == ch) { break; }
}
return ret;
}
inline int lastIndexOf(char ch, const int index) const
{
int ret;
for (ret = index; ret >= 0; --ret)
{
if (ptr_[ret] == ch) { break; }
}
return ret;
}
inline int lastIndexOfNot(char ch) const
{
int ret;
for (ret = length_ - 1; ret >= 0; --ret)
{
if (ptr_[ret] != ch) { break; }
}
return ret;
}
inline int lastIndexOfNot(char ch, const int index) const
{
int ret;
for (ret = index; ret >= 0; --ret)
{
if (ptr_[ret] != ch) { break; }
}
return ret;
}

inline StringView cutPrefix(int index)
{
StringView ret(ptr_, index);
removePrefix(index);
return ret;
}
inline bool cutPrefix(char ch, StringView &out)
{
int index = indexOf(ch);
if (index == length_) { return false; }
out = cutPrefix(index);
return true;
}
inline StringView splitPrefix(int index)
{
StringView ret = cutPrefix(index);
removePrefix(1);
return ret;
}
inline bool splitPrefix(char ch, StringView &out)
{
bool ret = cutPrefix(ch, out);
if (ret) { removePrefix(1); }
return ret;
}

std::vector<StringView> split(char ch) const
{
StringView cpy(*this);
int index;
std::vector<StringView> ret;
while ((index = cpy.indexOf(ch)) < cpy.getLength())
{
ret.push_back(cpy.splitPrefix(index));
}
ret.push_back(cpy);
return ret;
}
inline int toInt() const
{
int ret = 0;
if (length_ && (isdigit(ptr_[0]) || ptr_[0] == '-' || ptr_[0] == '.'))
{
ret = std::stoi(std::string(ptr_, length_));
}
return ret;
}
};

}

#endif
42 changes: 42 additions & 0 deletions Basics/Types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ROOTIVE_TYPES_H
#define ROOTIVE_TYPES_H

#include <memory>

namespace Rootive
{

namespace Types
{

template <typename To, typename From>
inline To implicitCast(const From &arg)
{
return arg;
}

template <typename To, typename From>
inline To downCast(const From &arg)
{
if (false)
{
implicitCast<From *, To *>(0);
}
return static_cast<To>(arg);
}

template <typename To, typename From>
inline ::std::shared_ptr<To> down_pointer_cast(const ::std::shared_ptr<From> &arg)
{
if (false)
{
implicitCast<From *, To *>(0);
}
return ::std::static_pointer_cast<To>(arg);
}

}

};

#endif
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.5)

project(Rootive LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(.)
add_executable(Rootive
main.cpp
Basics/Types.h Basics/StringView.h Basics/Noncopyable.h Network/PathSegmentView.h Network/PathSegmentView.cpp
IO/IOBuffer.h IO/IOBuffer.cpp
Thread/EventLoop.h Thread/EventLoop.cpp Thread/EventLoopThread.h Thread/EventLoopThread.cpp
Thread/Channel.h Thread/Channel.cpp Thread/Poller.h Thread/Poller.cpp
Thread/Acceptor.h Thread/Acceptor.cpp Thread/EventLoopThreadPool.h Thread/EventLoopThreadPool.cpp
Network/InetAddress.h Network/InetAddress.cpp
Network/Socket.h Network/Socket.cpp Network/NetworkBuffer.h
TCP/TCPServer.h TCP/TCPServer.cpp TCP/TCPConnection.h TCP/TCPConnection.cpp
Database/SQLConnection.h Database/SQLQuery.h Database/SQLConnectionPool.h
HTTP/HTTPServer.h HTTP/HTTPServer.cpp HTTP/HTTPServerBoot.h HTTP/HTTPServerBoot.cpp
HTTP/HTTPBuilder.h
#Example/SportsServer.h Example/SportsServer.cpp
Json/SQLToJson.h Json/SQLToJson.cpp
ForumServer/ForumServer.h ForumServer/ForumServer.cpp
IO/DoubleBufferingOutput.h IO/DoubleBufferingOutput.cpp Log/LogFile.h IO/OutputFileInterface.h IO/OutputInterface.h
Log/LogLine.h Log/LogLine.cpp
Thread/ThisThread.h Log/LogFile.cpp IO/OutputFile.h
Log/LogLineParse.h Log/LogLineParse.cpp
HTTP/HTTPParse.h HTTP/HTTPParse.cpp
)
target_link_libraries(Rootive pthread)
target_link_libraries(Rootive mariadbclient)


29 changes: 29 additions & 0 deletions Database/SQLConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef ROOTIVE_SQLCONNECTION_H
#define ROOTIVE_SQLCONNECTION_H

#include <mariadb/mysql.h>
#include <mutex>
#include "Basics/Noncopyable.h"
#include "Network/InetAddress.h"

namespace Rootive
{
class SQLQuery;
class SQLConnection : Noncopyable
{
friend class SQLQuery;

MYSQL *mysql_;
std::mutex mutex_;
public:
SQLConnection(const InetAddress &host, const char *user, const char *pw, const char *db, const char *unixSocket, unsigned long clientFlag)
{
mysql_ = mysql_init(nullptr);
if (!mysql_real_connect(mysql_, host.toIPString().c_str(), user, pw, db, host.getPort_HostEndian(), unixSocket, clientFlag)) { throw ; }
}
~SQLConnection() { mysql_close(mysql_); }

};
}

#endif
43 changes: 43 additions & 0 deletions Database/SQLConnectionPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef ROOTIVE_SQLCONNECTIONPOOL_H
#define ROOTIVE_SQLCONNECTIONPOOL_H

#include <vector>
#include <memory>
#include <mutex>
#include "Database/SQLConnection.h"
#include "Basics/Noncopyable.h"

namespace Rootive
{
class SQLConnectionPool : Noncopyable
{
std::vector<std::unique_ptr<SQLConnection> > connectionVector_;
int next_;
std::mutex mutex_;
public:
SQLConnectionPool(const InetAddress &host, const char *user, const char *pw, const char *db, int numConnection = 1) :
next_(0)
{
for (int _i = 0; _i < numConnection; ++_i)
{
connectionVector_.push_back(std::unique_ptr<SQLConnection>(new SQLConnection(host, user, pw, db, nullptr, CLIENT_MULTI_STATEMENTS)));
}
}

SQLConnection *getConnection()
{
SQLConnection *ret = nullptr;
{
std::lock_guard<std::mutex> guard(mutex_);
if (!connectionVector_.empty())
{
ret = connectionVector_[next_++].get();
next_ %= connectionVector_.size();
}
}
return ret;
}
};
}

#endif
Loading

0 comments on commit cf6cfb8

Please sign in to comment.