/*************************************************************************************************************
* This file contains sending datas function
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "SendData.h"

// Local static variables
static float sendmbytes_float;
static char sendbytes_tmp[TEMP_SIZE_MEDIUM];
static char sendmbytes_tmp[TEMP_SIZE_MEDIUM];
static char sendmbytes_tmp_short[TEMP_SIZE_MEDIUM];

// Send some data throught socket to client
void function_send_data (_client *current_client, char *buffer, int size)
{
	// Store the return information about the sending
	current_client->return_value = send(current_client->socket, buffer, size, 0);

	// Calculate bytes
	bytes_sent += current_client->return_value;

	// Calculate megabytes and short it
	if(current_client->return_value != 0)
	{
		sendmbytes_float = mbytes_sent + ((float)current_client->return_value / 1000000.0f);
		mbytes_sent = sendmbytes_float;
		sprintf_s(sendmbytes_tmp, TEMP_SIZE_MEDIUM, "%f", mbytes_sent);
		memcpy(sendmbytes_tmp_short, sendmbytes_tmp, 3);
	}

	// Update label
	memset(sendbytes_tmp, 0, TEMP_SIZE_MEDIUM);
	sprintf_s(sendbytes_tmp, TEMP_SIZE_MEDIUM, "<b>Sent:</b> <span foreground=\"blue\">%s</span> mbytes (<span foreground=\"blue\">%d</span> bytes)", sendmbytes_tmp_short, bytes_sent);
	gtk_label_set_markup (GTK_LABEL (server_datalabel), sendbytes_tmp);

	// Check for errors
	if (( current_client->return_value == SOCKET_ERROR ) || ( current_client->return_value != size )  || ( current_client->return_value == INVALID_SOCKET ))
	{
		// Write an error message
		memset(warnings_tmp, 0, WARNING_TMP_SIZE);
		sprintf_s(warnings_tmp, WARNING_TMP_SIZE, "Can't send data for [%s]! Mby hard offline. Disconnect!", current_client->nick_name);
		function_show_warning(warnings_tmp);

		// Disconnect client
		function_disconnect_client ( current_client );
	}
}