diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-12 20:36:08 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-12 20:36:08 +0200 |
commit | 1d41574c6e8e7bbf3705645feb429df6281ccb83 (patch) | |
tree | d0bf256321aef49dd386a9c459104c2fb019ca64 /proto_lib | |
parent | c98a83a1591b1bcb58b3ed29bdbfb1446948f466 (diff) |
working on service support
Diffstat (limited to 'proto_lib')
-rw-r--r-- | proto_lib/api_protocol.h | 16 | ||||
-rw-r--r-- | proto_lib/lib.project | 1 | ||||
-rw-r--r-- | proto_lib/packet.cpp | 45 | ||||
-rw-r--r-- | proto_lib/packet.h | 4 | ||||
-rw-r--r-- | proto_lib/utilities.cpp | 13 | ||||
-rw-r--r-- | proto_lib/utilities.h | 5 |
6 files changed, 66 insertions, 18 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h index 88195e2..2e42d42 100644 --- a/proto_lib/api_protocol.h +++ b/proto_lib/api_protocol.h @@ -17,6 +17,7 @@ #ifndef API_PROTOCOL_H #define API_PROTOCOL_H +namespace proto { struct service_s { @@ -28,8 +29,13 @@ struct service_s std::list<cmd> cmds; }; -enum packet_type {AUTH_REPLY, AUTH_REQUEST, SERVICES_REPLY, SERVICES_REQUEST, COMMAND_REPLY, COMMAND_REQUEST, UNKNOWN}; -enum status {failure = 0x00, success = 0x01}; +struct svc_cmd +{ + std::string service, command; +}; + +enum packet_type {TYPE_AUTH_REPLY, TYPE_AUTH_REQUEST, TYPE_SERVICES_REPLY, TYPE_SERVICES_REQUEST, TYPE_COMMAND_REPLY, TYPE_COMMAND_REQUEST, TYPE_CUSTOM, TYPE_UNKNOWN}; +enum status {STATUS_FAILURE = 0x00, STATUS_SUCCESS = 0x01}; class packet { @@ -47,7 +53,7 @@ public: //helper functions: //client functions - static packet *cli_make_auth_packet(); //should be first packet to server + static packet *cli_make_init_packet(); //should be first packet to server static packet *cli_make_command_packet(std::string &service, std::string &command); static packet *cli_make_request_services_packet(); @@ -58,6 +64,7 @@ public: static bool serv_validate_client_proto(packet&); //false on fail static packet *serv_make_services_packet(std::list<service_s>&); static packet *serv_make_command_reply_packet(std::string&, status s); + static svc_cmd serv_extract_command(packet&); //generic static packet *make_status_packet(packet_type type, status s); @@ -66,7 +73,6 @@ private: std::vector<unsigned char> data; }; - - +}; #endif diff --git a/proto_lib/lib.project b/proto_lib/lib.project index 39a0a37..65be5d8 100644 --- a/proto_lib/lib.project +++ b/proto_lib/lib.project @@ -73,6 +73,7 @@ <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp index 8cf73b2..0ed3150 100644 --- a/proto_lib/packet.cpp +++ b/proto_lib/packet.cpp @@ -22,7 +22,8 @@ #include "packet.h" #include "utilities.h" - +namespace proto +{ packet::packet(std::vector<unsigned char>& new_data) { @@ -41,6 +42,11 @@ bool packet::assign(std::vector<unsigned char>& v) return is_good(); } +const std::vector<unsigned char> &packet::raw() +{ + return data; +} + bool packet::is_good() { if(data.empty()) @@ -88,12 +94,12 @@ packet_type packet::get_type() type.push_back(*i); bool cli = is_client_packet(); if(std::search(type.begin(), type.end(), type_auth, type_auth + sizeof(type_auth)) != type.end()) - return cli?AUTH_REQUEST:AUTH_REPLY; + return cli?TYPE_AUTH_REQUEST:TYPE_AUTH_REPLY; if(std::search(type.begin(), type.end(), type_services, type_services + sizeof(type_services)) != type.end()) - return cli?SERVICES_REQUEST:SERVICES_REPLY; + return cli?TYPE_SERVICES_REQUEST:TYPE_SERVICES_REPLY; if(std::search(type.begin(), type.end(), type_command, type_command + sizeof(type_command)) != type.end()) - return cli?COMMAND_REQUEST:COMMAND_REPLY; - return UNKNOWN; + return cli?TYPE_COMMAND_REQUEST:TYPE_COMMAND_REPLY; + return TYPE_UNKNOWN; } bool packet::is_status_packet() @@ -101,13 +107,13 @@ bool packet::is_status_packet() const unsigned char *type = to_internal_type(get_type()); std::vector<unsigned char>::iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type)); i += sizeof(type); - if(*i == success || *i == failure) + if(*i == STATUS_SUCCESS || *i == STATUS_FAILURE) return true; return false; } -packet *packet::cli_make_auth_packet() +packet *packet::cli_make_init_packet() { std::vector<unsigned char> v; pack_cli_header(v); @@ -292,8 +298,31 @@ bool packet::check_status() if(i != data.end()) { i += sizeof(type); - if(*i == success) + if(*i == STATUS_SUCCESS) return true; } return false; } + +svc_cmd packet::serv_extract_command(packet& p) +{ + svc_cmd c; + std::vector<unsigned char>::const_iterator i = std::search(p.raw().begin(), p.raw().end(), type_command, type_command + sizeof(type_command)); + if(i != p.raw().end()) + { + i+= sizeof(type_command); + std::vector<unsigned char>::const_iterator i2 = std::find(i, p.raw().end(), delimiter); + if(i2 != p.raw().end()) + { + for(; i < i2; ++i) + c.service.push_back((char)*i); + i++; + i2 = std::search(p.raw().begin(), p.raw().end(), proto_footer, proto_footer + sizeof(proto_footer)); + for(; i < i2; ++i) + c.command.push_back((char)*i); + } + } + return c; +} + +}; diff --git a/proto_lib/packet.h b/proto_lib/packet.h index 58422cc..b431840 100644 --- a/proto_lib/packet.h +++ b/proto_lib/packet.h @@ -17,6 +17,8 @@ #ifndef PACKET_H #define PACKET_H +namespace proto +{ const unsigned char proto_header [] = { 0x06, 0x06, 0x06, 0x13}; const unsigned char proto_footer [] = { 0x13, 0x06, 0x06, 0x06}; @@ -38,4 +40,6 @@ const unsigned char type_auth [] = {0x01, 0x01}; const unsigned char type_services [] = {0xA, 0x01}; const unsigned char type_command [] = {0xA, 0x02}; +}; + #endif diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp index befa203..64f69bb 100644 --- a/proto_lib/utilities.cpp +++ b/proto_lib/utilities.cpp @@ -21,6 +21,9 @@ #include <string.h> #include "utilities.h" +namespace proto +{ + void pack_cli_header(std::vector<unsigned char> &v) { pack_buffer(proto_header, sizeof(proto_header), v); @@ -63,17 +66,19 @@ const unsigned char *to_internal_type(packet_type t) const unsigned char *type = NULL; switch(t) { - case AUTH_REPLY: case AUTH_REQUEST: + case TYPE_AUTH_REPLY: case TYPE_AUTH_REQUEST: type = type_auth; break; - case SERVICES_REPLY: case SERVICES_REQUEST: + case TYPE_SERVICES_REPLY: case TYPE_SERVICES_REQUEST: type = type_services; break; - case COMMAND_REPLY: case COMMAND_REQUEST: + case TYPE_COMMAND_REPLY: case TYPE_COMMAND_REQUEST: type = type_command; break; - case UNKNOWN: default: + case TYPE_UNKNOWN: default: break; }; return type; } + +}; diff --git a/proto_lib/utilities.h b/proto_lib/utilities.h index 7b8713b..6b41ca3 100644 --- a/proto_lib/utilities.h +++ b/proto_lib/utilities.h @@ -20,6 +20,9 @@ #ifndef UTILITIES_H #define UTILITIES_H +namespace proto +{ + void pack_cli_header(std::vector<unsigned char> &v); void pack_serv_header(std::vector<unsigned char> &v); void pack_buffer(const unsigned char *buf, size_t len, std::vector<unsigned char> &v); @@ -27,6 +30,6 @@ void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char> void pack_buffer(std::string &str, std::vector<unsigned char> &v); void pack_buffer(const char* str, std::vector<unsigned char> &v); const unsigned char *to_internal_type(packet_type t); - +}; #endif |