void SendObject(connection & s, amf3object & object) { try { if (serverstatus == 0) return; char buffer[15000]; int length = 0; amf3writer * writer; writer = new amf3writer(buffer+4); writer->Write(object); (*(int*)buffer) = length = writer->position; ByteSwap(*(int*)buffer); s.write(buffer, length+4); delete writer; } catch (std::exception& e) { std::cerr << "exception: " << __FILE__ << " @ " << __LINE__ << "\n"; std::cerr << e.what() << "\n"; } }
void linker:: link ( connection& a, connection& b ) { // make sure requires clause is not broken DLIB_CASSERT( this->is_running() == false , "\tvoid linker::link" << "\n\tis_running() == " << this->is_running() << "\n\tthis: " << this ); running_mutex.lock(); running = true; running_mutex.unlock(); cons_mutex.lock(); A = &a; B = &b; cons_mutex.unlock(); service_connection_running_mutex.lock(); service_connection_running = true; service_connection_running_mutex.unlock(); service_connection_error_mutex.lock(); service_connection_error = false; service_connection_error_mutex.unlock(); // if we fail to make the thread if (!create_new_thread(service_connection,this)) { a.shutdown(); b.shutdown(); service_connection_running_mutex.lock(); service_connection_running = false; service_connection_running_mutex.unlock(); cons_mutex.lock(); A = 0; B = 0; cons_mutex.unlock(); running_mutex.lock(); running = false; running_mutex.unlock(); throw dlib::thread_error ( ECREATE_THREAD, "failed to make new thread in linker::link()" ); } // forward data from a to b char buf[200]; int status; bool error = false; // becomes true if one of the connections returns an error while (true) { status = a.read(buf,sizeof(buf)); // if there was an error reading from the socket if (status == OTHER_ERROR) { error = true; break; } else if (status == SHUTDOWN) { b.shutdown(); } if (status <= 0) { // if a has closed normally if (status == 0) b.shutdown_outgoing(); break; } status = b.write(buf,status); // if there was an error writing to the socket then break if (status == OTHER_ERROR) { error = true; break; } if (status <= 0) break; } // if there was an error then shutdown both connections if (error) { a.shutdown(); b.shutdown(); } // wait for the other thread to end service_connection_running_mutex.lock(); while(service_connection_running) { service_connection_running_signaler.wait(); } service_connection_running_mutex.unlock(); // make sure connections are shutdown a.shutdown(); b.shutdown(); // both threads have ended so the connections are no longer needed cons_mutex.lock(); A = 0; B = 0; cons_mutex.unlock(); // if service_connection terminated due to an error then set error to true service_connection_error_mutex.lock(); if (service_connection_error) error = true; service_connection_error_mutex.unlock(); // if we are ending because of an error if (error) { // signal that the link() function is ending running_mutex.lock(); running = false; running_signaler.broadcast(); running_mutex.unlock(); // throw the exception for this error throw dlib::socket_error ( ECONNECTION, "a connection returned an error in linker::link()" ); } // signal that the link() function is ending running_mutex.lock(); running = false; running_signaler.broadcast(); running_mutex.unlock(); }