Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Started working on network codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Mar 11, 2016
1 parent 9f2b88b commit 14a3d40
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
57 changes: 53 additions & 4 deletions Master Server/Assets/MasterServer.cs
@@ -1,9 +1,32 @@
using UnityEngine;
using UnityEngine.Networking;

using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

//http://docs.unity3d.com/Manual/UNetUsingTransport.html

public class MasterServer : MonoBehaviour {
#region Essentials
bool debugging = true;
public void debug(string msg) {
if(debugging)
print(msg);
}

public byte[] StringToByteArray(string str, Encoding encoding) {
return encoding.GetBytes(str);
}

public string ByteArrayToString(byte[] bytes, Encoding encoding) {
return encoding.GetString(bytes);
}
#endregion

#region Network
#region Network Variables
// Configuration Channels
byte reliableChanId;

Expand All @@ -13,8 +36,9 @@ public class MasterServer : MonoBehaviour {

// Communication
int connectionId;
#endregion

void Start() {
void Start() {
// Initializing the Transport Layer with no arguments (default settings)
NetworkTransport.Init();

Expand Down Expand Up @@ -82,10 +106,15 @@ out error

switch (recData) {
case NetworkEventType.DataEvent:
Stream stream = new MemoryStream(recBuffer);
BinaryFormatter formatter = new BinaryFormatter();
string message = formatter.Deserialize(stream) as string;
// Find a way to tell if it was serialized or not.
///Stream stream = new MemoryStream(recBuffer);
///BinaryFormatter formatter = new BinaryFormatter();
///string message = formatter.Deserialize(stream) as string;
string message = ByteArrayToString(recBuffer, Encoding.UTF8);
print("Message received: " + message);

byte code = Convert.ToByte(message.Substring(0, message.IndexOf(' ') + 1));
PerformAction(code, message.Substring(message.IndexOf(' ') + 1));
break;

case NetworkEventType.ConnectEvent:
Expand Down Expand Up @@ -115,4 +144,24 @@ out error
break;
}
}
#endregion

#region Action Variables
[Flags]
public enum Actions : byte {
Auth = 0x00,
Debug = 0x01 // test
}

void PerformAction(byte code, string msg) {
// Unsure if this cast will work.
switch ((Actions)code) {
case Actions.Debug:
debug("Debugging socket: " + msg);
break;

}
}
#endregion

}
83 changes: 65 additions & 18 deletions Server/Assets/Server.cs
@@ -1,10 +1,31 @@
using UnityEngine;
using UnityEngine.Networking;

using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


public class Server : MonoBehaviour {
#region Essentials
bool debugging = true;
public void debug(string msg) {
if(debugging)
print(msg);
}

public byte[] StringToByteArray(string str, Encoding encoding) {
return encoding.GetBytes(str);
}

public string ByteArrayToString(byte[] bytes, Encoding encoding) {
return encoding.GetString(bytes);
}
#endregion

#region Network
#region Network Variables
// Configuration Channels
byte reliableChanId;

Expand All @@ -16,6 +37,7 @@ public class Server : MonoBehaviour {
int masterServerId;
string masterServerIp = "72.91.241.243";
int masterServerPort = 1337;
#endregion

/// <summary>
/// Should change ports and IPs
Expand All @@ -39,15 +61,15 @@ public class Server : MonoBehaviour {

// Socket Configurations
socketId = NetworkTransport.AddHost(topology, socketPort);
print("Socket open on port " + socketPort + ": ID #" + socketId);
debug("Socket open on port " + socketPort + ": ID #" + socketId);

// Master Server Connection
masterServerId = Connect(masterServerIp, masterServerPort, socketId);
}

void Update() {
Listen();
Send("Hello Master Server"); // Think about shortening messages in to numbered codes to minimize data.
Send((byte)Actions.Debug + " Hello world!", masterServerId); // Think about shortening messages in to numbered codes to minimize data.
}

/// <summary>
Expand All @@ -61,10 +83,10 @@ public class Server : MonoBehaviour {

int newConnectionId = NetworkTransport.Connect(id, ip, port, 0, out error);
if ((NetworkError)error != NetworkError.Ok) {
print("Failed to connect because:" + (NetworkError)error);
debug("Failed to connect because:" + (NetworkError)error);
}
else {
print("Connected to server (" + ip + ":" + port + "): Connection ID #" + newConnectionId);
debug("Connected to server (" + ip + ":" + port + "): Connection ID #" + newConnectionId);
}

return newConnectionId;
Expand All @@ -73,23 +95,29 @@ public class Server : MonoBehaviour {
/// <summary>
/// Sends a message to the Master Server. It should be modified to send a message to any connection provided in the future.
/// </summary>
public void Send(string msg) {
public void Send(string msg, int connection, bool serialize = false) {
byte error;

// Serialization
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, msg);
int bufferSize = 1024;

NetworkTransport.Send(socketId, masterServerId, reliableChanId, buffer, bufferSize, out error);
if(serialize) {
Stream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, msg);
}
else {
buffer = StringToByteArray(msg, Encoding.UTF8);
}

NetworkTransport.Send(socketId, connection, reliableChanId, buffer, bufferSize, out error);

if((NetworkError)error != NetworkError.Ok) {
print("Message failed to send because: " + (NetworkError)error);
debug("Message failed to send because: " + (NetworkError)error);
}
else {
print("Message successfully sent.");
debug("Message successfully sent.");
}
}

Expand Down Expand Up @@ -120,34 +148,53 @@ out error
Stream stream = new MemoryStream(recBuffer);
BinaryFormatter formatter = new BinaryFormatter();
string message = formatter.Deserialize(stream) as string;
print("Message received: " + message);
debug("Message received: " + message);
break;

case NetworkEventType.ConnectEvent:
if (masterServerId == recConnectionId) {
print("Self-connection approved.");
debug("Self-connection approved.");
}
else {
print("Remote connection incoming.");
debug("Remote connection incoming.");
}
break;

case NetworkEventType.DisconnectEvent:
if (masterServerId == recConnectionId) {
print("Self-connection failed: " + (NetworkError)error);
debug("Self-connection failed: " + (NetworkError)error);
}
else {
print("Remote connection closed.");
debug("Remote connection closed.");
}
break;

case NetworkEventType.Nothing:
//print("Nothing received.");
//debug("Nothing received.");
break;

case NetworkEventType.BroadcastEvent:
print("Broadcast discovery event received.");
debug("Broadcast discovery event received.");
break;
}
}
#endregion

#region Actions
#region Action Variables
[Flags]
public enum Actions : byte {
Auth = 0x00,
Debug = 0x01 // test -
}
#endregion

/// <summary>
/// Send the requested auth password to the master server.
/// </summary>
/// <param name="key">The public key.</param>
void Authenticate(string key) {
Send(Actions.Auth + key, masterServerId);
}
#endregion
}
Binary file modified Server/ProjectSettings/ProjectSettings.asset
Binary file not shown.

0 comments on commit 14a3d40

Please sign in to comment.