diff --git a/Server/main.cpp b/Server/main.cpp index c914e475..7af53a85 100644 --- a/Server/main.cpp +++ b/Server/main.cpp @@ -17,20 +17,18 @@ int get_empty( user ** list, int amount ) { } int main( int argc, char * argv[] ) { - if (argc < 3) { - std::cout << "usage: " << argv[0] << " buffers_count max_clients" << std::endl; + if (argc < 2) { + std::cout << "usage: " << argv[0] << " buffers_count" << std::endl; return 1; } int metabuflen = 0; char * metabuffer = 0; int buffers = atoi(argv[1]); - int connections = atoi(argv[2]); buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*)); - user ** connectionList = (user**) calloc (connections,sizeof(user*)); + std::vector connectionList; + std::vector::iterator connIt; for (int i = 0; i < buffers; ++i) ringbuf[i] = new buffer; - for (int i = 0; i < connections; ++i) connectionList[i] = new user; int current_buffer = 0; - int open_connection = -1; int lastproper = 0;//last properly finished buffer number unsigned int loopcount = 0; SWUnixSocket listener(SWBaseSocket::nonblocking); @@ -59,31 +57,25 @@ int main( int argc, char * argv[] ) { } incoming = listener.accept(&BError); if (incoming){ - open_connection = get_empty(connectionList,connections); - if (open_connection != -1) { - connectionList[open_connection]->connect(incoming); - //send the FLV header - std::cout << "Client " << open_connection << " connected." << std::endl; - connectionList[open_connection]->MyBuffer = lastproper; - connectionList[open_connection]->MyBuffer_num = ringbuf[lastproper]->number; - //TODO: Do this more nicely? - if (connectionList[open_connection]->Conn->send(FLVHeader,13,0) != 13){ - connectionList[open_connection]->disconnect(); - std::cout << "Client " << open_connection << " failed to receive the header!" << std::endl; - } - if (connectionList[open_connection]->Conn->send(metabuffer,metabuflen,0) != metabuflen){ - connectionList[open_connection]->disconnect(); - std::cout << "Client " << open_connection << " failed to receive metadata!" << std::endl; - } - std::cout << "Client " << open_connection << " received metadata and header!" << std::endl; - }else{ - std::cout << "New client not connected: no more connections!" << std::endl; - incoming->disconnect(); + connectionList.push_back(user(incoming)); + //send the FLV header + std::cout << "Client connected." << std::endl; + connectionList.back().MyBuffer = lastproper; + connectionList.back().MyBuffer_num = ringbuf[lastproper]->number; + //TODO: Do this more nicely? + if (connectionList.back().Conn->send(FLVHeader,13,0) != 13){ + connectionList.back().disconnect("failed to receive the header!"); + } + if (connectionList.back().Conn->send(metabuffer,metabuflen,0) != metabuflen){ + connectionList.back().disconnect("failed to receive metadata!"); } } ringbuf[current_buffer]->number = loopcount; //send all connections what they need, if and when they need it - for (int i = 0; i < connections; i++) {connectionList[i]->Send(ringbuf, buffers);} + for (connIt = connectionList.begin(); connIt != connectionList.end(); connIt++){ + if (!(*connIt).is_connected){connectionList.erase(connIt);} + (*connIt).Send(ringbuf, buffers); + } //keep track of buffers lastproper = current_buffer; current_buffer++; diff --git a/Server/user.cpp b/Server/user.cpp index 6a15adf9..a0700e03 100644 --- a/Server/user.cpp +++ b/Server/user.cpp @@ -4,10 +4,9 @@ class user{ public: - user(); + user(SWBaseSocket * newConn); ~user(); - void disconnect(); - void connect(SWBaseSocket * newConnection); + void disconnect(std::string reason); void Send(buffer ** ringbuf, int buffers); bool is_connected; SWUnixSocket * Conn; @@ -16,29 +15,21 @@ class user{ };//user -user::user() { - Conn = NULL; - is_connected = false; +user::user(SWBaseSocket * newConn) { + Conn = (SWUnixSocket*)newConn; + is_connected = (Conn != 0); } -user::~user() { - Conn->disconnect(); - Conn = NULL; - is_connected = false; -} +user::~user(){disconnect("Destroying object");} -void user::disconnect() { +void user::disconnect(std::string reason) { if (Conn) { Conn->disconnect(); Conn = NULL; + std::cout << "Disconnected user: " << reason << std::endl; } - std::cout << "Disconnected user" << std::endl; is_connected = false; } -void user::connect(SWBaseSocket * newConn) { - Conn = (SWUnixSocket*)newConn; - is_connected = (Conn != 0); -} void user::Send(buffer ** ringbuf, int buffers){ //not connected? cancel @@ -49,13 +40,13 @@ void user::Send(buffer ** ringbuf, int buffers){ if (MyBuffer_num < 0){return;} //buffer number changed? disconnect if ((ringbuf[MyBuffer]->number != MyBuffer_num)){ - disconnect(); + disconnect("Buffer number changed (connection too slow)"); return; } SWBaseSocket::SWBaseError err; int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, ringbuf[MyBuffer]->FLV->len, &err); if ((err != SWBaseSocket::ok) && (err != SWBaseSocket::notReady)){ - disconnect(); + disconnect("Socket error"); return; } if (ret == ringbuf[MyBuffer]->FLV->len){