Enable HGE to Support Network (1)-HGE network Basics

Source: Internet
Author: User

Now I am writing code that enables HGE to support the network...

This part of code is currently being written this week .. That is, today ..

However, if you want to compile the network. You have to add a line of code in the hge. h file,

1: and add it before # include <windows. h>.

# Define WIN32_LEAN_AND_MEAN # include <windows. h>

Because our network interface will reference the file winsock2.h. This is required ..

2: many codes have reversed min and max. I don't think it's a good thing. We can write one by ourselves:

# Define t_min (x, y) (x) <(y ))? (X): (y)
# Define t_max (x, y) (x)> (y ))? (X): (y)

Instead of dropping # unidef.

3: We will make the network part run in multi-thread mode.

So the following code will use. lock. h:

# Pragma once

# Include <assert. h>

// Configure //-----------------------------------------------------------------------------------------------
// TCriticalSection
// Configure //-----------------------------------------------------------------------------------------------
Class tCriticalSection
{
Public:
// Construct
TCriticalSection () {InitializeCriticalSection (& m_CritSect );}
// Structure
~ TCriticalSection () {DeleteCriticalSection (& m_CritSect );}

Public:
// Lock the critical lock
Void Lock () {EnterCriticalSection (& m_CritSect );}
// Unlock the critical lock
Void Unlock () {LeaveCriticalSection (& m_CritSect );}

Private:
CRITICAL_SECTION m_CritSect; // critical Lock Object
};

// Configure //-----------------------------------------------------------------------------------------------
// TAutoLock class, mutex class
// Configure //-----------------------------------------------------------------------------------------------
Class tAutoLock
{
Public:
TAutoLock (tCriticalSection * pLock)
{
Assert (pLock! = NULL );

M_pLock = pLock;

PLock-> Lock ();

M_LockNums = 1;
}
~ TAutoLock ()
{
Assert (m_pLock! = NULL );

While (m_LockNums> 0)
{
M_pLock-> Unlock ();

M_LockNums --;
}
}

Void Lock ()
{
Assert (m_pLock! = NULL );

M_pLock-> Lock ();

M_LockNums ++;
}

Void Unlock ()
{
Assert (m_pLock! = NULL );

If (m_LockNums> 0)
{
M_pLock-> Unlock ();

M_LockNums --;
}
}

Private:
TCriticalSection * m_pLock;
Int m_LockNums;
};

Reprinted with the source: original BLOG (ShowLong)

Okay. Other code I am writing ing... will be published soon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.