Ringbuffer in structs
This commit is contained in:
parent
966608bb5e
commit
9de16fd0d0
3 changed files with 69 additions and 7 deletions
7
Server/buffer.h
Normal file
7
Server/buffer.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
struct buffer{
|
||||||
|
int number;
|
||||||
|
int size;
|
||||||
|
char * data;
|
||||||
|
};//buffer
|
|
@ -4,6 +4,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include "buffer.h"
|
||||||
|
|
||||||
#define BUFLEN 1000000
|
#define BUFLEN 1000000
|
||||||
|
|
||||||
|
@ -13,13 +14,14 @@ int main( int argc, char * argv[] ) {
|
||||||
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;
|
||||||
std::cout << "Size per buffer: " << size_per_buffer << "\n";
|
std::cout << "Size per buffer: " << size_per_buffer << "\n";
|
||||||
char ** all_buffers = (char**) calloc(buffers,sizeof(char*));
|
buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*));
|
||||||
for (int i = 0; i < buffers; i ++ ) {
|
for (int i = 0; i < buffers; i ++ ) {
|
||||||
all_buffers[i] = (char*) malloc (size_per_buffer);
|
ringbuf[i] = new buffer;
|
||||||
all_buffers[i][0] = i+'a';
|
ringbuf[i]->data = (char*) malloc(size_per_buffer);
|
||||||
|
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]: " << all_buffers[i][0] << "\n";
|
std::cout << "Buffer[" << i << "][0]: " << ringbuf[i]->data[0] << "\n";
|
||||||
}
|
}
|
||||||
char input[BUFLEN];
|
char input[BUFLEN];
|
||||||
char header[BUFLEN];
|
char header[BUFLEN];
|
||||||
|
@ -51,7 +53,7 @@ int main( int argc, char * argv[] ) {
|
||||||
}
|
}
|
||||||
position_current = 0;
|
position_current = 0;
|
||||||
position_startframe = position_current;
|
position_startframe = position_current;
|
||||||
for(int i = 0; i < 11; i++) { all_buffers[current_buffer][position_current] = input[i]; position_current ++; }
|
for(int i = 0; i < 11; i++) { ringbuf[current_buffer]->data[position_current] = input[i]; position_current ++; }
|
||||||
frame_bodylength = 0;
|
frame_bodylength = 0;
|
||||||
frame_bodylength += input[3];
|
frame_bodylength += input[3];
|
||||||
frame_bodylength += (input[2] << 8);
|
frame_bodylength += (input[2] << 8);
|
||||||
|
@ -60,13 +62,13 @@ int main( int argc, char * argv[] ) {
|
||||||
std::cout << frame_bodylength << "\n";
|
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);
|
||||||
all_buffers[current_buffer][position_current] = input[0];
|
ringbuf[current_buffer]->data[position_current] = input[0];
|
||||||
position_current ++;
|
position_current ++;
|
||||||
}
|
}
|
||||||
std::cout << "Total message read!\n";
|
std::cout << "Total message read!\n";
|
||||||
if (mySocket) {
|
if (mySocket) {
|
||||||
std::cout << " mySocket: " << mySocket << "\n";
|
std::cout << " mySocket: " << mySocket << "\n";
|
||||||
if ( mySocket->fsend(&all_buffers[current_buffer][0], position_current, &BError) == -1) {
|
if ( mySocket->fsend(&ringbuf[current_buffer]->data[0], position_current, &BError) == -1) {
|
||||||
mySocket->disconnect();
|
mySocket->disconnect();
|
||||||
mySocket->close_fd();
|
mySocket->close_fd();
|
||||||
std::cout << "Disconnected, closed..." << "\n";
|
std::cout << "Disconnected, closed..." << "\n";
|
||||||
|
|
53
Server/user.h
Normal file
53
Server/user.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#include "buffer.h"
|
||||||
|
#include "sockets/SocketW.h"
|
||||||
|
|
||||||
|
class user{
|
||||||
|
user();
|
||||||
|
~user();
|
||||||
|
void set_buffer(buffer * newBuffer);
|
||||||
|
int get_number();
|
||||||
|
bool complete_send();
|
||||||
|
void disconnect();
|
||||||
|
void connect(SWBaseSocket * newConnection);
|
||||||
|
private:
|
||||||
|
int sent;
|
||||||
|
buffer * myBuffer;
|
||||||
|
SWBaseSocket * myConnection;
|
||||||
|
};//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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue