File: ServerMultiplexer.C

package info (click to toggle)
dxpc 3.9.2-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,164 kB
  • ctags: 1,252
  • sloc: cpp: 12,036; sh: 2,823; makefile: 77
file content (98 lines) | stat: -rw-r--r-- 2,490 bytes parent folder | download | duplicates (3)
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
#include "dxpcconf.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "ServerMultiplexer.H"
#include "ServerChannel.H"

#if !defined(__MINGW32__)
# include <netinet/in.h>
#endif
#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
# include <netinet/tcp.h>
#endif

ServerMultiplexer::ServerMultiplexer(int proxyFD, int xServerAddrFamily,
                                     sockaddr * xServerAddr,
                                     unsigned int xServerAddrLength,
                                     unsigned int
                                     statisticsLevel) :
    Multiplexer(proxyFD),
    xServerAddrFamily_(xServerAddrFamily),
    xServerAddr_(xServerAddr),
    xServerAddrLength_(xServerAddrLength),
    statisticsLevel_(statisticsLevel)
{
    for (unsigned int i = 0; i < MAX_CONNECTIONS; i++)
    {
        channelIDToFDMap_[i] = -1;
    }
}

ServerMultiplexer::~ServerMultiplexer()
{
    if (xServerAddr_)
    {
        delete xServerAddr_;
    }
}

void ServerMultiplexer::createNewConnection(int clientFD)
{
    clientFD = 0;
    CERR << "Internal error: in ServerMultiplexer::createNewConnection" <<
        ENDL;
}

int ServerMultiplexer::createNewConnectionFromProxy(int channelID)
{
    // Connect to the real X server
    int xServerFD = socket(xServerAddrFamily_, SOCK_STREAM, PF_UNSPEC);

    if (xServerFD == -1)
    {
        CERR << "socket() failed, errno=" << errno << ENDL;
        return 0;
    }

    SETNODELAY(xServerFD);

    if (connect(xServerFD, xServerAddr_, xServerAddrLength_) == -1)
    {
        CERR << "connect() to X server failed, errno=" << errno << " (" <<
            strerror(errno) << ")" << ENDL;
        SOCKCLOSE(xServerFD);
        return 0;
    }
    channelIDToFDMap_[channelID] = xServerFD;

    channels_[channelID] = new ServerChannel(xServerFD, statisticsLevel_);
    return 1;
}

int ServerMultiplexer::channelIDToFD(int channelID) const
{
    if ((channelID < 0) || ((unsigned int) channelID >= MAX_CONNECTIONS))
        return -1;
    else
        return channelIDToFDMap_[channelID];
}

int ServerMultiplexer::fdToChannelID(int fd) const
{
    for (unsigned i = 0; i < MAX_CONNECTIONS; i++)
    {
        if (channelIDToFDMap_[i] == fd)
        {
            return i;
        }
    }
    CERR << "No such fd " << fd << " in ServerMultiplexer::fdToChannelID" <<
        ENDL;
    return -1;
}

void ServerMultiplexer::cleanupChannelFDMapping(int channelID)
{
    channelIDToFDMap_[channelID] = -1;
}