如何解决libcurl与TWS API运行时的数据重叠问题
问题:libcurl发送邮件混入TWS API传输数据
我在C++项目中同时运行libcurl发送邮件与TWS API(连接端口为4002),定期调用libcurl发送邮件时,发现邮件内容混入了TWS API传输的部分数据,目前无法确定导致数据重叠的原因,这是我首次使用libcurl。相关代码如下:
libcurl邮件发送代码
string sender = "senderEmail@gmail.com"; string password = "password!"; string receiver1 = "otheremail@gmail.com"; string receiver2 = "5551234567@msg.fi.google.com"; string latestEmailSubject = "default Subject"; string latestEmailBody = "email Body"; size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp) { // Simply discard the response for simplicity in this example return size * nmemb; } struct upload_status { int lines_read; }; static size_t payload_source(void* ptr, size_t size, size_t nmemb, void* userp) { struct upload_status* upload_ctx = (struct upload_status*)userp; const char* data; if ((size == 0) || (nmemb == 0) || ((size * nmemb) < 1)) { return 0; } cout << "sending" << endl; string senderLine = ("From: " + sender + "\r\n"); string receiverLine = "To: " + receiver1 + "\r\n"; string ccLine = "Cc: " + receiver2 + "\r\n"; string subjectLine = "Subject: " + latestEmailSubject + "\r\n"; string bodyLine = latestEmailBody + "\r\n"; string footerLine = "Sent from Accelerated Investing at " + StockThread::TimeToString(system_clock::now()) + "\r\n"; static const char* payload_text[] = { "Date: Mon, 29 Nov 2023 21:54:29 +1100\r\n", receiverLine.c_str(), senderLine.c_str(), ccLine.c_str(), "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@" "rfcpedant.example.org>\r\n", subjectLine.c_str(), "\r\n", bodyLine.c_str(), "\r\n", footerLine.c_str(), //"Check RFC5322.\r\n", NULL }; data = payload_text[upload_ctx->lines_read]; if (data) { size_t len = strlen(data); memcpy(ptr, data, len); upload_ctx->lines_read++; return len; } cout << "end of payload_source()" << endl; return 0; } void Strategy::SendEmail(string subject, string message) { CURL* curl = curl_easy_init(); struct upload_status upload_ctx; upload_ctx.lines_read = 0; if (curl) { latestEmailSubject = subject; latestEmailBody = message; // Set the SMTP server details curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465"); curl_easy_setopt(curl, CURLOPT_USERNAME, "senderEmail@gmail.com"); // generated password copied from gmail curl_easy_setopt(curl, CURLOPT_PASSWORD, "1234 5678 90ab cdef"); // Set the sender's email address curl_easy_setopt(curl, CURLOPT_MAIL_FROM, sender); // Set the recipient's email address struct curl_slist* recipients = NULL; recipients = curl_slist_append(recipients, receiver1.c_str()); recipients = curl_slist_append(recipients, receiver2.c_str()); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); // Set the email body curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); // Set the callback function to handle the server response curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); cout << "sending:" << endl; cout << "sending subject:" << subject << endl; cout << "sending body:" << message << endl; // Perform the email sending operation CURLcode res = curl_easy_perform(curl); // Check for errors if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } // Cleanup curl_slist_free_all(recipients); curl_easy_cleanup(curl); cout << "end of SendEmail()" << endl; } }
TWS API初始化代码
const char* host = argc > 1 ? argv[1] : ""; int port = argc > 2 ? atoi(argv[2]) : 0; if (port <= 0) port = 4002; string connectOptions = ""; int clientId = 0; unsigned attempt = 0; for (;;) { Strategy client; EClientSocket* socketpointer = client.m_pClient; client.runningLiveTest = testOnlineOrders || exportingOnlineData; if (!client.isConnected()) { client.connect(host, port, clientId); } …
此外,程序还会将一些字符串存入vector,准备发送至spdlog。
内容的提问来源于stack exchange,提问作者grizzlyborg
相关产品推荐
相关产品推荐

