Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions WHARGARBL/WHARRGARBL/Assets/ClientConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.Networking;

public class ClientConnection : MonoBehaviour {
int clientSocketID = -1;
//Will store the unique identifier of the session that keeps the connection between the client
//and the server. You use this ID as the 'target' when sending messages to the server.
int clientServerConnectionID = -1;
int maxConnections = 10;
byte unreliableChannelID;
byte reliableChannelID;
bool isClientConnected = false;

void Start()
{
DontDestroyOnLoad(this);

//Build the global config

//Build the channel config

//Create the host topology

//Initialize the network transport

//Open a socket for the client

//Make sure the client created the socket successfully

//Create a byte to store a possible error

//Connect to the server using
//int NetworkTransport.Connect(int socketConnectingFrom, string ipAddress, int port, 0, out byte possibleError)
//Store the ID of the connection in clientServerConnectionID

//Display the error (if it did error out)
}

void Update()
{
//If the client failed to create the socket, leave this function

PollBasics();

//If the user pressed the Space key
//Send a message to the server "FirstConnect"

//If the user pressed the R key
//Send a message to the server "Random message!"
}

void SendMessage(string message)
{
//create a byte to store a possible error
//Create a buffer to store the message
//Create a memory stream to send the information through
//Create a binary formatter to serialize and translate the message into binary
//Serialize the message

//Send the message from this client, over the client server connection, using the reliable channel

//Display the error (if it did error out)
}

void InterperateMessage(string message)
{
//if the message is "goto_NewScene"
//load the level named "Scene2"
}

void PollBasics()
{
//prepare to receive messages by practicing good bookkeeping

//do
//Receive network events
//switch on the network event types
//if nothing, do nothing
//if connection
//verify that the message was meant for me
//debug out that i connected to the server, and display the ID of what I connected to
//set my bool that is keeping track if I am connected to a server to true
//if data event
//verify that the message was meant for me and if I am connected to a server
//decode the message (bring it through the memory stream, deseralize it, translate the binary)
//Debug the message and the connection that the message was sent from
InterperateMessage(//the message to interperate);
//if disconnection
//verify that the message was meant for me, and that I am disconnecting from the current connection I have with the server
//debug that I disconnected
//set my bool that is keeping track if I am connected to a server to false
//while (the network event I am receiving is not Nothing)
}
12 changes: 12 additions & 0 deletions WHARGARBL/WHARRGARBL/Assets/ClientConnection.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions WHARGARBL/WHARRGARBL/Assets/ServerConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.Networking;

public class ServerConnection : MonoBehaviour {


int serverSocketID = -1;
int maxConnections = 10;
byte unreliableChannelID;
byte reliableChannelID;
bool serverInitialized = false;

// Use this for initialization
void Start ()
{
DontDestroyOnLoad(this);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.ReactorModel = ReactorModel.FixRateReactor;
globalConfig.ThreadAwakeTimeout = 10;

ConnectionConfig connectionConfig = new ConnectionConfig();
reliableChannelID = connectionConfig.AddChannel(QosType.ReliableSequenced);
unreliableChannelID = connectionConfig.AddChannel(QosType.UnreliableSequenced);

HostTopology hostTopology = new HostTopology(connectionConfig, maxConnections);

NetworkTransport.Init(globalConfig);

serverSocketID = NetworkTransport.AddHost(hostTopology, 7777);

if(serverSocketID < 0)
{
Debug.Log("Server socket creation failed!");
}
else
{
Debug.Log("Server socket creation success!");
serverInitialized = true;
}
}

// Update is called once per frame
void Update ()
{
if (serverInitialized)
{
int recHostID; //Who received the message
int connectionID; //who sent the message
int channelID; //what channel the message was sent from
int dataSize; //how large the message can be
byte[] buffer = new byte[1024]; //the actual message
byte error; //if there is an error

NetworkEventType networkEvent = NetworkEventType.DataEvent;

do
{
networkEvent = NetworkTransport.Receive(out recHostID, out connectionID, out channelID, buffer, 1024, out dataSize, out error);

switch (networkEvent)
{
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
if (recHostID == serverSocketID)
{
Debug.Log("Server: Player " + connectionID.ToString() + " connected!");
}
break;
case NetworkEventType.DataEvent:
if (recHostID == serverSocketID)
{
Stream memoryStream = new MemoryStream(buffer);

BinaryFormatter binaryFormatter = new BinaryFormatter();

string message = binaryFormatter.Deserialize(memoryStream).ToString();

Debug.Log("Server: Received Data from " + connectionID.ToString() + "! Message: " + message);

RespondMessage(message, connectionID);
}
break;

case NetworkEventType.DisconnectEvent:
if (recHostID == serverSocketID)
{
Debug.Log("Server: Received disconnect from " + connectionID.ToString());
}
break;
}
} while (networkEvent != NetworkEventType.Nothing);
}
}

void SendMessage(string message, int target)
{
byte error;
byte[] buffer = new byte[1024];
Stream memoryStream = new MemoryStream(buffer);
BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(memoryStream, message);

NetworkTransport.Send(serverSocketID, target, reliableChannelID, buffer, (int)memoryStream.Position, out error);

if (error != (byte)NetworkError.Ok)
{
NetworkError networkError = (NetworkError)error;
Debug.Log("Error: " + networkError.ToString());
}
}

void RespondMessage(string message, int clientID)
{
if (message == "FirstConnect")
{
Debug.Log("Server: Received First Connect message from " + clientID);
SendMessage("goto_NewScene", clientID);
if (Application.loadedLevelName != "Scene2")
{
Application.LoadLevel("Scene2");
}
}
}
}
12 changes: 12 additions & 0 deletions WHARGARBL/WHARRGARBL/Assets/ServerConnection.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added WHARGARBL/WHARRGARBL/Assets/baseScene.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions WHARGARBL/WHARRGARBL/Assets/baseScene.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions WHARGARBL/WHARRGARBL/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
m_EditorVersion: 5.1.2f1
m_StandardAssetsVersion: 0
Binary file not shown.
Binary file not shown.
Binary file not shown.