Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom proto. #13

Merged
merged 9 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/talkingtree/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.o
*.os
*.pyc
*.pb.h
*.pb.cc
42 changes: 41 additions & 1 deletion modules/talkingtree/SCsub
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
# SCsub
Import('env')


import os.path


env_talkingtree = env.Clone()
env_talkingtree.add_source_files(env.modules_sources,"*.cpp")

talkingtree_dir = '#modules/talkingtree/'
protobuf_proto = 'TalkingTree.proto'

protobuf_suffix = ".proto"
#protobuf_target = env_talkingtree.Command( ['TalkingTree.pb.cc', 'TalkingTree.pb.h'], \
# protobuf_proto, \
# "protoc --proto_path=. --cpp_out=. $SOURCE")
#Depends(,protobuf_target )

def _protobuf_emitter(target, source, env):
target = []
for s in source:
src = str(s)
if src.endswith(protobuf_suffix):
p_stem = src[:-len(protobuf_suffix)]
else:
p_stem = src
target.append(p_stem + '.pb.h')
target.append(p_stem + '.pb.cc')
return target, source

def filter_headers(f_list):
l = []
for f in f_list:
if os.path.splitext(f.rstr())[1] != '.h':
l.append(f)
return l

bld = Builder(action="protoc --proto_path=. --cpp_out=. $SOURCE", \
# suffix = '.pb.cc', \
src_suffix = [protobuf_suffix], \
emitter = _protobuf_emitter)
env_talkingtree['BUILDERS']['Protoc'] = bld
protobuf_target = env_talkingtree.Protoc( protobuf_proto )
sources = Glob("*.cpp")+filter_headers(protobuf_target)
env_talkingtree.add_source_files(env.modules_sources, sources )
env_talkingtree.Append(CXXFLAGS=['-O2', '-std=c++11'])
33 changes: 33 additions & 0 deletions modules/talkingtree/TalkingTree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,36 @@ package TalkingTreeProto;

option optimize_for = SPEED;


message Version {
// 2-byte Major, 1-byte Minor and 1-byte Patch version number.
optional uint32 version = 1;
// Client release name.
optional string release = 2;
// Client OS name.
optional string os = 3;
// Client OS version.
optional string os_version = 4;
}


// Used to send and broadcast text messages.
message TextMessage {
// The message sender, identified by its session.
optional uint32 actor = 1;
// Target users for the message, identified by their session.
repeated uint32 session = 2;
// The channels to which the message is sent, identified by their
// channel_ids.
repeated uint32 channel_id = 3;
// The root channels when sending message recursively to several channels,
// identified by their channel_ids.
repeated uint32 tree_id = 4;
// The UTF-8 encoded message. May be HTML if the server allows.
required string message = 5;
}

message UDPTunnel {
// Not used.
required bytes packet = 1;
}
5 changes: 3 additions & 2 deletions modules/talkingtree/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# config.py

def can_build(platform):
return True
return True

def configure(env):
pass
env.Append(LIBS=['protobuf'])
pass
3 changes: 2 additions & 1 deletion modules/talkingtree/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include "register_types.h"
#include "class_db.h"
#include "talking_tree.h"

void register_talkingtree_types() {
ClassDB::register_class<TalkingTree>();
}

void unregister_talkingtree_types() {
Expand Down
137 changes: 137 additions & 0 deletions modules/talkingtree/talking_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* talking_tree.cpp */

#include "talking_tree.h"
#include "talking_tree_enum.h"

#include "TalkingTree.pb.h"
#include "io/marshalls.h"


void TalkingTree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &TalkingTree::set_network_peer);
ClassDB::bind_method(D_METHOD("is_network_server"), &TalkingTree::is_network_server);
ClassDB::bind_method(D_METHOD("has_network_peer"), &TalkingTree::has_network_peer);
ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &TalkingTree::get_network_connected_peers);
ClassDB::bind_method(D_METHOD("send_text", "message"), &TalkingTree::send_text);
ClassDB::bind_method(D_METHOD("poll"), &TalkingTree::poll);

ADD_SIGNAL(MethodInfo("text_message", PropertyInfo(Variant::STRING, "message"), PropertyInfo(Variant::INT, "sender_id")));
}

TalkingTree::TalkingTree(){
}
void TalkingTree::send_text(String msg) {
TalkingTreeProto::TextMessage txtMsg;
CharString m = msg.utf8();
txtMsg.set_message(m.get_data(), m.length());
_send_packet(0, PacketType::TEXTMESSAGE, txtMsg, NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
}
void TalkingTree::_send_packet(int p_to, PacketType type, google::protobuf::Message &message, NetworkedMultiplayerPeer::TransferMode transferMode){
Vector<uint8_t> packet;
//incorrect
packet.resize(1 + 4 + message.ByteSize());
packet[0] = (uint8_t)type;
encode_uint32(message.ByteSize(), &packet[1]);
message.SerializeToArray( &packet[5], message.ByteSize());
network_peer->set_transfer_mode(transferMode);
network_peer->set_target_peer(0);
network_peer->put_packet(packet.ptr(), packet.size());
}
void TalkingTree::_network_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
PacketType packet_type = (PacketType) p_packet[0];
const uint8_t * proto_packet = &p_packet[5];
int proto_packet_len = p_packet_len - 5;
switch(packet_type){
case PacketType::VERSION: {
} break;
case PacketType::UDPTUNNEL: {
} break;
case PacketType::TEXTMESSAGE: {
TalkingTreeProto::TextMessage txtMsg;
txtMsg.ParseFromArray( proto_packet, proto_packet_len );
String msg;
msg.parse_utf8(txtMsg.message().c_str(), txtMsg.message().length());
this->emit_signal("text_message", msg, p_from);
} break;
default:
break;
}
}

bool TalkingTree::is_network_server() const {

ERR_FAIL_COND_V(!network_peer.is_valid(), false);
return network_peer->is_server();
}

bool TalkingTree::has_network_peer() const {
return network_peer.is_valid();
}

void TalkingTree::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer) {
if (network_peer.is_valid()) {
network_peer->disconnect("peer_connected", this, "_network_peer_connected");
network_peer->disconnect("peer_disconnected", this, "_network_peer_disconnected");
network_peer->disconnect("connection_succeeded", this, "_connected_to_server");
network_peer->disconnect("connection_failed", this, "_connection_failed");
network_peer->disconnect("server_disconnected", this, "_server_disconnected");
connected_peers.clear();
last_send_cache_id = 1;
}

ERR_EXPLAIN("Supplied NetworkedNetworkPeer must be connecting or connected.");
ERR_FAIL_COND(p_network_peer.is_valid() && p_network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED);
network_peer = p_network_peer;
if (network_peer.is_valid()) {
network_peer->connect("peer_connected", this, "_network_peer_connected");
network_peer->connect("peer_disconnected", this, "_network_peer_disconnected");
network_peer->connect("connection_succeeded", this, "_connected_to_server");
network_peer->connect("connection_failed", this, "_connection_failed");
network_peer->connect("server_disconnected", this, "_server_disconnected");
}
}
Vector<int> TalkingTree::get_network_connected_peers() const {
ERR_FAIL_COND_V(!network_peer.is_valid(), Vector<int>());
Vector<int> ret;
for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
ret.push_back(E->get());
}
return ret;
}

void TalkingTree::_network_poll() {

if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED)
return;

network_peer->poll();

if (!network_peer.is_valid()) //it's possible that polling might have resulted in a disconnection, so check here
return;

while (network_peer->get_available_packet_count()) {

int sender = network_peer->get_packet_peer();
const uint8_t *packet;
int len;

Error err = network_peer->get_packet(&packet, len);
if (err != OK) {
ERR_PRINT("Error getting packet!");
}

_network_process_packet(sender, packet, len);

if (!network_peer.is_valid()) {
break; //it's also possible that a packet or RPC caused a disconnection, so also check here
}
}
}

void TalkingTree::poll(){
while(true){
//_timer->set_wait_time(500.0/1000.0);
_network_poll();
//_timer->start();
}
}
36 changes: 36 additions & 0 deletions modules/talkingtree/talking_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* talking_tree.h */
#ifndef TALKING_TREE_H
#define TALKING_TREE_H

#include "reference.h"
#include "io/networked_multiplayer_peer.h"
#include "ustring.h"
#include "talking_tree_enum.h"
#include "scene/main/timer.h"
#include <google/protobuf/message.h>

class TalkingTree : public Reference {
GDCLASS(TalkingTree, Reference);

protected:
static void _bind_methods();

public:
TalkingTree();
void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer);
Vector<int> get_network_connected_peers() const;
bool is_network_server() const;
bool has_network_peer() const;
void send_text(String msg);
void poll();
private:
Set<int> connected_peers;
int last_send_cache_id;
Ref<NetworkedMultiplayerPeer> network_peer;
void _send_packet(int p_to, PacketType type, google::protobuf::Message &message, NetworkedMultiplayerPeer::TransferMode transfer);
void _network_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
void _network_poll();
//Ref<Timer> _timer;
};

#endif
7 changes: 7 additions & 0 deletions modules/talkingtree/talking_tree_enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

enum class PacketType{
VERSION = 0,
TEXTMESSAGE = 1,
UDPTUNNEL = 2
};