Skip to content

Network

高凯 edited this page Feb 22, 2018 · 1 revision

Introduction

   NetWorkManger encapsulates common Socket connections (TCP, UDP), and built-in Json and Protocol protocols, and provides an interface to support protocol development.

API

   NetworkManager
static void Init <TProtocol, TSocket> (ProtocolType protocolType)   Initialize NetWorkManager, set the connection method and communication protocol   TProtocol: INetworkInterface   TSocket: SocketBase

static void Dispose () Release NetworkManager

static void SetServer (string IP, int port) Set IP and port

static void Connect () to establish a connection

static void DisConnect () Disconnects

static void SendMessage (string messageType, Dictionary <string, object> data) Send a message

static void SendMessage (Dictionary <string, object> data) Sends a message to retrieve the 'message' field in data as the messageType

Json communication protocol

   The data is transmitted in a string format. The messages end with an ampersand. If the message contains '&', it is replaced by "<FCP: AND>" and the Dictionary sent by NetworkManager is serialized as Json.

Protocol communication protocol

   Data is transmitted in binary format in the form of Google Protocol Buffer, which supports Int8 and Int16 for bandwidth savings, tools for generating Protocol files, and data parsing classes.

Self-generated tool usage

Tools -> Protocol -> C # to protocol c # generate protocol file
Tools -> Protocol -> protocol to c # According to the protocol file generated c # category
Tools -> Protocol -> 生成解析代码 (Generate the parsing code automatically) generated from the message into a message class parsing code
Tools -> Protocol -> 生成空解析文件 (Generate an empty parsing file) Clear all the generated code, but keep the call interface
Tools -> Protocol -> 清空文件夹 (Clear Folder Clears) all generated files

Automatic generation tool will get to the project CsharpProtocolInterface and IProtocolStructInterface subclass, generate the corresponding protocol file,
The following features are available:
Int16Attribute specifies that a field be transmitted as a 16-bit Int
Int8Attribute specifies that a field is transmitted as 8-bit Int

ModuleAttribute statement which message belongs to a certain module, explicitly specify the module number
MessageModeAttribute statement of a message is ToClient or ToServer

Example

//establish connection
NetworkManager.Init <ProtocolNetworkService, SocketService> (System.Net.Sockets.ProtocolType.Tcp);
NetworkManager.SetServer (data.m_Address, data.m_port);
NetworkManager.Connect ();
//Send a message
Dictionary <string, object> msg = new Dictionary <string, object> ();
msg.Add ("MT", "testMsg");
msg.Add ("content", "hello");
NetworkManager.SendMessage (msg);
// Receive the message
InputManager.AddListener <InputNetworkMessageEvent> (ReceviceMsg);
InputManager.RemoveListener <InputNetworkMessageEvent> (ReceviceMsg);
public void ReceviceMsg (InputNetworkMessageEvent msg)
{
    Debug.Log (msg.m_MessgaeType);
    Debug.Log (msg.Data ["content"]);
}
// send the message by generating the code
PlayerLoginMsg_s msg = new PlayerLoginMsg_s ();
msg.playerID = id;
msg.nickName = nickName;
ProtocolAnalysisService.SendCommand (msg);
// Receive the message by generating the code
ProtocolAnalysisService.Init (); // This step must be done
GlobalEvent.AddTypeEvent <PlayerLoginMsg_c> (ReceviceLoginMsg);
GlobalEvent.RemoveTypeEvent <PlayerLoginMsg_c> (ReceviceLoginMsg);
ProtocolAnalysisService.Dispose ();

void ReceviceLoginMsg (PlayerLoginMsg_c e, params object [] obj)
{
    Debug.Log ("ReceviceLoginMsg");
}

Tip

Front-end framework does not provide server code, if necessary, can refer to
JAVA Service Framework
UnityLockStepDemo

Clone this wiki locally