Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iomanip>
- #include <iostream>
- #include "client.hpp"
- #include "handlers.hpp"
- client::pointer client::create(asio::io_service &ios) {
- return pointer(new client(ios));
- }
- tcp::socket& client::socket() {
- return socket_;
- }
- void client::start() {
- buf = new byte[4096];
- socket_.async_receive(asio::buffer(buf, 4096), 0,
- bind(&client::on_receive, shared_from_this(),
- std::placeholders::_1, std::placeholders::_2));
- }
- void client::send(byte* data, size_t len) {
- cryptor->encrypt(data, len);
- cout << "Sent " << socket_.send(asio::buffer(data, len)) << " bytes!" << endl;
- }
- client::client(asio::io_service& ios)
- : socket_(ios) {
- cryptor = new crypt();
- }
- void client::on_receive(const asio::error_code& err, size_t len) {
- if (err) {
- cout << "Client disconnected!" << endl;
- delete cryptor;
- //delete[] buf;
- socket_.shutdown(asio::ip::tcp::socket::shutdown_both);
- socket_.close();
- return;
- }
- cout << "Received packet of length: " << len << endl;
- len += prelen;
- prelen = 0;
- int32 i = 0;
- size_t plen = 0;
- if (plen > len) {
- prelen = len;
- socket_.async_receive(asio::buffer(&buf[len], 4096 - len), 0,
- bind(&client::on_receive, shared_from_this(),
- std::placeholders::_1, std::placeholders::_2));
- return;
- }
- while (i < len) {
- if (!cryptor->is_valid(buf, i))
- return;
- plen = cryptor->packet_len(buf, i);
- cryptor->decrypt(buf, i, plen);
- uint16 op = *(uint16*)&buf[8];
- if ((uint32)handlers[op] != 0xCDCDCDCD) // Valid in Windows debug build only!
- handlers[op](buf, this);
- else
- for (int32 i = 0; i < len; ++i)
- cout << hex << setfill('0') << setw(2) << (int32)buf[i] << ", " << dec << endl;
- i += plen;
- }
- socket_.async_receive(asio::buffer(buf, 4096), 0,
- bind(&client::on_receive, shared_from_this(),
- std::placeholders::_1, std::placeholders::_2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement