private void onData(int ConnectionId, int channelId, int recHostId, nMsg msg) { switch (msg.OP) //Server Recieves much data than the client { case NetOP.None: Debug.Log("Incorrect initialisation"); break; case NetOP.Position: n_Position Decode = (n_Position)msg; //Player.NetworkInstance.ObjectID = (int)Decode.Position.x; //Player.ID = (int)Decode.Position.x; //checkMoving.PlayerID = Player.ID; //Test.transform.position = ((n_Position)msg).Position+OffsetDistance; break; case NetOP.SyncSceneObjects: Debug.Log("SceneObjects"); UpdatedObjectContainer temp = (UpdatedObjectContainer)msg; checkMoving.SyncObjects(temp.SceneObjects); break; case NetOP.NewPlayer: nPlayerNew PlayerType = (nPlayerNew)msg; // spawn.Spawn(true, PlayerType.ID); break; case NetOP.Action: nAction ActionToggle = (nAction)msg; crystalize.crystalize(); break; } }
public void SendClients(nMsg msg) //Send the Client Data of any type as long as it is serialisable { if (Clients.Count > 0) { for (int i = 0; i < Clients.Count; i++) { nMsg temp = msg; SendClient(rechost, Clients[0], temp); } } }
public void SendServer(nMsg msg) { //Hold data before sending it byte[] buffer = new byte[ByteSize]; //crush data into a byte array (packet) BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer); //designate memory from ram to load this into msg formatter.Serialize(ms, msg); NetworkTransport.Send(hostID, connectionId, ReliableChannel, buffer, ByteSize, out error); //NetworkTransport.SendQueuedMessages(hostID, connectionId, out error); }
void UpdateMessagePump() //Check All Network Events in case of tranmission { if (!Started) { return; } int recHostId; //Standalone/web e.t.c int connectionId; //Which user int channelId; //Which lane byte[] recBuffer = new byte[ByteSize]; //recieving buffer int dataSize; byte error; NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, ByteSize, out dataSize, out error); switch (type) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: connected = true; break; case NetworkEventType.DisconnectEvent: Debug.Log(string.Format("Disconnected", connectionId)); connected = false; break; case NetworkEventType.DataEvent: BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(recBuffer); //designate memory from ram to load this into msg nMsg msg = (nMsg)formatter.Deserialize(ms); onData(connectionId, channelId, recHostId, msg); //Decode this data and execute appropriately break; default: case NetworkEventType.BroadcastEvent: Debug.Log("Unexpected network event type"); break; } }
private void SendClient(int recHost, int connectionID, nMsg msg) //Send the Client Data of any type as long as it is serialisable { //Hold data before sending it byte[] buffer = new byte[ByteSize]; //crush data into a byte array (packet) BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer); //designate memory from ram to load this into msg formatter.Serialize(ms, msg); //Standalone client // if(((UpdatedObjectContainer)msg).SceneObjects.Count>1) //It doesn't seem to be the host? // { // Debug.Log("I'm here"); // } NetworkTransport.Send(hostID, connectionID, ReliableChannel, buffer, ByteSize, out error); // NetworkTransport.SendQueuedMessages(hostID, connectionID, out error); }
//Data is sent here and stored as a NetOP so it can be distinguished as the correct action private void onData(int ConnectionId, int channelId, int recHostId, nMsg msg) { switch (msg.OP) //Server Recieves much data than the client { case NetOP.None: Debug.Log("Incorrect initialisation"); break; case NetOP.Position: Debug.Log("New Position"); Player1Proper.transform.position = ((n_Position)msg).Position; Player1Proper.GetComponent <standOnBoomerang>().forcedOff(); break; case NetOP.SyncSceneObjects: //recieve objects and sync unity objects of the correct ID ObjectManager.SyncObjects(((UpdatedObjectContainer)msg).SceneObjects); break; case NetOP.Action: nAction ActionToggle = (nAction)msg; crystalize.crystalize(); break; } }
//public method for sending any data of your choosing to the client public void SendClientData(nMsg msg) { SendClient(rechost, ClientID, msg); }
public void UpdateMessagePump() { if (!Started) //Don't do this stuff if you don't need to { return; } int recHostId; //Host ID int connectionId; //User ID int channelId; //Which lane is this occuring through byte[] recBuffer = new byte[ByteSize]; //recieving buffer int dataSize; //byte error; //Decode incoming data NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, ByteSize, out dataSize, out error); switch (type) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: Debug.Log(string.Format("User {0} has connected from host {1}", connectionId, recHostId)); ClientID = connectionId; Clients.Add(ClientID); rechost = recHostId; //player.Spawn(true,connectionId); n_Position SendID = new n_Position(); SendID.Position = new SerialVector3(connectionId, 0, 0); SendClient(rechost, connectionId, SendID); nPlayerNew playerNew = new nPlayerNew(); playerNew.ID = connectionId; SendClients(playerNew); //nPlayerNew playerNew = new nPlayerNew(); //Connect = true; // Connection.text = "Client Connected"; // ConnectionConfig cc = new ConnectionConfig(); //ReliableChannel = cc.AddChannel(QosType.UnreliableSequenced); break; case NetworkEventType.DisconnectEvent: Debug.Log(string.Format("User {0} has disconnected", connectionId)); // Connection.text = "Client Disconnected"; // Connect = false; break; case NetworkEventType.DataEvent: BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(recBuffer); //designate memory from ram to load this into msg nMsg msg = (nMsg)formatter.Deserialize(ms); //Decode this data and deserialise it into something recognisable onData(connectionId, channelId, recHostId, msg); break; default: case NetworkEventType.BroadcastEvent: Debug.Log("Unexpected network event type"); break; } }