Multiclient, but only supporting one?

This commit is contained in:
Erik Zandvliet 2010-06-13 20:50:01 +02:00
parent 9de16fd0d0
commit 3937ec1db9
3 changed files with 87 additions and 57 deletions

View file

@ -5,11 +5,19 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include "buffer.h" #include "buffer.h"
#include "user.h"
#define BUFLEN 1000000 #define BUFLEN 1000000
int get_empty( user ** list, int amount ) {
for (int i = 0; i < amount; i++ ){
if (!list[i]->is_connected()) { return i; }
}
return -1;
}
int main( int argc, char * argv[] ) { int main( int argc, char * argv[] ) {
if (argc != 3) { std::cout << "Not the right amount of arguments!\n"; exit(1);} if (argc != 4) { std::cout << "Not the right amount of arguments!\n"; exit(1);}
int buffers = atoi(argv[1]); int buffers = atoi(argv[1]);
int total_buffersize = atoi(argv[2]); int total_buffersize = atoi(argv[2]);
int size_per_buffer = total_buffersize/buffers; int size_per_buffer = total_buffersize/buffers;
@ -20,9 +28,10 @@ int main( int argc, char * argv[] ) {
ringbuf[i]->data = (char*) malloc(size_per_buffer); ringbuf[i]->data = (char*) malloc(size_per_buffer);
ringbuf[i]->data[0] = i+'a'; ringbuf[i]->data[0] = i+'a';
} }
for (int i = 0; i < buffers; i ++ ) { for (int i = 0; i < buffers; i ++ ) { std::cout << "Buffer[" << i << "][0]: " << ringbuf[i]->data[0] << "\n"; }
std::cout << "Buffer[" << i << "][0]: " << ringbuf[i]->data[0] << "\n"; int connections = atoi(argv[3]);
} user ** connectionList = (user**) calloc (connections,sizeof(user*));
for (int i = 0; i < connections; i++) { connectionList[i] = new user; }
char input[BUFLEN]; char input[BUFLEN];
char header[BUFLEN]; char header[BUFLEN];
int inp_amount; int inp_amount;
@ -31,6 +40,8 @@ int main( int argc, char * argv[] ) {
int position_startframe = 0; int position_startframe = 0;
int frame_bodylength = 0; int frame_bodylength = 0;
int current_buffer = 0; int current_buffer = 0;
int open_connection = -1;
unsigned int loopcount = 0;
SWUnixSocket listener; SWUnixSocket listener;
SWUnixSocket *mySocket = NULL; SWUnixSocket *mySocket = NULL;
SWBaseSocket::SWBaseError BError; SWBaseSocket::SWBaseError BError;
@ -38,16 +49,19 @@ int main( int argc, char * argv[] ) {
listener.bind("/tmp/socketfile"); listener.bind("/tmp/socketfile");
listener.listen(); listener.listen();
listener.set_timeout(1,0); listener.set_timeout(0,50000);
while(true) { while(true) {
loopcount ++;
std::cout << "#" << loopcount << "\n";
inp_amount = fread(&input,1,11,stdin); inp_amount = fread(&input,1,11,stdin);
if (input[0] == 9) { if (input[0] == 9) {
std::cout << "9!!\n"; std::cout << "9!!\n";
if (!mySocket) { open_connection = get_empty(connectionList,connections);
mySocket = (SWUnixSocket *)listener.accept(&BError); if (open_connection != -1) {
if (mySocket) { connectionList[open_connection]->connect( (SWUnixSocket *)listener.accept(&BError) );
mySocket->send(&header[0],13); if (connectionList[open_connection]->is_connected()) {
connectionList[open_connection]->send_msg(&header[0],13,NULL);
} }
} }
} }
@ -58,21 +72,21 @@ int main( int argc, char * argv[] ) {
frame_bodylength += input[3]; frame_bodylength += input[3];
frame_bodylength += (input[2] << 8); frame_bodylength += (input[2] << 8);
frame_bodylength += (input[1] << 16); frame_bodylength += (input[1] << 16);
std::cout << frame_bodylength << "\n";
for (int i = 0; i < frame_bodylength + 4; i++) { for (int i = 0; i < frame_bodylength + 4; i++) {
inp_amount = fread(&input,1,1,stdin); inp_amount = fread(&input,1,1,stdin);
ringbuf[current_buffer]->data[position_current] = input[0]; ringbuf[current_buffer]->data[position_current] = input[0];
position_current ++; position_current ++;
} }
ringbuf[current_buffer]->size = position_current;
std::cout << "Total message read!\n"; std::cout << "Total message read!\n";
if (mySocket) { for (int i = 0; i < connections; i++) {
std::cout << " mySocket: " << mySocket << "\n"; std::cout << "Checking connection " << i << "\n";
if ( mySocket->fsend(&ringbuf[current_buffer]->data[0], position_current, &BError) == -1) { if (connectionList[i]->is_connected()) {
mySocket->disconnect(); std::cout << "Connected...\n";
mySocket->close_fd(); if ( connectionList[i]->myConnection->send(&ringbuf[current_buffer]->data[0], ringbuf[current_buffer]->size, &BError) == -1) {
std::cout << "Disconnected, closed..." << "\n"; std::cout << " -1 :(\n";
mySocket = 0; connectionList[i]->disconnect();
}
} }
} }
current_buffer++; current_buffer++;

47
Server/user.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "user.h"
user::user() {
myBuffer = NULL;
myConnection = NULL;
}
user::~user() {
myConnection->disconnect();
myConnection = NULL;
}
void user::set_buffer(buffer * newBuffer) {
myBuffer = newBuffer;
sent = 0;
}
int user::get_number() {
return myBuffer->number;
}
bool user::complete_send() {
if (sent == myBuffer->size) { return true; }
return false;
}
void user::disconnect() {
if (myConnection) {
myConnection->disconnect();
myConnection = NULL;
}
}
void user::connect(SWUnixSocket * newConnection) {
myConnection = newConnection;
}
bool user::is_connected( ) {
std::cout << " - Checking...:";
if (myConnection) { std::cout << " true\n"; return true; }
std::cout << " false\n";
return false;
}
int user::send_msg(char * message, int length, SWBaseSocket::SWBaseError * BError) {
return myConnection->send(message,length,BError);
}

View file

@ -1,53 +1,22 @@
#pragma once
#include "buffer.h" #include "buffer.h"
#include "sockets/SocketW.h" #include "sockets/SocketW.h"
#include <iostream>
class user{ class user{
public:
user(); user();
~user(); ~user();
void set_buffer(buffer * newBuffer); void set_buffer(buffer * newBuffer);
int get_number(); int get_number();
bool complete_send(); bool complete_send();
void disconnect(); void disconnect();
void connect(SWBaseSocket * newConnection); void connect(SWUnixSocket * newConnection);
private: bool is_connected();
int send_msg(char * message, int length, SWBaseSocket::SWBaseError * BError);
int sent; int sent;
buffer * myBuffer; buffer * myBuffer;
SWBaseSocket * myConnection; SWUnixSocket * myConnection;
private:
};//user };//user
user::user() { }
user::~user() {
myConnection->disconnect();
myConnection = NULL;
}
void user::set_buffer(buffer * newBuffer) {
myBuffer = newBuffer;
sent = 0;
}
int user::get_number() {
return myBuffer->number;
}
bool user::complete_send() {
if (sent == myBuffer->size) { return true; }
return false;
}
void user::disconnect() {
if (myConnection) {
myConnection->disconnect();
myConnection = NULL;
}
}
void user::connect(SWBaseSocket * newConnection) {
myConnection = newConnection;
}
bool user::is_connected( ) {
if (myConnection) { return true; }
return false;
}