handle:处理崩溃问题。
This commit is contained in:
		
							parent
							
								
									f9e2662cdb
								
							
						
					
					
						commit
						1648b6c961
					
				@ -106,5 +106,8 @@ int main()
 | 
				
			|||||||
        std::cout << "coptk: " << frame->coptk << std::endl;
 | 
					        std::cout << "coptk: " << frame->coptk << std::endl;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    delete frame;
 | 
				
			||||||
 | 
					    t.join();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										34
									
								
								server.cxx
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								server.cxx
									
									
									
									
									
								
							@ -1,12 +1,18 @@
 | 
				
			|||||||
#include "server.h"
 | 
					#include "server.h"
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Server::Server(asio::io_context& io_context, short port)
 | 
					Server::Server(asio::io_context& io_context, short port) : io_context_(io_context), acceptor_(io_context)
 | 
				
			||||||
    : io_context_(io_context), acceptor_(io_context)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    port_ = port;
 | 
					    port_ = port;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Server::~Server()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    for (auto& client : clients_) {
 | 
				
			||||||
 | 
					        client.second.detach();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Server::start()
 | 
					void Server::start()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port_);
 | 
					    asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port_);
 | 
				
			||||||
@ -50,6 +56,18 @@ void Server::do_accept()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void Server::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, const std::string& client_key)
 | 
					void Server::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, const std::string& client_key)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::shared_ptr<int> deleter(new int(0), [&](int* p) {
 | 
				
			||||||
 | 
					        std::unique_lock<std::mutex> lock(cli_mutex_);
 | 
				
			||||||
 | 
					        delete p;
 | 
				
			||||||
 | 
					        client_map_.erase(client_key);
 | 
				
			||||||
 | 
					        if (clients_.find(client_key) != clients_.end()) {
 | 
				
			||||||
 | 
					            clients_.at(client_key).detach();
 | 
				
			||||||
 | 
					            clients_.erase(client_key);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        std::cout << "th_client deleter client " << client_key << "exit." << std::endl;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    asio::error_code error;
 | 
					    asio::error_code error;
 | 
				
			||||||
    std::shared_ptr<ClientCache> cache = nullptr;
 | 
					    std::shared_ptr<ClientCache> cache = nullptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -84,11 +102,15 @@ void Server::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, con
 | 
				
			|||||||
                    req.type = FrameType::TYPE_RESPONSE_ERROR;
 | 
					                    req.type = FrameType::TYPE_RESPONSE_ERROR;
 | 
				
			||||||
                    send_frame(socket, req);
 | 
					                    send_frame(socket, req);
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
 | 
					                    auto parse = json_->parse(out);
 | 
				
			||||||
                    FrameData req;
 | 
					                    FrameData req;
 | 
				
			||||||
                    req.type = FrameType::TYPE_RESPONSE_SUCCESS;
 | 
					                    req.type = FrameType::TYPE_RESPONSE_SUCCESS;
 | 
				
			||||||
                    req.data = new char[out.size()];
 | 
					                    req.len = parse.message_content.size() + 1;
 | 
				
			||||||
                    req.len = out.size();
 | 
					                    req.data = new char[req.len];
 | 
				
			||||||
                    memcpy(req.data, out.c_str(), out.size());
 | 
					                    req.protk = parse.prompt_tokens;
 | 
				
			||||||
 | 
					                    req.coptk = parse.completion_tokens;
 | 
				
			||||||
 | 
					                    memcpy(req.data, parse.message_content.c_str(), parse.message_content.size());
 | 
				
			||||||
 | 
					                    req.data[req.len - 1] = '\0';
 | 
				
			||||||
                    send_frame(socket, req);
 | 
					                    send_frame(socket, req);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                ask_mutex_.unlock();
 | 
					                ask_mutex_.unlock();
 | 
				
			||||||
@ -114,5 +136,7 @@ bool Server::send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, Fr
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto send_len = socket->send(asio::buffer(send_data, len));
 | 
					    auto send_len = socket->send(asio::buffer(send_data, len));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    delete[] send_data;
 | 
				
			||||||
    return send_len == len;
 | 
					    return send_len == len;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user