473,809 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My_CSockets::On Receive(int nErrorCode){ delete this;} /* stupid or smart? */

Hello everyone,

This is my first time posting, up until now i've only used google to seach
:)

I'm creating a multiplayer minesweeper game in Windows, so I have been
reading up on CSockets. I am begining to have a grasp on them, but I have
this one query.

If a socket some data comes along that isnt part of my protocol, I want to
remove the close the socket and delete it. The main question is on a
OnReceive call can you tell the socket to delete itself?

The CSockets have OnReceive functions, my understanding of them is that
you implement them yourself and they then get called when any data is
sitting
on the Socket (i like this far better than an infinitely better idea than
blocking
my game's process :).

If this does get called from Windows, then it is not apart of my game's
process, it runs parallel with it but does have access to all the functions.
I
haven't actually tested if this is true yet. If someone can verify that my
understanding of how the OnReceive function works, that would be great.

So it's like this
// Multi-mine protocol CSocket
MMPCSocket::OnR eceive(int nErrorCode){
Network::MMP msg; //enum for all the allowable protocol numbers.
int bytes = Receive(&msg, sizeof(n->MMP));

if(bytes == sizeof(n->MMP){ // read the correct number of bytes
network->processPending Message(msg);
}
else{
msg = network->mmp_server_rec eived_bad_packe t; //part of
MMP
Send(&msg, sizeof(n->MMP));
network->deleteSocket(p layer);
/* player is the reference to this socket */
}
}

Network::delete Socket(int player){
delete myConnections[player];
myConnections[player] = NULL;
/* my current idea is that if a socket is null, means it can be reused
for a new player */
}

Does that cause problems from the perspective of the game?
I could imagine that there is a small possibility that just at this
moment when I'm trying to delete it... my actual game is trying to send
a message to it.

Like within my game, it's going

For(all my players)
If(myConnection s[player] != NULL){
/* meanwhile, the deleteSocket has been run and
it's gone and deleted the socket */
int answer = myConnections[player].Send(some data, some
number);
}

I don't see this as really being a problem.. because if the socket has
been deleted, the Send message will return a WSANOTINITIALIS ED error to
the GetLastError function. Whether I check or care about that is up to
how I create my program, hopefully it wouldn't be necessary and the
Network layer would have taken care of all that i.e. told all players
that player x is gone now).

The same goes if later on in the processPendingM essage part.. it finds
out that the message is actually bogus.. it would also call the
deleteSocket(pl ayer).

Is this a smart way to go about things? An alternative to this would be just
to
Close the CSocket from within the OnReceive function.. then later on.. in my
game process, every second or so.. check if any CSockets are closed, and
delete them as required...

Thanks for reading all the way to the end :)
Jul 19 '05 #1
1 2333
Clive wrote:
Hello everyone,

This is my first time posting, up until now i've only used google to seach
:)

I'm creating a multiplayer minesweeper game in Windows, so I have been
reading up on CSockets. I am begining to have a grasp on them, but I have
this one query.

If a socket some data comes along that isnt part of my protocol, I want to
remove the close the socket and delete it. The main question is on a
OnReceive call can you tell the socket to delete itself?


CSocket is not part of the standard C++. This group is for standard C++
questions only.

Perhaps you could find help from:

microsoft.publi c.vc.mfc

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
15733
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if (--references_ == 0) { delete this;
10
2172
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
5
1650
by: Yongming Wang | last post by:
FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me? class A { public: void FuncA() { B b(this);
32
3214
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
10
1475
by: n2xssvv g02gfr12930 | last post by:
In a job interview I was asked about the statement below: delete this; Naturally I was horrified, yet they claimed they had used it. Personally I'm pretty damn sure I could never justify this. So is this a case of Lord Bryon, "Mad, bad, and dangerous to know", or do you think you could ever justify using it? JB
16
10354
by: Martin Vorbrodt | last post by:
some butthole asked me an interview question: can you delete "this" pointer in a member function of a class, like this: class C { public: void foo() { delete this; } }; my answer was... undefined behavior, maybe? object kills itself and frees
31
8493
by: Anamika | last post by:
Hello friends.... can anyone tell me what will happen when we do..."delete this"...
4
1142
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Just interested to learn how C++ implements delete this statement internally. From logical point of view, I can not imagine how to implement a component which destroy itself. What makes me confused is the component is relying on itself even in the process of destroy, how could it destroys itself from itself?
1
1687
by: MorgyTheMole | last post by:
I have a base class: class A { protected: static void* operator new (size_t size); void operator delete (void *p); public: void Delete(); }
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.