use the -newos toolchain even if -elf is present.
[newos.git] / apps / irc / ircreader.cpp
blobeac1d29387ed2a96e74e705c3044413e5e91b852
1 /*
2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <sys/syscalls.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <socket/socket.h>
10 #include "ircreader.h"
11 #include "ircengine.h"
13 IRCReader::IRCReader(int socket, IRCEngine *engine)
14 : mSocket(socket),
15 mEngine(engine),
16 mReaderThread(-1),
17 mBufPos(0)
21 IRCReader::~IRCReader()
23 // forgive me father...
24 if(mReaderThread >= 0)
25 _kern_thread_kill_thread(mReaderThread);
28 int IRCReader::Start()
30 mReaderThread = _kern_thread_create_thread("irc reader", &ThreadEntry, (void *)this);
31 if(mReaderThread < 0)
32 return mReaderThread;
34 _kern_thread_resume_thread(mReaderThread);
36 return 0;
39 int IRCReader::Run()
41 for(;;) {
42 char buf[1024];
43 ssize_t bytes_received;
45 bytes_received = read(mSocket, buf, sizeof(buf));
46 if(bytes_received < 0) {
47 mEngine->SocketError(bytes_received);
48 break;
51 // add this data to the end of the line buffer we have
52 for(int i = 0; i < bytes_received; i++) {
53 mBuf[mBufPos++] = buf[i];
54 if(mBufPos == sizeof(mBuf) - 1 || buf[i] == '\n') {
55 mBuf[mBufPos] = 0;
56 mEngine->ReceivedSocketData(mBuf, mBufPos);
57 mBufPos = 0;
61 return 0;
64 int IRCReader::ThreadEntry(void *args)
66 IRCReader *reader = (IRCReader *)args;
68 return reader->Run();