/* ----------------------------------------------------------
ep.cc by biscuit@actoz.com
for Linux 2.6.x
v1.0 06/24/2004 first release
---------------------------------------------------------- */
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include "epbasic.h"
#define SIZE_APACKET 1400
using namespace std;
// ¸®ÅÏ°ªÀÌ ±×´ë·Î OnError·Î Àü´ÞµÈ´Ù
int TBasicClient::OnRead(void)
{
char buf[SIZE_OSBUF];
int nread = read(fd, buf, SIZE_OSBUF);
if(nread == 0) return EP_ERROR_READ_CLOSE;
else if(nread < 0){
cout << "nread returns -1 : " << strerror(errno) << "(" << errno << ")" << endl;
if(errno == EAGAIN) return EP_ERROR_NONE;
return EP_ERROR_READ_GEN;
}
if(recvbuf->Push(nread, buf) < 1) return EP_ERROR_READ_OVERFLOW;
nin += nread;
return EP_ERROR_NONE;
};
// ¸®ÅÏ°ªÀÌ ±×´ë·Î OnError·Î Àü´ÞµÈ´Ù
int TBasicClient::OnWrite(void)
{
is_would_block = false;
return Flush();
}
int TBasicClient::Flush(void)
{
if(is_error) return EP_ERROR;
char buf[SIZE_APACKET];
int nsize;
while(! is_would_block){
if(sendbuf->empty()) return EP_ERROR_NONE;
nsize = sendbuf->View(SIZE_APACKET, buf);
int nwrite = write(fd, buf, nsize);
if( nwrite != nsize ){
// ¹º°¡ ¿¡·¯°¡ »ý±ä°Í
if(nwrite < 1){
return OnError(EP_ERROR_WRITE_GEN);
}
is_would_block = true;
}
sendbuf->Skip(nwrite);
nout += nwrite;
}
return EP_ERROR_NONE;
}
int TBasicClient::OnError(const int nerror)
{
is_error = true;
return nerror;
};
int TBasicClient::OnIdle(void)
{
// µé¾î¿Â µ¥ÀÌÅÍ Ã³¸®
while(! recvbuf->empty()){
char buf[SIZE_APACKET];
int nsize = recvbuf->Pop(SIZE_APACKET, buf);
if(sendbuf->Push(nsize, buf) < 1){
// cout << "sendbuffer overflow\n";
}
}
// ³»º¸³¾ µ¥ÀÌÅÍ Ã³¸®
Flush();
return 0;
}