init networking, connecting and packet sending is now possible

This commit is contained in:
Sofia 2017-05-08 01:59:50 +03:00
parent 2bb1df00ee
commit bb8d9889a1
19 changed files with 1862 additions and 4 deletions

3
.gitignore vendored
View File

@ -46,3 +46,6 @@ sysinfo.txt
# Emacs # # Emacs #
*~ *~
# Distribution #
/Dist/

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d74936181e8335c46a02a1bb60cba54e
folderAsset: yes
timeCreated: 1494188604
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Client : MonoBehaviour {
NetworkClient client;
private bool running = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (client.isConnected) {
}
}
public void LaunchClient(string ip, int port) {
if (running) {
return;
}
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);
config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(config, 10);
client = new NetworkClient();
client.Configure(config, 10);
running = true;
client.RegisterHandler(MsgType.Connect, OnConnected);
client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
client.RegisterHandler(MsgType.Error, OnError);
client.Connect(ip, port);
Debug.Log("Client launched!");
}
public void OnConnected(NetworkMessage msg) {
Debug.Log("Connected!");
client.Send(PktType.TestMessage, new TextMessage("Hai, I connected!"));
}
public void OnDisconnected(NetworkMessage msg) {
Debug.Log("Disconnected!");
}
public void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error. Shutting down.");
client.Disconnect();
running = false;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 59c7e1a1c51593e4093238f13c713947
timeCreated: 1494189789
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e8b8f48aeaed80f42b444b6498fe859d
folderAsset: yes
timeCreated: 1494190696
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,22 @@
using UnityEngine.Networking;
public class TextMessage : MessageBase {
public string message;
public TextMessage(string message) {
this.message = message;
}
public TextMessage() {
}
public override void Deserialize(NetworkReader reader) {
message = reader.ReadString();
}
public override void Serialize(NetworkWriter writer) {
writer.Write(message);
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e1fce6b5facaaff4899461bf7b3c69be
timeCreated: 1494190706
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PktType {
public const short TestMessage = 200;
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 430a933ca3f0fa849bc55f725fe9cffc
timeCreated: 1494190350
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,64 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/**
* This class is used pretty much anywhere in order to make the "first step" of networking.
* It adds the proper components to World Root and tells them to start.
* */
public class NetworkEstablisher : MonoBehaviour {
[Tooltip("Required field only if StartClient() is used.")]
public InputField IPField;
[Tooltip("Required field only if StartClient() is used.")]
public InputField clientPortField;
[Tooltip("Required field only if StartServer() is used.")]
public InputField serverPortField;
public GameObject worldRoot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void StartClient() {
string ip = IPField.text;
if (ip.Length == 0) {
ip = "localhost";
}
string portText = clientPortField.text;
int port = 3935;
if (portText.Length > 0) {
port = Int32.Parse(portText);
}
StartClient(ip, port);
}
public void StartClient(string ip, int port) {
Client client = worldRoot.AddComponent<Client>();
client.LaunchClient(ip, port);
}
public void StartServer() {
string portText = serverPortField.text;
int port = 3935;
if (portText.Length > 0) {
port = Int32.Parse(portText);
}
StartServer(port);
}
public void StartServer(int port) {
Server server = worldRoot.AddComponent<Server>();
server.LaunchServer(port);
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 556e33cbb85299f4db702c15ab869ad7
timeCreated: 1494188614
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Server : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void LaunchServer(int port) {
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);
config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(config, 10);
NetworkServer.Listen(port);
NetworkServer.RegisterHandler(PktType.TestMessage, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetworkServer.RegisterHandler(MsgType.Error, OnError);
Debug.Log("Server started on port " + port);
}
public void HandlePacket(NetworkMessage msg) {
switch(msg.msgType) {
case PktType.TestMessage:
TextMessage textMsg = new TextMessage();
textMsg.Deserialize(msg.reader);
Debug.Log("Received message: " + textMsg.message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
break;
}
}
public void OnConnected(NetworkMessage msg) {
Debug.Log("Someone connected!");
}
public void OnDisconnected(NetworkMessage msg) {
Debug.Log("Someone disconnected?");
}
public void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error on server");
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8c71a508208a2fe4fa5ba111e3b8b540
timeCreated: 1494189796
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,4 +4,7 @@
EditorBuildSettings: EditorBuildSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 2 serializedVersion: 2
m_Scenes: [] m_Scenes:
- enabled: 1
path: Assets/Scenes/TestMap.unity
guid: 1e552a0d1c3eb4ea994f34bf49696c90

View File

@ -35,6 +35,7 @@ GraphicsSettings:
- {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: [] m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0} type: 0}

View File

@ -66,7 +66,7 @@ PlayerSettings:
disableDepthAndStencilBuffers: 0 disableDepthAndStencilBuffers: 0
defaultIsFullScreen: 1 defaultIsFullScreen: 1
defaultIsNativeResolution: 1 defaultIsNativeResolution: 1
runInBackground: 0 runInBackground: 1
captureSingleScreen: 0 captureSingleScreen: 0
muteOtherAudioSources: 0 muteOtherAudioSources: 0
Prepare IOS For Recording: 0 Prepare IOS For Recording: 0

View File

@ -1 +1 @@
m_EditorVersion: 5.6.0xf3Linux m_EditorVersion: 5.6.0f3

View File

@ -46,6 +46,6 @@ Version 0.1.0 - Gigantic
- [x] Spawning basic capsule characters. - [x] Spawning basic capsule characters.
- [x] ...With movement capabilities. - [x] ...With movement capabilities.
- [ ] Client-server connectivity, players can see each other moving around. - [ ] Client-server connectivity, players can see each other moving around.
- [ ] 1. Client-server connectivity: the clients can connect to the server and send some messages back and forth. - [x] 1. Client-server connectivity: the clients can connect to the server and send some messages back and forth.
- [ ] 2. Players can see eachothers capsule-characters and send commands to move them around. - [ ] 2. Players can see eachothers capsule-characters and send commands to move them around.
- [ ] 3. Basic syncing (Send sync packages at certain intervals) in order to sync positions correctly between server and client. - [ ] 3. Basic syncing (Send sync packages at certain intervals) in order to sync positions correctly between server and client.