File: giosocket.h

package info (click to toggle)
imms 3.1.0~svn301-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,320 kB
  • sloc: cpp: 7,810; ansic: 1,923; xml: 252; sh: 189; makefile: 142
file content (182 lines) | stat: -rw-r--r-- 4,384 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program 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, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#ifndef __GIOSOCKET_H
#define __GIOSOCKET_H

#include <glib.h>

#include <string>
#include <list>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>

#include <string.h>

#include "immsconf.h"

using std::string;

class LineProcessor
{
public:
    virtual void process_line(const string &line) = 0;
    virtual ~LineProcessor() {}
};

class GIOSocket : public LineProcessor
{
public:
    GIOSocket() : con(0), read_tag(0), write_tag(0), outp(0) {}
    virtual ~GIOSocket() { close(); }

    bool isok() { return con; }

    void init(int fd)
    {
        fcntl(fd, F_SETFD, O_NONBLOCK);

        con = g_io_channel_unix_new(fd);
        read_tag = g_io_add_watch(con,
                (GIOCondition)(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),
                _read_event, this);
    }

    void write(const string &line)
    {
        if (outbuf.empty())
            write_tag = g_io_add_watch(con, G_IO_OUT, _write_event, this);

        outbuf.push_back(line);
    }

    void close()
    {
        if (con)
        {
            g_io_channel_close(con);
            g_io_channel_unref(con);
        }
        if (write_tag)
            g_source_remove(write_tag);
        if (read_tag)
            g_source_remove(read_tag);
        write_tag = read_tag = 0;
        inbuf = "";
        outbuf.clear();
        outp = 0;
        con = 0;
    }

    virtual void connection_lost() = 0;

    static gboolean _read_event(GIOChannel *source,
            GIOCondition condition, gpointer data)
    {
        GIOSocket *s = (GIOSocket*)data;
        return s->read_event(condition);
    }

    static gboolean _write_event(GIOChannel *source,
            GIOCondition condition, gpointer data)
    {
        GIOSocket *s = (GIOSocket*)data;
        return s->write_event(condition);
    }

    bool write_event(GIOCondition condition)
    {
        if (!con)
            return false;

        assert(condition & G_IO_OUT);

        if (!outp && !outbuf.empty())
            outp = outbuf.front().c_str();

        if (!outp)
            return (write_tag = 0);

        unsigned len = strlen(outp);
        gsize n = 0;
        GIOError e = g_io_channel_write(con, (char*)outp, len, &n);
        if (e == G_IO_ERROR_NONE)
        {
            if (n == len)
            {
                outbuf.pop_front();
                outp = 0;
                return outbuf.empty() ? (write_tag = 0) : 1;
            }
            outp += n;
        }

        return true;
    }

    bool read_event(GIOCondition condition)
    {
        if (!con)
            return false;

        if (condition & G_IO_HUP)
        {
            close();
            connection_lost();
#ifdef DEBUG
            std::cerr << "Connection terminated." << std::endl;
#endif
            return false;
        }

        if (condition & G_IO_IN)
        {
            gsize n = 0;
            GIOError e = g_io_channel_read(con, buf, sizeof(buf) - 1, &n);
            if (e == G_IO_ERROR_NONE)
            {
                buf[n] = '\0';
                char *lineend, *cur = buf;
                while ((lineend = strchr(cur, '\n')))
                {
                    *lineend = '\0';
                    inbuf += cur;
                    cur = lineend + 1;
                    process_line(inbuf);
                    inbuf = "";
                }
                inbuf += cur;
            }
        }

        return true;
    }

private:
    char buf[128];

    GIOChannel *con;
    int read_tag, write_tag;
    string inbuf;
    const char *outp;
    std::list<string> outbuf;
};

#endif