OSDN Git Service

Vss2Git
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast_1 / core / common / socket.h
1 // ------------------------------------------------
2 // File : socket.h
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //
7 // (c) 2002 peercast.org
8 // ------------------------------------------------
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 // ------------------------------------------------
19
20 #ifndef _SOCKET_H
21 #define _SOCKET_H
22
23
24 #include "common.h"
25 #include "stream.h"
26
27 //#define DISABLE_NAGLE 1
28
29 class SocketBuffer {
30
31 public:
32         SocketBuffer(const void *p, int l){
33                 buf = ::new char[l];
34                 len = l;
35                 pos = 0;
36                 next = NULL;
37                 ctime = sys->getTime();
38                 memcpy((void*)buf, p, l);
39         }
40
41         ~SocketBuffer(){
42                 if (buf){
43                         ::delete [] buf;
44                 }
45         }
46         char *buf;
47         int len;
48         int pos;
49         unsigned int ctime;
50         SocketBuffer *next;
51 };
52
53 class SocketBufferList {
54
55 public:
56         SocketBufferList(){
57                 top = NULL;
58                 last = NULL;
59                 skipCount = 0;
60                 lastSkipTime = 0;
61         }
62
63         bool isNull(){ return (top == NULL); }
64         void add(const void *p, int l){
65                 SocketBuffer *tmp = new SocketBuffer(p,l);
66
67                 if (!last){
68                         top = tmp;
69                         last = tmp;
70                 } else {
71                         last->next = tmp;
72                         last = tmp;
73                 }
74
75 //              LOG_DEBUG("tmp = %d, top = %d, last = %d", tmp, top, last);
76         }
77
78         SocketBuffer *getTop(){
79                 unsigned int ctime = sys->getTime();
80
81                 while(top){
82                         if (top && (top->ctime + 10 >= ctime)){
83                                 break;
84                         } else {
85 //                              LOG_DEBUG("over 10sec(data skip)");
86                                 skipCount++;
87                                 lastSkipTime = sys->getTime();
88                                 deleteTop();
89                         }
90                 }
91                 return top;
92         }
93
94         void deleteTop(){
95 //              LOG_DEBUG("oldtop = %d", top);
96                 SocketBuffer *tmp = top;
97                 top = tmp->next;
98                 delete tmp;
99                 if (!top){
100                         last = NULL;
101                 }
102
103 //              LOG_DEBUG("newtop = %d",top);
104         }
105
106         void clear(){
107                 while(top){
108                         SocketBuffer *tmp = top;
109                         top = tmp->next;
110                         delete tmp;
111                 }
112                 top = NULL;
113                 last = NULL;
114         }
115
116         SocketBuffer *top;
117         SocketBuffer *last;
118         unsigned int skipCount;
119         unsigned int lastSkipTime;
120
121 };
122
123 // --------------------------------------------------
124 class ClientSocket : public Stream
125 {
126 public:
127
128         ClientSocket()
129         {
130                 readTimeout = 30000;
131                 writeTimeout = 30000;
132 #ifdef WIN32
133                 skipCount = 0;
134                 lastSkipTime = 0;
135 #endif
136         }
137
138         ~ClientSocket(){
139 #ifdef WIN32
140                 bufList.clear();
141 #endif
142         }
143
144     // required interface
145         virtual void    open(Host &) = 0;
146         virtual void    bind(Host &) = 0;
147         virtual void    connect() = 0;
148         virtual bool    active() = 0;
149         virtual ClientSocket    *accept() = 0;
150         virtual Host    getLocalHost() = 0;
151
152         virtual void    setReadTimeout(unsigned int t)
153         {
154                 readTimeout = t;
155         }
156         virtual void    setWriteTimeout(unsigned int t)
157         {
158                 writeTimeout = t;
159         }
160         virtual void    setBlocking(bool) {}
161
162
163     static unsigned int    getIP(char *);
164         static bool                     getHostname(char *,unsigned int);
165
166     virtual bool eof()
167     {
168         return active()==false;
169     }
170
171     Host    host;
172
173 #ifdef WIN32
174         SocketBufferList        bufList;
175         virtual void bufferingWrite(const void *, int) = 0;
176         unsigned int skipCount;
177         unsigned int lastSkipTime;
178 #endif
179
180         unsigned int readTimeout,writeTimeout;
181
182 };
183
184
185 #endif