Skip to content

Commit

Permalink
ini
Browse files Browse the repository at this point in the history
  • Loading branch information
TIME-GATE committed Sep 29, 2017
0 parents commit a67c715
Show file tree
Hide file tree
Showing 30 changed files with 16,849 additions and 0 deletions.
Empty file added Makefile
Empty file.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### 简介
```text
业余时间用libuv写的一个CTP服务器,包含:
1、通信协议转换: TCP CTP(量化交易) HTTP/HTTPS
2、数据协议: protobuff、JSON、XML、stream
3、并发控制: 多线程与多进程 epoll 定时器 信号
```
### 项目框架

### 性能测试

### 欢迎交流
Empty file added data/README.md
Empty file.
Binary file added data/traderDialogRsp.con
Binary file not shown.
Binary file added data/traderPrivate.con
Binary file not shown.
Binary file added data/traderPublic.con
Binary file not shown.
Binary file added data/traderQueryRsp.con
Binary file not shown.
Binary file added data/traderTradingDay.con
Binary file not shown.
51 changes: 51 additions & 0 deletions dependencies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### 项目依赖安装 LINUX环境

#### 1 LIBUV

```text
sudo apt-get install automake
sudo apt-get install libtool
git clone https://github.com/libuv/libuv.git && cd libuv
sh autogen.sh
./configure
make && make check
sudo make install
```

#### 2 PROTOBUFF

```text
git clone https://github.com/google/protobuf.git
cd protobuf && ./configure
make && make check
sudo make install && sudo ldconfig
```

#### 3 交易所SDK

```text
```

#### 4 MYSQL

```text
```

#### 5 REDIS

```text
```
Empty file added logs/README.md
Empty file.
Binary file added src/a.out
Binary file not shown.
188 changes: 188 additions & 0 deletions src/database/MysqlEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <string.h>

#include "base/base.h"
#include "mysqlengine.h"

MysqlEngine::MysqlEngine( const std::string & host, uint16_t port )
: m_Host( host ),
m_Port( port ),
m_DBHandler( NULL ){}

MysqlEngine::~MysqlEngine(){}

void MysqlEngine::selectdb( const std::string & database ) {
m_Database = database;

if ( m_DBHandler != NULL ) {
mysql_select_db( m_DBHandler, m_Database.c_str() );
}
}

void MysqlEngine::setCharsets( const std::string & charsets ) {
m_Charsets = charsets;
}

void MysqlEngine::setToken( const std::string & user, const std::string & passwd ) {
m_Username = user;
m_Password = passwd;
}

bool MysqlEngine::initialize() {
// 初始化句柄
m_DBHandler = mysql_init( NULL );

if ( m_DBHandler == NULL ) {
return false;
}

// 设置属性
bool flags = true;
int32_t timeout = 0;

// 尝试重新连接
mysql_options( m_DBHandler, MYSQL_OPT_RECONNECT, (const char *)&flags );

// 设置字符集
mysql_options( m_DBHandler, MYSQL_SET_CHARSET_NAME, m_Charsets.c_str() );

// 连接超时
timeout = eMysqlEngine_ConnectTimeout;

mysql_options( m_DBHandler, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout );

// 读超时
timeout = eMysqlEngine_ReadTimeout;
mysql_options( m_DBHandler, MYSQL_OPT_READ_TIMEOUT, (const char *)&timeout );

// 写超时
timeout = eMysqlEngine_WriteTimeout;
mysql_options( m_DBHandler, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&timeout );

// 连接MySQL服务器
if ( mysql_real_connect( m_DBHandler, m_Host.c_str(), m_Username.c_str(), m_Password.c_str(), m_Database.c_str(), m_Port, NULL, 0 ) == NULL ) {
mysql_close( m_DBHandler );
m_DBHandler = NULL;
return false;
}

return true;
}

void MysqlEngine::finalize() {
if ( m_DBHandler != NULL ) {
mysql_close( m_DBHandler );
m_DBHandler = NULL;
}
}

void MysqlEngine::keepalive() {

int32_t rc = 0;

rc = mysql_ping( m_DBHandler );
if ( rc != 0 ) {
LOG_ERROR( "MysqlEngine::keepalive() : Result:%d, Error:(%d,'%s') .\n", rc, mysql_errno(m_DBHandler), mysql_error(m_DBHandler) );
}
}

void MysqlEngine::escape( const std::string & src, std::string & dst ) {

// 重置dst长度
dst.resize( src.size() * 2 + 1 );

char * to = const_cast<char *>( dst.data() );
size_t len = mysql_real_escape_string(
m_DBHandler,
to, (const char *)src.data(), src.size() );

dst.resize( len );
}

bool MysqlEngine::insert( const std::string & sqlcmd, uint64_t & insertid ) {

int32_t rc = 0;

rc = mysql_real_query( m_DBHandler, sqlcmd.data(), sqlcmd.size() );

if ( rc != 0 ) {
LOG_ERROR( "MysqlEngine::insert(SQLCMD:'%s') : Result:%d, Error:(%d,'%s') .\n",
sqlcmd.c_str(), rc, mysql_errno(m_DBHandler), mysql_error(m_DBHandler) );
return false;
}

// 获取返回的自增ID
insertid = mysql_insert_id( m_DBHandler );

LOG_TRACE( "MysqlEngine::insert(SQLCMD:'%s') : InsertID:%lu .\n", sqlcmd.c_str(), insertid );
return true;
}

bool MysqlEngine::query( const std::string & sqlcmd, Results & results ) {

int32_t rc = 0;
rc = mysql_real_query( m_DBHandler, sqlcmd.data(), sqlcmd.size() );
if ( rc != 0 ) {
LOG_ERROR( "MysqlEngine::query(SQLCMD:'%s') : Result:%d, Error:(%d,'%s') .\n",
sqlcmd.c_str(), rc, mysql_errno(m_DBHandler), mysql_error(m_DBHandler) );
return false;
}

MYSQL_RES * result = mysql_store_result( m_DBHandler );
if ( result == NULL ) {
LOG_ERROR( "MysqlEngine::query(SQLCMD:'%s') : Store Result Failed .\n", sqlcmd.c_str() );
return false;
}

MYSQL_ROW row;
uint32_t nfields = mysql_num_fields( result );
while ( (row = mysql_fetch_row(result)) != NULL ) {
Result tmpresult;
uint64_t * lengths = mysql_fetch_lengths( result );

for ( uint32_t i = 0; i < nfields; ++i ) {
tmpresult.push_back( std::string( row[i], lengths[i] ) );
}

results.push_back( tmpresult );
}

mysql_free_result( result );
LOG_TRACE( "MysqlEngine::query(SQLCMD:'%s') : ResultCount:%lu .\n", sqlcmd.c_str(), results.size() );

return true;
}

bool MysqlEngine::update( const std::string & sqlcmd, uint32_t & naffected ) {
int32_t rc = 0;

rc = mysql_real_query( m_DBHandler, sqlcmd.data(), sqlcmd.size() );
if ( rc != 0 ) {
LOG_ERROR( "MysqlEngine::update(SQLCMD:'%s') : Result:%d, Error:(%d,'%s') .\n",
sqlcmd.c_str(), rc, mysql_errno(m_DBHandler), mysql_error(m_DBHandler) );

return false;
}

// 获取影响的行数
naffected = mysql_affected_rows( m_DBHandler );
LOG_TRACE( "MysqlEngine::update(SQLCMD:'%s') : AffectedRows:%u .\n", sqlcmd.c_str(), naffected );

return true;
}

bool MysqlEngine::remove( const std::string & sqlcmd, uint32_t & naffected ) {
int32_t rc = 0;

rc = mysql_real_query( m_DBHandler, sqlcmd.data(), sqlcmd.size() );
if ( rc != 0 ) {
LOG_ERROR( "MysqlEngine::remove(SQLCMD:'%s') : Result:%d, Error:(%d,'%s') .\n",
sqlcmd.c_str(), rc, mysql_errno(m_DBHandler), mysql_error(m_DBHandler) );
return false;
}

// 获取影响的行数
naffected = mysql_affected_rows( m_DBHandler );
LOG_TRACE( "MysqlEngine::remove(SQLCMD:'%s') : AffectedRows:%u .\n", sqlcmd.c_str(), naffected );

return true;
}
73 changes: 73 additions & 0 deletions src/database/MysqlEngine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef __SRC_DATASERVERS_MYSQLENGINE_H__
#define __SRC_DATASERVERS_MYSQLENGINE_H__

#include <vector>
#include <string>

#include <stdint.h>
#include <mysql/mysql.h>

// 结果集
typedef std::vector<std::string> Result;
typedef std::vector<Result> Results;

class MysqlEngine {
public :
MysqlEngine(const std::string & host, uint16_t port);
~MysqlEngine();

public :
// 选择数据库
void selectdb( const std::string & database );

// 设置字符集
void setCharsets( const std::string & charsets );

// 设置访问权限
void setToken( const std::string & user, const std::string & passwd );

public :
// 初始化
bool initialize();

// 销毁
void finalize();

// 保活
void keepalive();

// 转义
void escape( const std::string & src, std::string & dst );

// 插入
bool insert( const std::string & sqlcmd, uint64_t & insertid );

// 查询
bool query( const std::string & sqlcmd, Results & results );

// 更新
bool update( const std::string & sqlcmd, uint32_t & naffected );

// 删除
bool remove( const std::string & sqlcmd, uint32_t & naffected );

private :
enum {
eMysqlEngine_ConnectTimeout = 5, // 连接超时时间
eMysqlEngine_ReadTimeout = 5, // 读超时时间
eMysqlEngine_WriteTimeout = 5, // 写超时时间
};

private :
std::string m_Host; // 主机地址
uint16_t m_Port; // 端口号
std::string m_Username; // 用户名
std::string m_Password; // 密码
std::string m_Database; // 数据库名
std::string m_Charsets; // 字符集

private :
MYSQL * m_DBHandler; // 数据库句柄
};

#endif
Loading

0 comments on commit a67c715

Please sign in to comment.