I referred to a python program about this function, but I can't understand how it works. In my opinion, it just sends the packet to a connection which is from 'pymysql.connect'. How can I realize this function using C++;
self._stream_connection.wfile.write(packet)
#the packet is data which maintains the payload
I did some attempts as follow:
// slave.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sys/socket.h>
/*
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
*/
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace sql;
using namespace std;
#define DBHOST "tcp://0.0.0.0:3306"
#define USER "slave1"
#define PASSWORD "125555"
typedef unsigned char BYTE;
struct Payload {
BYTE cmd[1];//default 12 COM_BINLOG_DUMP
BYTE bpos[4];//
BYTE flag[2];//0
BYTE s_id[4];//1
BYTE bfile[16]; //mysql - bin.000002
};
void intTobyte(BYTE bytes[], int n, int len) {
for (int i = 0; i < len; i++) {
bytes[i] = n & 0x000000ff << 8*i;
}
}
void stringTobyte(BYTE bytes[], string str, int len) {
char chs[50];
strcpy(chs, str.c_str());
for (int i = 0; i < len; i++) {
bytes[i] = (int)chs[i] & 0x000000ff << 8 * 0;
}
}
int main() {
Driver* driver;
Connection* conn;
Statement *state;
ResultSet* result;
driver = get_driver_instance();
conn = driver->connect(DBHOST, USER, PASSWORD);
string binlog_file;
int binlog_pos;
struct Payload *payload;
state = conn->createStatement();
result = state->executeQuery("show master status;");
while (result->next() != NULL)
{
binlog_file = result->getString("File");
binlog_pos = result->getInt("Position");
cout << binlog_file << endl << binlog_pos << endl;
}
intTobyte(payload->cmd, 12, 1);
intTobyte(payload->bpos,binlog_pos,4);
intTobyte(payload->flag, 0, 2);
intTobyte(payload->s_id, 1, 4);
stringTobyte(payload->bfile, binlog_file, binlog_file.length());
printf("%0x\n", payload->cmd[0]);
printf("%0x %0x %0x %0x\n", payload->bpos[0], payload->bpos[1], payload->bpos[2], payload->bpos[3]);
//printf("%0x %0x %0x %0x\n", payload.s_id[0], payload.s_id[1], payload.s_id[2], payload.s_id[3]);
//printf("%0x %0x %0x %0x\n", payload.bfile[0], payload.bfile[1], payload.bfile[2], payload.bfile[3]);
send((socket)conn, (char*)payload, 27, 0);//this sentence goes wrong.
delete conn;
driver = NULL;
conn = NULL;
return 0;
}
this is the error information from g++:
register_slave.cpp:87:42: error: invalid conversion from ‘int (*)(int, int, int) throw ()’ to ‘int’ [-fpermissive]
send((socket)conn, (char*)payload, 27, 0);
If you have any good thoughts about this question, please leave your comments, thanks in advance. Or if you have some advices, please let me know as well.
User contributions licensed under CC BY-SA 3.0