-
Notifications
You must be signed in to change notification settings - Fork 0
/
Socket.cpp
69 lines (53 loc) · 1.63 KB
/
Socket.cpp
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
#include "stdafx.h"
#include "Socket.h"
Socket::Socket() {
}
void Socket::setMyID(int &id) { myID = id; }
int Socket::getMyID()const { return myID; }
void Socket::setFD(SOCKET &fd) { m_fd = fd; }
SOCKET Socket::getFD()const { return m_fd; }
void Socket::setCP(HANDLE cp) { m_cp = cp; }
void Socket::setChatManager(chatManager* mngr) {cmngr=mngr; }
chatManager*Socket::getChatMngr() { return cmngr; }
void Socket::AssingCP() {
CreateIoCompletionPort((HANDLE)m_fd, m_cp, (ULONG_PTR)this, 0);
}
void Socket::onRead() {
getChatMngr()->HandlePacket(this);
}
void Socket::Disconnect() {
getChatMngr()->onDisconnect(this);
closesocket(m_fd);
}
void Socket::Send_(Packet& pkt) {
pkt.setPacket(sendpkt.data);
sendpkt.slen = PACKETSIZE;
setSendEvent();
sendpkt.slen = 0;
}
void Socket::setRecvEvent() {
WSABUF wsa;
DWORD len = 0, flags = 0;
wsa.buf = recvpkt.data;
wsa.len = sizeof(Packet);
m_readevent.Reset(Event::RECVD);
if (WSARecv(m_fd, &wsa, 1, &len, &flags,&m_readevent.m_ov, 0)) {
int err = WSAGetLastError();
if (err != WSA_IO_PENDING) {
Disconnect();
}
}
}
void Socket::setSendEvent() {
std::lock_guard<std::mutex> lock(mMutex);
WSABUF wsa;
DWORD len = 0, flags = 0;
wsa.buf = sendpkt.data;
wsa.len = sendpkt.slen; //this very important! if we sended data we should set this to 0 or this gonna fuck up program.
m_sendevent.Reset(Event::SENDD);
int result =WSASend(m_fd, &wsa, 1, &len, flags,&m_sendevent.m_ov, 0);
int err = WSAGetLastError();
if (err != WSA_IO_PENDING) {
//disconnect event
}
}