| | |
| | | #include <thread> |
| | | #include <string> |
| | | |
| | | |
| | | SOCKET SocketManager::socketServer; |
| | | |
| | | ActionCallback SocketManager::actionCallback; |
| | |
| | | |
| | | SocketManager::SocketManager(ActionCallback callback, void* context) { |
| | | |
| | | init(callback,context); |
| | | init(callback, context); |
| | | } |
| | | |
| | | SocketManager::~SocketManager() { |
| | |
| | | if (recv_len > 0) |
| | | { |
| | | cout << "接受到操作指令:" << recvBuff << endl; |
| | | try { |
| | | bool success = actionCallback(string(recvBuff), callbackContext); |
| | | //发送操作结果 |
| | | if (success) |
| | | { |
| | | char buff[] = "OK"; |
| | | int iSend = send(client, buff, sizeof(buff), 0); |
| | | cout << "返回给客户端数据:" << buff << endl; |
| | | } |
| | | else { |
| | | char buff[] = "FAIL"; |
| | | int iSend = send(client, buff, sizeof(buff), 0); |
| | | cout << "返回给客户端数据:" << buff << endl; |
| | | } |
| | | } |
| | | catch (string st) { |
| | | char buff[] = "FAIL"; |
| | | int iSend = send(client, buff, sizeof(buff), 0); |
| | | cout << "设置采集代码出错:" << st << endl; |
| | | } |
| | | |
| | | |
| | | string result = actionCallback(string(recvBuff), callbackContext); |
| | | |
| | | //发送操作结果 |
| | | int iSend = send(client, result.c_str(), result.size(), 0); |
| | | cout << "返回给客户端数据:" << result << endl; |
| | | } |
| | | else { |
| | | cout << "客户端退出" << endl; |