/* Copyright © 2003, Russell C. Hay All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: i. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ii. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. iii. Neither the name of the dead[protocol]society nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include "stl.h" #include "mtxtype.h" #include "action.h" #include "device.h" #include "protocol.h" #include "connection.h" connection::connection(protocol *p, int sock, device *dev) : m_protocol(p) , m_sock(sock) , m_device(dev) , m_notification_active(0) , m_priority_active(0) , m_buf_offset(0) { struct timeval tm; tm.tv_sec = 0; tm.tv_usec = 1; setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO, &tm, sizeof(tm)); } connection::~connection() { if (m_sock != -1) { printf("Killing conn#%d\n", m_sock); close(m_sock); } } int connection::HandleConnection() { int len, done=0, tmp = 0;; action act; len = m_buf_offset; while(!done) { tmp = read(m_sock, &(m_buf[len]), 1); if ((tmp <= 0)&&(errno != EAGAIN)) { len = tmp; done = 1; printf("Error\n"); continue; } else if (errno == EAGAIN) { len = tmp; m_buf_offset = len; done = 2; printf("EAGAIN %d\n", m_sock); } else if (m_buf[len] == '\n') done = 1; len += tmp; } if (done == 2) { return 0; } m_buf_offset = 0; if (len == 0) return 1; if (len < 0) return -1; m_buf[len] = 0; #ifdef DEBUG printf("#%d:%s:\n",m_sock, m_buf); #endif // DEBUG // Decode data act = m_protocol->parse(m_buf, len); // Dispatch decoded action to device if (act.GetActionType() != -1) { switch(act.GetActionType()) { case ACTION_NSTART: m_notification_active = 1; break; case ACTION_NSTOP: m_notification_active = 0; break; case ACTION_PRI_START: m_priority_active = 1; m_device->StartPriority(); break; case ACTION_PRI_STOP: m_priority_active = 0; m_device->StopPriority(); break; default: if ((!m_device->InPriority())||(m_priority_active)) m_device->Push(act); break; } } else if (act.GetActionType() == ACTION_PARSE) return -2; else if ((!m_device->InPriority())||(m_priority_active)) m_device->Push(act); m_device->Process(); return 0; } void connection::AddSocketToFdSet(fd_set *fdset, int *maxsock) { FD_SET(m_sock, fdset); *maxsock = m_sock>=*maxsock?m_sock+1:*maxsock; } int connection::IsSet(fd_set *fdset) { return FD_ISSET(m_sock, fdset); } void connection::KeyNotification(char c) { if (m_notification_active) { char buf[2]; buf[0] = c; buf[1] = '\n'; write(m_sock, buf, 2); printf("Writing Key to socket#%d\n", m_sock); } else { printf("Key Notification is not active\n"); } }