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
1 change: 1 addition & 0 deletions Lab08-LLAPI/.~lock.LabReport08-vhaskin1.odt#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,Shori-sha/Victor,Shori-sha,28.10.2015 08:24,file:///C:/Users/Victor/AppData/Roaming/LibreOffice/4;
Binary file added Lab08-LLAPI/LabReport08-vhaskin1.odt
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/Client.prefab.meta

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

170 changes: 170 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/ClientConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
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
GlobalConfig globalConfig = new GlobalConfig ();
globalConfig.ReactorModel = ReactorModel.FixRateReactor;
globalConfig.ThreadAwakeTimeout = 10;

//Build the channel config
ConnectionConfig connectionConfig = new ConnectionConfig ();
reliableChannelID = connectionConfig.AddChannel (QosType.ReliableSequenced);
unreliableChannelID = connectionConfig.AddChannel (QosType.UnreliableSequenced);

//Create the host topology
HostTopology hostTopology = new HostTopology (connectionConfig, maxConnections);

//Initialize the network transport
NetworkTransport.Init (globalConfig);

//Open a socket for the client
clientSocketID = NetworkTransport.AddHost (hostTopology, 7777);

//Make sure the client created the socket successfully
if (clientSocketID < 0)
{
Debug.Log ("Client socket creation failed!");
}
else
{
Debug.Log ("Client socket creation success");
}
//Create a byte to store a possible error
byte 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
clientServerConnectionID =
NetworkTransport.Connect(clientSocketID, Network.player.ipAddress.ToString(), 7777, 0, out error);
//Display the error (if it did error out)
if(error != (byte)NetworkError.Ok)
{
NetworkError networkError = (NetworkError)error;
Debug.Log ("Error: " + networkError.ToString());
}
}

void Update()
{
//If the client failed to create the socket, leave this function
if (!isClientConnected)
{
return;
}

PollBasics();

//If the user pressed the Space key
//Send a message to the server "FirstConnect"
if (Input.GetKeyDown (KeyCode.Space)) {
SendMessage ("FirstConnect");
}
//If the user pressed the R key
//Send a message to the server "Random message!"
if (Input.GetKeyDown (KeyCode.R)) {
SendMessage ("Random Message!");
}
}

void SendMessage(string message)
{
//create a byte to store a possible error
byte error;
//Create a buffer to store the message
byte[] buffer = new byte[1024];
//Create a memory stream to send the information through
Stream memoryStream = new MemoryStream (buffer);
//Create a binary formatter to serialize and translate the message into binary
BinaryFormatter binaryFormatter = new BinaryFormatter ();

//Serialize the message
binaryFormatter.Serialize (memoryStream, message);

//Send the message from this client, over the client server connection, using the reliable channel
NetworkTransport.Send (clientSocketID, clientServerConnectionID, reliableChannelID, buffer, (int)memoryStream.Position, out error);

//Display the error (if it did error out)
if(error != (byte)NetworkError.Ok)
{
NetworkError networkError = (NetworkError)error;
Debug.Log ("Error: " + networkError.ToString());
}
}

void InterperateMessage(string message)
{
//if the message is "goto_NewScene"
//load the level named "Scene2"
if (message == "goto_NewScene")
Application.LoadLevel ("Scene2");
}

void PollBasics()
{
//prepare to receive messages by practicing good bookkeeping
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 teh message can be
byte[] buffer = new byte[1024]; //the actual message
byte error; //if there is an error

NetworkEventType networkEvent = NetworkEventType.DataEvent;

//do
do{
//Receive network events
networkEvent = NetworkTransport.Receive(out recHostId, out connectionID, out channelId,
buffer, 1024, out dataSize, out error);
//switch on the network event types
switch(networkType)
{
//if nothing, do nothing
case NetworkEventType.Nothing:
break;
//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
case NetworkEventType.ConnectEvent:
if(recHostId == clientSocketID)
{

}
break;
//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*/
case NetworkEventType.DataEvent:
break;
//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
case NetworkEventType.DisconnectEvent:
break;
default:
break;
}
//while (the network event I am receiving is not Nothing)
while(networkType != NetworkEventType.Nothing);
}
12 changes: 12 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/ClientConnection.cs.meta

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

Binary file added Lab08-LLAPI/New Unity Project/Assets/Sample.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/Sample.unity.meta

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

Binary file added Lab08-LLAPI/New Unity Project/Assets/Scene2.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/Scene2.unity.meta

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

140 changes: 140 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/ServerConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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)
{
return;
}

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 teh 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:
//Server received disconnect event
if(recHostId == serverSocketID){
Debug.Log ("Server:Player " + connectionID.ToString() + " connected!");
}
break;
case NetworkEventType.DataEvent: //verify the server is the intended target
if(recHostId == serverSocketID)
{
//Open a memory stream with a size equal to the buffer we set up earlier
Stream memoryStream = new MemoryStream(buffer);

//Create a binaryformatter to begin reading the information from the memory string
BinaryFormatter binaryFormatter = new BinaryFormatter();

//Create a binaryformatter to begin reading the information stored in the memory string
//then convert that into a string
string message = binaryFormatter.Deserialize(memoryStream).ToString();

//debug out the message you worked so hard to figure out!
Debug.Log ("Server: Received Data from " +
connectionID.ToString() +
"! Message: " + message);

RespondMessage(message, connectionID);//who send the message so the server can respond to that specific person
}
break;
case NetworkEventType.DisconnectEvent:
//Server received disconnect event
if(recHostId == serverSocketID)
{
Debug.Log ("Server: Received disconnect from " + connectionID.ToString());
}
break;
default:
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);

// Who is sending, where to, what channel, what info, how much info, if there is an error
NetworkTransport.Send (serverSocketID, target, reliableChannelID, buffer, (int)memoryStream.Position, out error);

if(error != (byte)NetworkError.Ok) //error is always assigned, and it uses Ok to notate that there is nothing wrong
{
NetworkError networkError = (NetworkError) error;
Debug.Log ("Error: " + networkError.ToString());
}
}

void RespondMessage(string message, int playerID)
{
//Student will fill this out.
if (message == "FirstConnect") {
Debug.Log("Received a First Connection message from Player ID " + playerID);

SendMessage ("goto_NewScene", playerID);
}
}

}
12 changes: 12 additions & 0 deletions Lab08-LLAPI/New Unity Project/Assets/ServerConnection.cs.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.
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.