Removed old callbacks
Implemented new callback methods Made ReceiveData renamed to ReceiveMessages, returns void amd calls the callback methods
This commit is contained in:
5
DEV_INFO
5
DEV_INFO
@ -1,5 +1,2 @@
|
|||||||
id -1 is server / valid message if other parameters are valid as well
|
id -1 is server / valid message if other parameters are valid as well
|
||||||
id -2 is invalid network message
|
id -2 is invalid network message
|
||||||
|
|
||||||
|
|
||||||
currently working on: TcpClient::SendMessage
|
|
||||||
3
TODO
3
TODO
@ -1,3 +1,4 @@
|
|||||||
initialization code for other operating systems - currently VoidNet only supports windows
|
initialization code for other operating systems - currently VoidNet only supports windows
|
||||||
initialization code for tcp client for other operating systems - "
|
initialization code for tcp client for other operating systems - "
|
||||||
handle SendNetworkMessage errors for windows
|
handle SendNetworkMessage errors for windows
|
||||||
|
handle ReceiveDataArray errors for windows
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -19,7 +19,6 @@
|
|||||||
</ProjectConfiguration>
|
</ProjectConfiguration>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\include\Callbacks.hpp" />
|
|
||||||
<ClInclude Include="..\..\include\Config.hpp" />
|
<ClInclude Include="..\..\include\Config.hpp" />
|
||||||
<ClInclude Include="..\..\include\Defs.hpp" />
|
<ClInclude Include="..\..\include\Defs.hpp" />
|
||||||
<ClInclude Include="..\..\include\Init.hpp" />
|
<ClInclude Include="..\..\include\Init.hpp" />
|
||||||
|
|||||||
@ -39,9 +39,6 @@
|
|||||||
<ClInclude Include="..\..\include\Serializer.hpp">
|
<ClInclude Include="..\..\include\Serializer.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\Callbacks.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\src\Tags.hpp">
|
<ClInclude Include="..\..\src\Tags.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|||||||
@ -1,21 +0,0 @@
|
|||||||
#ifndef CALLBACKS_HPP
|
|
||||||
#define CALLBACKS_HPP
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma once
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "Defs.hpp"
|
|
||||||
#include "Serializer.hpp"
|
|
||||||
|
|
||||||
/*Client Callbacks begin*/
|
|
||||||
|
|
||||||
void OnMessage(uint16 sender, uint16 tag, uint16 subject, void *data);
|
|
||||||
|
|
||||||
void OnDisconnect(uint16 id);
|
|
||||||
|
|
||||||
void OnConnect(uint16 id);
|
|
||||||
|
|
||||||
/*Client Callbacks end*/
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -184,13 +184,11 @@ enum VoidCode
|
|||||||
|
|
||||||
enum DistributionType
|
enum DistributionType
|
||||||
{
|
{
|
||||||
All,
|
All = 1, // Others and Server
|
||||||
AllAndMe,
|
AllAndMe, // Other, Server and Ourselfs
|
||||||
Server,
|
Server,
|
||||||
Others,
|
Others,
|
||||||
ID,
|
ID,
|
||||||
Reply,
|
|
||||||
Custom,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DEFS_HPP
|
#endif // DEFS_HPP
|
||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -28,17 +29,23 @@ public:
|
|||||||
|
|
||||||
VoidCode Connect();
|
VoidCode Connect();
|
||||||
|
|
||||||
NetworkBuffer ReceiveDataArray();
|
void ReceiveMessages();
|
||||||
const NetworkMessage &ReceiveData();
|
|
||||||
void SendMessage(const NetworkMessage &message);
|
void SendMessage(const NetworkMessage &message);
|
||||||
|
|
||||||
|
std::function<void(uint16)> OnDisconnect;
|
||||||
|
std::function<void(uint16)> OnConnect;
|
||||||
|
std::function<void(uint16, uint16, uint16, void*)> OnMessage;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const NetworkBuffer &ReceiveDataArray();
|
||||||
|
static void ReceiveData(TcpClient *client);
|
||||||
static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
|
static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
|
||||||
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
||||||
|
|
||||||
std::string ip;
|
std::string ip;
|
||||||
uint16 port = 0;
|
uint16 port = 0;
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
bool receive = false;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
SOCKET tcp_socket = INVALID_SOCKET;
|
SOCKET tcp_socket = INVALID_SOCKET;
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
#include "Init.hpp"
|
#include "Init.hpp"
|
||||||
#include "TcpClient.hpp"
|
#include "TcpClient.hpp"
|
||||||
#include "Callbacks.hpp"
|
|
||||||
#include "NetworkBuffer.hpp"
|
#include "NetworkBuffer.hpp"
|
||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -86,6 +86,6 @@ const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &b
|
|||||||
tag = buffer.body[6];
|
tag = buffer.body[6];
|
||||||
subject = Utility::BitConverter::ToUint16(buffer.body, 7);
|
subject = Utility::BitConverter::ToUint16(buffer.body, 7);
|
||||||
this->buffer = buffer;
|
this->buffer = buffer;
|
||||||
valid = sender != -2 && tag != CONNECT && tag != DISCONNECT;
|
valid = sender != -2 && tag != CONNECT && tag != DISCONNECT && distribution_mode > 0;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
#include "Utility.hpp"
|
#include "Utility.hpp"
|
||||||
#include "Config.hpp"
|
#include "Config.hpp"
|
||||||
#include "NetworkBuffer.hpp"
|
#include "NetworkBuffer.hpp"
|
||||||
#include "Callbacks.hpp"
|
|
||||||
#include "Tags.hpp"
|
#include "Tags.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -99,9 +98,11 @@ VoidCode TcpClient::Connect()
|
|||||||
uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen);
|
uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen);
|
||||||
if (connect_code == SOCKET_ERROR)
|
if (connect_code == SOCKET_ERROR)
|
||||||
return VOID_COULDNT_CONNECT;
|
return VOID_COULDNT_CONNECT;
|
||||||
|
receive = true;
|
||||||
|
return VOID_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkBuffer TcpClient::ReceiveDataArray()
|
const NetworkBuffer &TcpClient::ReceiveDataArray()
|
||||||
{
|
{
|
||||||
NetworkBuffer buffer;
|
NetworkBuffer buffer;
|
||||||
|
|
||||||
@ -110,7 +111,6 @@ NetworkBuffer TcpClient::ReceiveDataArray()
|
|||||||
if (header_received != 4 || WSAGetLastError() != 0) // this header is completely unrelated to the network message header - this header is the body size of the network message
|
if (header_received != 4 || WSAGetLastError() != 0) // this header is completely unrelated to the network message header - this header is the body size of the network message
|
||||||
{
|
{
|
||||||
// there was a problem receiving the body size of the message or theres no header to receive
|
// there was a problem receiving the body size of the message or theres no header to receive
|
||||||
return NetworkBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.body = new byte[buffer.body_size]();
|
buffer.body = new byte[buffer.body_size]();
|
||||||
@ -118,22 +118,31 @@ NetworkBuffer TcpClient::ReceiveDataArray()
|
|||||||
if (body_received == SOCKET_ERROR || body_received != buffer.body_size || WSAGetLastError() != 0)
|
if (body_received == SOCKET_ERROR || body_received != buffer.body_size || WSAGetLastError() != 0)
|
||||||
{
|
{
|
||||||
//there was a problem receiving the body of the message or theres no body to receive
|
//there was a problem receiving the body of the message or theres no body to receive
|
||||||
return NetworkBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NetworkMessage &TcpClient::ReceiveData()
|
void TcpClient::ReceiveData(TcpClient *client)
|
||||||
{
|
{
|
||||||
NetworkMessage message(ReceiveDataArray());
|
while (client->receive)
|
||||||
if (message.tag == CONNECT)
|
{
|
||||||
OnConnect(message.sender);
|
NetworkMessage message(client->ReceiveDataArray());
|
||||||
else if (message.tag == DISCONNECT)
|
if (message.valid)
|
||||||
OnDisconnect(message.sender);
|
{
|
||||||
else
|
if (message.tag == CONNECT) // some user has connected
|
||||||
OnMessage(message.sender, message.tag, message.subject, message.data);
|
client->OnConnect(message.sender);
|
||||||
return message;
|
else if (message.tag == DISCONNECT) // some user has disconnected
|
||||||
|
client->OnDisconnect(message.sender);
|
||||||
|
else
|
||||||
|
client->OnMessage(message.sender, message.tag, message.subject, message.data); // we received data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpClient::ReceiveMessages()
|
||||||
|
{
|
||||||
|
std::async(std::launch::async, &ReceiveData, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient::SendNetworkMessage(const NetworkMessage &message, TcpClient *client)
|
void TcpClient::SendNetworkMessage(const NetworkMessage &message, TcpClient *client)
|
||||||
|
|||||||
@ -56,19 +56,7 @@ void VoidNetClientAPI::Receive()
|
|||||||
|
|
||||||
void VoidNetClientAPI::ProcessAllData()
|
void VoidNetClientAPI::ProcessAllData()
|
||||||
{
|
{
|
||||||
while (receive)
|
client->ReceiveMessages();
|
||||||
{
|
|
||||||
NetworkMessage message = client->ReceiveDataArray();
|
|
||||||
if (message.valid)
|
|
||||||
{
|
|
||||||
if (message.tag == CONNECT)
|
|
||||||
OnConnect(message.sender);
|
|
||||||
else if (message.tag == DISCONNECT)
|
|
||||||
OnDisconnect(message.sender);
|
|
||||||
else
|
|
||||||
OnMessage(message.sender, message.tag, message.subject, message.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::Disconnect()
|
void VoidNetClientAPI::Disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user