aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/server/ClientProxy1_5.cpp
blob: 72c0bf81ba4687d875d6a0c0472f80c8088ad16b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2013-2016 Symless Ltd.
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "server/ClientProxy1_5.h"

#include "server/Server.h"
#include "barrier/FileChunk.h"
#include "barrier/StreamChunker.h"
#include "barrier/ProtocolUtil.h"
#include "io/IStream.h"
#include "base/TMethodEventJob.h"
#include "base/Log.h"

#include <sstream>

//
// ClientProxy1_5
//

ClientProxy1_5::ClientProxy1_5(const std::string& name, barrier::IStream* stream, Server* server,
                               IEventQueue* events) :
    ClientProxy1_4(name, stream, server, events),
    m_events(events)
{

    m_events->adoptHandler(m_events->forFile().keepAlive(),
                            this,
                            new TMethodEventJob<ClientProxy1_3>(this,
                                &ClientProxy1_3::handleKeepAlive, NULL));
}

ClientProxy1_5::~ClientProxy1_5()
{
    m_events->removeHandler(m_events->forFile().keepAlive(), this);
}

void
ClientProxy1_5::sendDragInfo(UInt32 fileCount, const char* info, size_t size)
{
    std::string data(info, size);

    ProtocolUtil::writef(getStream(), kMsgDDragInfo, fileCount, &data);
}

void
ClientProxy1_5::fileChunkSending(UInt8 mark, char* data, size_t dataSize)
{
    FileChunk::send(getStream(), mark, data, dataSize);
}

bool
ClientProxy1_5::parseMessage(const UInt8* code)
{
    if (memcmp(code, kMsgDFileTransfer, 4) == 0) {
        fileChunkReceived();
    }
    else if (memcmp(code, kMsgDDragInfo, 4) == 0) {
        dragInfoReceived();
    }
    else {
        return ClientProxy1_4::parseMessage(code);
    }

    return true;
}

void
ClientProxy1_5::fileChunkReceived()
{
    Server* server = getServer();
    int result = FileChunk::assemble(
                    getStream(),
                    server->getReceivedFileData(),
                    server->getExpectedFileSize());


    if (result == kFinish) {
        m_events->addEvent(Event(m_events->forFile().fileRecieveCompleted(), server));
    }
    else if (result == kStart) {
        if (server->getFakeDragFileList().size() > 0) {
            std::string filename = server->getFakeDragFileList().at(0).getFilename();
            LOG((CLOG_DEBUG "start receiving %s", filename.c_str()));
        }
    }
}

void
ClientProxy1_5::dragInfoReceived()
{
    // parse
    UInt32 fileNum = 0;
    std::string content;
    ProtocolUtil::readf(getStream(), kMsgDDragInfo + 4, &fileNum, &content);

    m_server->dragInfoReceived(fileNum, content);
}