123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include <stdio.h>
- #include <unistd.h>
- #ifdef _WIN32
- #include <winsock2.h>
- #include <windows.h>
- #define CLOSESOCKET(x) closesocket(x)
- #define SHUT_RDWR SD_BOTH
- #else
- #include <sys/socket.h>
- #define CLOSESOCKET(x) ::close(x)
- #endif
- #include "BBS2chProxyRawSocket.h"
- BBS2chProxyRawSocket::BBS2chProxyRawSocket(int sock) :
- socket(sock)
- {
- }
- BBS2chProxyRawSocket::~BBS2chProxyRawSocket()
- {
- }
- int BBS2chProxyRawSocket::read(char *buffer, int length)
- {
- return recv(socket, buffer, length, 0);
- }
- int BBS2chProxyRawSocket::readLine(char *buffer, int maxLength)
- {
- char *ptr = buffer;
- while (ptr < buffer + maxLength - 1) {
- int read = recv(socket, ptr, 1, 0);
- if (read != 1) {
- return 0;
- }
- if (*ptr++ == '\n') {
- break;
- }
- }
- *ptr = 0;
- return 1;
- }
- int BBS2chProxyRawSocket::write(const char *buffer, int length)
- {
- return send(socket, buffer, length, 0);
- }
- int BBS2chProxyRawSocket::writeString(const std::string &str)
- {
- return send(socket, str.data(), str.length(), 0);
- }
- void BBS2chProxyRawSocket::close(void)
- {
- if (socket >= 0) {
- shutdown(socket, SHUT_RDWR);
- #ifdef _WIN32
- Sleep(10);
- #else
- usleep(10000);
- #endif
- CLOSESOCKET(socket);
- socket = -1;
- }
- }
|