Betere server
This commit is contained in:
parent
01834792d8
commit
a9d10c5a72
2 changed files with 29 additions and 46 deletions
|
@ -17,20 +17,18 @@ int get_empty( user ** list, int amount ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char * argv[] ) {
|
int main( int argc, char * argv[] ) {
|
||||||
if (argc < 3) {
|
if (argc < 2) {
|
||||||
std::cout << "usage: " << argv[0] << " buffers_count max_clients" << std::endl;
|
std::cout << "usage: " << argv[0] << " buffers_count" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int metabuflen = 0;
|
int metabuflen = 0;
|
||||||
char * metabuffer = 0;
|
char * metabuffer = 0;
|
||||||
int buffers = atoi(argv[1]);
|
int buffers = atoi(argv[1]);
|
||||||
int connections = atoi(argv[2]);
|
|
||||||
buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*));
|
buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*));
|
||||||
user ** connectionList = (user**) calloc (connections,sizeof(user*));
|
std::vector<user> connectionList;
|
||||||
|
std::vector<user>::iterator connIt;
|
||||||
for (int i = 0; i < buffers; ++i) ringbuf[i] = new buffer;
|
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 current_buffer = 0;
|
||||||
int open_connection = -1;
|
|
||||||
int lastproper = 0;//last properly finished buffer number
|
int lastproper = 0;//last properly finished buffer number
|
||||||
unsigned int loopcount = 0;
|
unsigned int loopcount = 0;
|
||||||
SWUnixSocket listener(SWBaseSocket::nonblocking);
|
SWUnixSocket listener(SWBaseSocket::nonblocking);
|
||||||
|
@ -59,31 +57,25 @@ int main( int argc, char * argv[] ) {
|
||||||
}
|
}
|
||||||
incoming = listener.accept(&BError);
|
incoming = listener.accept(&BError);
|
||||||
if (incoming){
|
if (incoming){
|
||||||
open_connection = get_empty(connectionList,connections);
|
connectionList.push_back(user(incoming));
|
||||||
if (open_connection != -1) {
|
//send the FLV header
|
||||||
connectionList[open_connection]->connect(incoming);
|
std::cout << "Client connected." << std::endl;
|
||||||
//send the FLV header
|
connectionList.back().MyBuffer = lastproper;
|
||||||
std::cout << "Client " << open_connection << " connected." << std::endl;
|
connectionList.back().MyBuffer_num = ringbuf[lastproper]->number;
|
||||||
connectionList[open_connection]->MyBuffer = lastproper;
|
//TODO: Do this more nicely?
|
||||||
connectionList[open_connection]->MyBuffer_num = ringbuf[lastproper]->number;
|
if (connectionList.back().Conn->send(FLVHeader,13,0) != 13){
|
||||||
//TODO: Do this more nicely?
|
connectionList.back().disconnect("failed to receive the header!");
|
||||||
if (connectionList[open_connection]->Conn->send(FLVHeader,13,0) != 13){
|
}
|
||||||
connectionList[open_connection]->disconnect();
|
if (connectionList.back().Conn->send(metabuffer,metabuflen,0) != metabuflen){
|
||||||
std::cout << "Client " << open_connection << " failed to receive the header!" << std::endl;
|
connectionList.back().disconnect("failed to receive metadata!");
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ringbuf[current_buffer]->number = loopcount;
|
ringbuf[current_buffer]->number = loopcount;
|
||||||
//send all connections what they need, if and when they need it
|
//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
|
//keep track of buffers
|
||||||
lastproper = current_buffer;
|
lastproper = current_buffer;
|
||||||
current_buffer++;
|
current_buffer++;
|
||||||
|
|
|
@ -4,10 +4,9 @@
|
||||||
|
|
||||||
class user{
|
class user{
|
||||||
public:
|
public:
|
||||||
user();
|
user(SWBaseSocket * newConn);
|
||||||
~user();
|
~user();
|
||||||
void disconnect();
|
void disconnect(std::string reason);
|
||||||
void connect(SWBaseSocket * newConnection);
|
|
||||||
void Send(buffer ** ringbuf, int buffers);
|
void Send(buffer ** ringbuf, int buffers);
|
||||||
bool is_connected;
|
bool is_connected;
|
||||||
SWUnixSocket * Conn;
|
SWUnixSocket * Conn;
|
||||||
|
@ -16,29 +15,21 @@ class user{
|
||||||
};//user
|
};//user
|
||||||
|
|
||||||
|
|
||||||
user::user() {
|
user::user(SWBaseSocket * newConn) {
|
||||||
Conn = NULL;
|
Conn = (SWUnixSocket*)newConn;
|
||||||
is_connected = false;
|
is_connected = (Conn != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
user::~user() {
|
user::~user(){disconnect("Destroying object");}
|
||||||
Conn->disconnect();
|
|
||||||
Conn = NULL;
|
|
||||||
is_connected = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void user::disconnect() {
|
void user::disconnect(std::string reason) {
|
||||||
if (Conn) {
|
if (Conn) {
|
||||||
Conn->disconnect();
|
Conn->disconnect();
|
||||||
Conn = NULL;
|
Conn = NULL;
|
||||||
|
std::cout << "Disconnected user: " << reason << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << "Disconnected user" << std::endl;
|
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
}
|
}
|
||||||
void user::connect(SWBaseSocket * newConn) {
|
|
||||||
Conn = (SWUnixSocket*)newConn;
|
|
||||||
is_connected = (Conn != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void user::Send(buffer ** ringbuf, int buffers){
|
void user::Send(buffer ** ringbuf, int buffers){
|
||||||
//not connected? cancel
|
//not connected? cancel
|
||||||
|
@ -49,13 +40,13 @@ void user::Send(buffer ** ringbuf, int buffers){
|
||||||
if (MyBuffer_num < 0){return;}
|
if (MyBuffer_num < 0){return;}
|
||||||
//buffer number changed? disconnect
|
//buffer number changed? disconnect
|
||||||
if ((ringbuf[MyBuffer]->number != MyBuffer_num)){
|
if ((ringbuf[MyBuffer]->number != MyBuffer_num)){
|
||||||
disconnect();
|
disconnect("Buffer number changed (connection too slow)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SWBaseSocket::SWBaseError err;
|
SWBaseSocket::SWBaseError err;
|
||||||
int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, ringbuf[MyBuffer]->FLV->len, &err);
|
int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, ringbuf[MyBuffer]->FLV->len, &err);
|
||||||
if ((err != SWBaseSocket::ok) && (err != SWBaseSocket::notReady)){
|
if ((err != SWBaseSocket::ok) && (err != SWBaseSocket::notReady)){
|
||||||
disconnect();
|
disconnect("Socket error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ret == ringbuf[MyBuffer]->FLV->len){
|
if (ret == ringbuf[MyBuffer]->FLV->len){
|
||||||
|
|
Loading…
Add table
Reference in a new issue