Skip to content

Commit 3d988c8

Browse files
committed
1 parent abfb193 commit 3d988c8

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

Server/Server/BaseSocket.cpp

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include "BaseSocket.h"
2+
3+
4+
BaseSocket::BaseSocket()
5+
{
6+
_ip = "";
7+
_port = 0;
8+
_protoType = SocketProtoType::PROTO_TCP;
9+
_socket = INVALID_SOCKET;
10+
_inited = false;
11+
}
12+
BaseSocket::~BaseSocket()
13+
{
14+
}
15+
BaseSocket::BaseSocket(string ip, unsigned short port, SocketProtoType prototype) :
16+
_ip(ip),
17+
_port(port),
18+
_protoType(prototype),
19+
_socket(INVALID_SOCKET),
20+
_inited(false)
21+
{
22+
}
23+
bool BaseSocket::Init()
24+
{
25+
_inited = true;
26+
WORD wversion = MAKEWORD(2, 2);
27+
WSADATA wsdata;
28+
int err = -1;
29+
err = WSAStartup(wversion, &wsdata);
30+
if (err == SOCKET_ERROR && wsdata.wVersion != wversion)
31+
{
32+
return false;
33+
}
34+
int type = 0;
35+
int protocol = 0;
36+
switch (_protoType)
37+
{
38+
case PROTO_TCP:
39+
type = SOCK_STREAM;
40+
protocol = IPPROTO_TCP;
41+
break;
42+
case PROTO_UDP:
43+
type = SOCK_DGRAM;
44+
protocol = IPPROTO_UDP;
45+
break;
46+
case PROTO_RAW:
47+
type = SOCK_RAW;
48+
protocol = IPPROTO_RAW;
49+
break;
50+
default:
51+
type = SOCK_DGRAM;
52+
protocol = IPPROTO_IP;
53+
break;
54+
}
55+
_socket = socket(AF_INET, type, protocol);
56+
if (_socket == INVALID_SOCKET || _socket == SOCKET_ERROR)
57+
{
58+
Clean();
59+
return false;
60+
}
61+
62+
if (!Bind(_ip, _port))
63+
{
64+
Clean();
65+
return false;
66+
}
67+
68+
return true;
69+
}
70+
void BaseSocket::Clean()
71+
{
72+
if (_socket != INVALID_SOCKET)
73+
closesocket(_socket);
74+
75+
if (_inited)
76+
WSACleanup();
77+
78+
_ip = "";
79+
_port = 0;
80+
_protoType = SocketProtoType::PROTO_TCP;
81+
_socket = INVALID_SOCKET;
82+
_inited = false;
83+
}
84+
bool BaseSocket::Bind(string ip, unsigned short port)
85+
{
86+
if (!_inited && !Init())
87+
return false;
88+
89+
if (_socket == INVALID_SOCKET)
90+
return false;
91+
92+
SOCKADDR_IN addr;
93+
int len = sizeof(SOCKADDR);
94+
memset(&addr, 0, sizeof(SOCKADDR_IN));
95+
96+
_ip = ip;
97+
_port = port;
98+
99+
if (_ip == "")
100+
addr.sin_addr.s_addr = htonl(ADDR_ANY);
101+
else
102+
addr.sin_addr.s_addr = inet_addr(_ip.c_str());
103+
104+
addr.sin_family = AF_INET;
105+
addr.sin_port = htons(_port);
106+
107+
if (bind(_socket, (SOCKADDR*)&addr, len) == SOCKET_ERROR)
108+
{
109+
return false;
110+
}
111+
return true;
112+
}
113+
bool BaseSocket::Connect(string ip,unsigned short port)
114+
{
115+
SOCKADDR_IN addr;
116+
int len = sizeof(SOCKADDR);
117+
memset(&addr, 0, sizeof(SOCKADDR_IN));
118+
119+
if (ip == "")
120+
addr.sin_addr.s_addr = htonl(ADDR_ANY);
121+
else
122+
addr.sin_addr.s_addr = inet_addr(ip.c_str());
123+
124+
addr.sin_family = AF_INET;
125+
addr.sin_port = htons(port);
126+
127+
if (SOCKET_ERROR == connect(_socket, (SOCKADDR*)&addr, len))
128+
return false;
129+
130+
return true;
131+
}
132+
bool BaseSocket::CheckRead()
133+
{
134+
fd_set readset;
135+
timeval timeout;
136+
timeout.tv_sec = 0;
137+
timeout.tv_usec = 0;
138+
FD_ZERO(&readset);
139+
int ret = select(FD_SETSIZE, &readset, NULL, NULL, &timeout);
140+
if (ret > 0 && FD_ISSET(_socket, &readset))
141+
{
142+
return true;
143+
}
144+
return false;
145+
}
146+
bool BaseSocket::CheckWrite()
147+
{
148+
149+
}
150+
bool BaseSocket::Receive()
151+
{
152+
return true;
153+
}
154+
bool BaseSocket::Send()
155+
{
156+
return true;
157+
}
158+
void BaseSocket::ReceiveFrom()
159+
{
160+
161+
}
162+
void BaseSocket::SendTo()
163+
{
164+
165+
}
166+
void BaseSocket::OnReceive()
167+
{
168+
169+
}
170+
void BaseSocket::OnSend()
171+
{
172+
173+
}
174+
void BaseSocket::OnSendTo()
175+
{
176+
177+
}
178+
void BaseSocket::OnReceiveFrom()
179+
{
180+
181+
}

Server/Server/BaseSocket.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <string>
3+
#include <WinSock2.h>
4+
#include "MessageType.h"
5+
#pragma comment(lib,"ws2_32.lib")
6+
using namespace std;
7+
8+
9+
class BaseSocket
10+
{
11+
private:
12+
SOCKET _socket;
13+
SocketProtoType _protoType;
14+
string _ip;
15+
unsigned short _port;
16+
bool _inited;
17+
public:
18+
BaseSocket();
19+
virtual ~BaseSocket();
20+
BaseSocket(string ip, unsigned short port, SocketProtoType prototype = SocketProtoType::PROTO_TCP);
21+
bool Bind(string ip, unsigned short port);
22+
bool Connect(string ip, unsigned short port);
23+
virtual bool Receive();
24+
virtual bool Send();
25+
virtual void ReceiveFrom();
26+
virtual void SendTo();
27+
protected:
28+
bool Init();
29+
void Clean();
30+
bool CheckRead();
31+
bool CheckWrite();
32+
virtual void OnReceive();
33+
virtual void OnSend();
34+
virtual void OnSendTo();
35+
virtual void OnReceiveFrom();
36+
};
37+

Server/Server/SyncSocket.cpp

Whitespace-only changes.

Server/Server/SyncSocket.h

Whitespace-only changes.

0 commit comments

Comments
 (0)