@@ -28,14 +28,17 @@ RingBuffer::RingBuffer(const char *lfilename, long long size)
28
28
{
29
29
normalfile = false ;
30
30
filename = lfilename;
31
-
31
+ filesize = size;
32
+
32
33
fd = -1 ; fd2 = -1 ;
33
34
34
35
fd = open (filename.c_str (), O_WRONLY|O_CREAT|O_LARGEFILE, 0644 );
35
36
fd2 = open (filename.c_str (), O_RDONLY|O_LARGEFILE);
36
37
37
38
writestart = writepos = 0 ;
38
39
readstart = readpos = 0 ;
40
+
41
+ wrapcount = 0 ;
39
42
}
40
43
41
44
RingBuffer::~RingBuffer (void )
@@ -62,10 +65,30 @@ int RingBuffer::Read(void *buf, int count)
62
65
}
63
66
else
64
67
{
65
- while (readpos + count >= writepos)
68
+ while (readpos + count >= writepos + wrapcount * filesize)
69
+ {
66
70
usleep (5 );
67
- ret = read (fd2, buf, count);
68
- readpos += ret;
71
+ }
72
+
73
+ if (readpos + count > filesize)
74
+ {
75
+ int toread = filesize - readpos;
76
+
77
+ ret = read (fd2, buf, toread);
78
+
79
+ int left = count - toread;
80
+ lseek (fd2, 0 , SEEK_SET);
81
+
82
+ ret = read (fd2, (char *)buf + toread, left);
83
+ ret += toread;
84
+
85
+ readpos = left;
86
+ }
87
+ else
88
+ {
89
+ ret = read (fd2, buf, count);
90
+ readpos += ret;
91
+ }
69
92
}
70
93
return ret;
71
94
}
@@ -87,9 +110,27 @@ int RingBuffer::Write(const void *buf, int count)
87
110
}
88
111
else
89
112
{
90
- ret = write (fd, buf, count);
91
- writepos += ret;
113
+ if (writepos + count > filesize)
114
+ {
115
+ int towrite = filesize - writepos;
116
+ ret = write (fd, buf, towrite);
117
+
118
+ int left = count - towrite;
119
+ lseek (fd, 0 , SEEK_SET);
120
+
121
+ ret = write (fd, (char *)buf + towrite, left);
122
+ writepos = left;
123
+
124
+ ret += towrite;
125
+ wrapcount++;
126
+ }
127
+ else
128
+ {
129
+ ret = write (fd, buf, count);
130
+ writepos += ret;
131
+ }
92
132
}
133
+
93
134
return ret;
94
135
}
95
136
@@ -103,6 +144,7 @@ long long RingBuffer::Seek(long long pos, int whence)
103
144
else
104
145
{
105
146
ret = lseek (fd2, pos, whence);
147
+ readpos = pos;
106
148
}
107
149
return ret;
108
150
}
0 commit comments