File: wincomsocket.cpp

package info (click to toggle)
musicbrainz 1.0.1.final-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,168 kB
  • ctags: 2,610
  • sloc: ansic: 16,052; sh: 8,658; cpp: 6,283; perl: 327; makefile: 112
file content (168 lines) | stat: -rw-r--r-- 4,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*__________________________________________________________________________

        FreeAmp - The Free MP3 Player

        Portions Copyright (C) 2000 Relatable

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, Write to the Free Software
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
        
        $Id: wincomsocket.cpp,v 1.5 2001/09/12 21:43:19 robert Exp $
____________________________________________________________________________*/

#include "wincomsocket.h"

#if !defined(WIN32) && !defined(__BEOS__)
#define closesocket(s) close(s)
#endif

MBCOMSocket::MBCOMSocket(int nSocket, int nSockType)
{
    m_nSocket = nSocket;
    m_bConnected = true;
    if (m_nSocket != INVALID_SOCKET) 
        m_bConnected = true;
    m_nSockType = nSockType;
}

MBCOMSocket::~MBCOMSocket()
{
    if (IsConnected()) Disconnect();
}

/** Connects a socket to pIP, on nPort, of type nType. */
int MBCOMSocket::Connect(const char* pIP, int nPort, int nType, bool bBroadcast)
{
    if (IsConnected()) 
        Disconnect();

    sockaddr_in addr;
    //hostent* pServer;
    unsigned long uAddr = inet_addr(pIP);
    int nErr = 0;
    m_nSockType = nType;
    m_nSocket = socket(AF_INET, nType, 0);
    if (m_nSocket == INVALID_SOCKET) 
        return INVALID_SOCKET;
    /*
    pServer = gethostbyname(pIP);
    if (pServer == NULL)
    {
        closesocket(m_nSocket);
        m_nSocket = INVALID_SOCKET;
        return INVALID_SOCKET;
    }
    */
    memset((char*)&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = uAddr;
    //memcpy((char *)&addr.sin_addr.s_addr, (char*)(pServer->h_addr), pServer->h_length);   /* set address */
    //bcopy((char*)(pServer->h_addr), (char*)&(addr.sin_addr.s_addr), pServer->h_length);
    addr.sin_port = htons(nPort);

    nErr = connect(m_nSocket, (sockaddr*)&addr, sizeof(sockaddr_in));
    if (nErr == SOCKET_ERROR)
    {
        closesocket(m_nSocket);
        m_nSocket = INVALID_SOCKET;
        return INVALID_SOCKET;
    }

    m_bConnected = true;
    return 0;
}

/** Disconnects the current socket */
int MBCOMSocket::Disconnect()
{
    int nErr = 0;
    if (!IsConnected()) 
        return SOCKET_ERROR;

    if (m_nSockType == SOCK_STREAM)
    {
        nErr = shutdown(m_nSocket, 2);
    }

    nErr = closesocket(m_nSocket);
    m_nSocket = INVALID_SOCKET;
    m_bConnected = false;
    return (nErr != SOCKET_ERROR) - 1;
}

/** Checks if there is a current open connection */
bool MBCOMSocket::IsConnected()
{
    return m_bConnected;
}

/** Reads from a socket, into pbuffer, up to a max of nLen byte, and writes 
  * how many were actually written to nBytesWritten. */
int MBCOMSocket::Read(char* pBuffer, int nLen, int* nBytesWritten)
{
    if (!IsConnected()) 
        return SOCKET_ERROR;  // no connection
    int nErr = 0;

    nErr = recv(m_nSocket, pBuffer, nLen, 0);
    //nErr = recv(m_nSocket, (void*)pBuffer, nLen, 0);
    if ((nErr != SOCKET_ERROR) && (nBytesWritten != NULL))
    {
        *nBytesWritten = nErr;
    }
    return (nErr != SOCKET_ERROR) - 1;
}

/** Reads in a non blocking fashion (ie, selects and polls) for nTimeout seconds */
int MBCOMSocket::NBRead(char* pBuffer, int nLen, int* nBytesWritten, int nTimeout)
{
    timeval tval;
    tval.tv_sec = nTimeout;
    tval.tv_usec = 0;
    fd_set rset;
    int nErr = 0;

    FD_ZERO(&rset);
    FD_SET(m_nSocket, &rset);
    int nResSelect = select(m_nSocket + 1, &rset, NULL, NULL, &tval);
    if ((nResSelect != 0) && (nResSelect != SOCKET_ERROR) && 
        (FD_ISSET(m_nSocket, &rset)))
    {
        if ((nErr = Read(pBuffer, nLen, nBytesWritten)) == 0)
        {
            return 0;
        }
    }
    else
    {
        return -1;  // FD_ISSET failed.
    }
    return 0;
}

/** Writes to a socket, from buffer pBuffer, up to nLen bytes, and returns the number of written bytes in pnBytesWritten. */
int MBCOMSocket::Write(const char* pBuffer, int nLen, int* pnBytesWritten)
{
    if (!IsConnected()) 
        return SOCKET_ERROR; // no connection
    int nErr = 0;

    nErr = send(m_nSocket, pBuffer, nLen, 0);
    //nErr = send(m_nSocket, (void*)pBuffer, nLen, 0);
    if ((nErr != SOCKET_ERROR) && (pnBytesWritten != NULL))
    {
        *pnBytesWritten = nErr;
    }
    return (nErr != SOCKET_ERROR) - 1;
}