OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [remote/] [tcpclient.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2017 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      Remote access to debugger via TCP connection. Client thread.
6
 */
7
 
8
#include "tcpclient.h"
9
 
10
namespace debugger {
11
 
12
/** Class registration in the Core */
13
REGISTER_CLASS(TcpClient)
14
 
15
TcpClient::TcpClient(const char *name) : IService(name),
16
    tcpcmd_(static_cast<IService *>(this)) {
17
    registerInterface(static_cast<IThread *>(this));
18
    registerAttribute("Enable", &isEnable_);
19
    RISCV_mutex_init(&mutexTx_);
20
}
21
 
22
TcpClient::~TcpClient() {
23
    RISCV_mutex_destroy(&mutexTx_);
24
}
25
 
26
void TcpClient::postinitService() {
27
    if (isEnable_.to_bool()) {
28
        if (!run()) {
29
            RISCV_error("Can't create thread.", NULL);
30
            return;
31
        }
32
    }
33
}
34
 
35
void TcpClient::updateData(const char *buf, int buflen) {
36
    AttributeType console;
37
    console.make_list(2);
38
    console[0u].make_string("Console");
39
    console[1].make_string(buf);
40
    console.to_config();
41
 
42
    RISCV_mutex_lock(&mutexTx_);
43
    memcpy(&txbuf_[txcnt_], console.to_string(), console.size() + 1);
44
    txcnt_ += console.size() + 1;
45
    RISCV_mutex_unlock(&mutexTx_);
46
}
47
 
48
void TcpClient::busyLoop() {
49
    int rxbytes;
50
    int sockerr;
51
    addr_size_t sockerr_len = sizeof(sockerr);
52
    RISCV_add_default_output(static_cast<IRawListener *>(this));
53
 
54
    cmdcnt_ = 0;
55
    txcnt_ = 0;
56
    while (isEnabled()) {
57
        rxbytes = recv(hsock_, rcvbuf, sizeof(rcvbuf), 0);
58
        getsockopt(hsock_, SOL_SOCKET, SO_ERROR,
59
                    reinterpret_cast<char *>(&sockerr), &sockerr_len);
60
 
61
        if (rxbytes == 0) {
62
            RISCV_error("Socket error: rxbytes=%d, sockerr=%d",
63
                        rxbytes, sockerr);
64
            loopEnable_.state = false;
65
        } else if (rxbytes < 0) {
66
            // Timeout:
67
        } else if (rxbytes > 0) {
68
            for (int i = 0; i < rxbytes; i++) {
69
                cmdbuf_[cmdcnt_++] = rcvbuf[i];
70
                if (rcvbuf[i] == '\0') {
71
                    processRxString();
72
                    cmdcnt_ = 0;
73
                }
74
            }
75
        }
76
        if (sendTxBuf() < 0) {
77
            RISCV_error("Send error: txcnt=%d", txcnt_);
78
            loopEnable_.state = false;
79
        }
80
    }
81
    closeSocket();
82
    RISCV_remove_default_output(static_cast<IRawListener *>(this));
83
}
84
 
85
void TcpClient::processRxString() {
86
    tcpcmd_.updateData(cmdbuf_, cmdcnt_);
87
    AttributeType *resp = tcpcmd_.response();
88
    RISCV_mutex_lock(&mutexTx_);
89
    memcpy(&txbuf_[txcnt_], resp->to_string(), resp->size() + 1);
90
    txcnt_ += resp->size() + 1;
91
    RISCV_mutex_unlock(&mutexTx_);
92
}
93
 
94
int TcpClient::sendTxBuf() {
95
    int total = txcnt_;
96
    char *ptx = txbuf_;
97
    int txbytes;
98
    while (total > 0) {
99
        txbytes = send(hsock_, ptx, total, 0);
100
        if (txbytes == 0) {
101
            return -1;
102
        }
103
        total -= txbytes;
104
        ptx += txbytes;
105
    }
106
    RISCV_mutex_lock(&mutexTx_);
107
    txcnt_ = 0;
108
    RISCV_mutex_unlock(&mutexTx_);
109
    return 0;
110
}
111
 
112
void TcpClient::closeSocket() {
113
    if (hsock_ < 0) {
114
        return;
115
    }
116
 
117
#if defined(_WIN32) || defined(__CYGWIN__)
118
    closesocket(hsock_);
119
#else
120
    shutdown(hsock_, SHUT_RDWR);
121
    close(hsock_);
122
#endif
123
    hsock_ = -1;
124
}
125
 
126
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.