EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Socket.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24 */
25 
26 #include "eve-core.h"
27 
28 #include "network/Socket.h"
29 
30 Socket::Socket( int af, int type, int protocol )
31 : mSock( INVALID_SOCKET )
32 {
33  // Create new socket
34  mSock = ::socket( af, type, protocol );
35 
36  // Verify socket
37  assert( mSock != INVALID_SOCKET );
38 }
39 
41 : mSock( sock )
42 {
43  // Verify socket
44  assert( mSock != INVALID_SOCKET );
45 }
46 
48 {
49  ::shutdown( mSock, SHUT_RD );
50  ::shutdown( mSock, SHUT_WR );
51  ::close( mSock );
52 }
53 
54 int Socket::connect( const sockaddr* name, unsigned int namelen )
55 {
56  return ::connect( mSock, name, namelen );
57 }
58 
59 unsigned int Socket::recv( void* buf, unsigned int len, int flags )
60 {
61  return ::recv( mSock, (char*)buf, len, flags );
62 }
63 
64 unsigned int Socket::recvfrom( void* buf, unsigned int len, int flags, sockaddr* from, unsigned int* fromlen )
65 {
66  return ::recvfrom( mSock, buf, len, flags, from, fromlen );
67 }
68 
69 unsigned int Socket::send( const void* buf, unsigned int len, int flags )
70 {
71  return ::send( mSock, (const char*)buf, len, flags );
72 }
73 
74 unsigned int Socket::sendto( const void* buf, unsigned int len, int flags, const sockaddr* to, unsigned int tolen )
75 {
76  return ::sendto( mSock, (const char*)buf, len, flags, to, tolen );
77 }
78 
79 int Socket::bind( const sockaddr* name, unsigned int namelen )
80 {
81  return ::bind( mSock, name, namelen );
82 }
83 
84 int Socket::listen( int backlog )
85 {
86  return ::listen( mSock, backlog );
87 }
88 
89 Socket* Socket::accept( sockaddr* addr, unsigned int* addrlen )
90 {
91  SOCKET sock = ::accept( mSock, addr, addrlen );
92 
93  if( sock != INVALID_SOCKET )
94  return new Socket( sock );
95  else
96  return nullptr;
97 }
98 
99 int Socket::setopt( int level, int optname, const void* optval, unsigned int optlen )
100 {
101  return ::setsockopt( mSock, level, optname, (const char*)optval, optlen );
102 }
103 
104 int Socket::fcntl( int cmd, long arg )
105 {
106  return ::fcntl( mSock, cmd, arg );
107 }
Simple wrapper for sockets.
Definition: Socket.h:34
SOCKET mSock
Definition: Socket.h:58
int listen(int backlog=SOMAXCONN)
Definition: Socket.cpp:84
unsigned int recvfrom(void *buf, unsigned int len, int flags, sockaddr *from, unsigned int *fromlen)
Definition: Socket.cpp:64
int bind(const sockaddr *name, unsigned int namelen)
Definition: Socket.cpp:79
Socket(int af, int type, int protocol)
Definition: Socket.cpp:30
#define INVALID_SOCKET
Definition: eve-compat.h:129
unsigned int sendto(const void *buf, unsigned int len, int flags, const sockaddr *to, unsigned int tolen)
Definition: Socket.cpp:74
unsigned int recv(void *buf, unsigned int len, int flags)
Definition: Socket.cpp:59
int setopt(int level, int optname, const void *optval, unsigned int optlen)
Definition: Socket.cpp:99
~Socket()
Definition: Socket.cpp:47
Socket * accept(sockaddr *addr, unsigned int *addrlen)
Definition: Socket.cpp:89
int SOCKET
Definition: eve-compat.h:127
unsigned int send(const void *buf, unsigned int len, int flags)
Definition: Socket.cpp:69
int fcntl(int cmd, long arg)
Definition: Socket.cpp:104
int connect(const sockaddr *name, unsigned int namelen)
Definition: Socket.cpp:54