public void Initialize(string id) { LNSClientParameters clientParameters = new LNSClientParameters(id, System.Environment.UserName); LNSConnectSettings settings = new LNSConnectSettings(); settings.gameKey = "com.hybriona.multiplayertest"; settings.gameVersion = Application.version; if (connectToLocalServer) { settings.serverIp = "127.0.0.1"; } else { settings.serverIp = "45.55.33.88"; } settings.serverPort = 10002; settings.serverSecurityKey = serverKey; //settings.serverIp = "192.168.0.100"; connector = new LNSConnector(clientParameters, settings, this); if (writer == null) { writer = new LNSWriter(); } writer.Reset(); connector.onPlayerConnected = OnPlayerConnected; connector.onPlayerDisconnected = OnPlayerDisconnected; connector.onMasterClientUpdated += (LNSClient client) => { Debug.Log("Masterclient changed to " + client.id); }; }
public bool SendCachedDataToAll(string key, LNSWriter m_writer) { if (isConnected && isInActiveRoom) { if (isLocalPlayerMasterClient) { new Thread(() => { lock (thelock) { writer.Reset(); writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_CACHE); writer.Put(key); writer.Put(m_writer.Data); peer.Send(writer, DeliveryMethod.ReliableOrdered); //peer.Send(writer, deliveryMethod); } }).Start(); return(true); } else { throw new Exception("Only master client can send Cached Data to server"); } } return(false); }
public bool RaiseEventOnMasterClient(ushort eventCode, LNSWriter m_writer, DeliveryMethod deliveryMethod) { if (isLocalPlayerMasterClient) { return(false); } return(RaiseEventOnClient(_lastConnectedRoomMasterClient, eventCode, m_writer, deliveryMethod)); }
public bool RaiseEventOnAll(ushort eventCode, LNSWriter m_writer, DeliveryMethod deliveryMethod) { if (isConnected && isInActiveRoom) { writer.Reset(); writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_NOCACHE); writer.Put(eventCode); writer.Put(m_writer.Data, 0, m_writer.Length); peer.Send(writer, deliveryMethod); return(true); } return(false); }
public FakeClient(string ip, int port, string id) { LNSClientParameters clientParameters = new LNSClientParameters(id, null); LNSConnectSettings connectSettings = new LNSConnectSettings(); connectSettings.serverIp = ip; connectSettings.serverPort = port; connectSettings.serverSecurityKey = "iamatestserver"; connectSettings.gameKey = "hybriona.ccutest"; writer = new LNSWriter(); connector = new LNSConnector(clientParameters, connectSettings, this); connector.onConnected += () => { connector.JoinRoomOrCreateIfNotExist("test", 10000); }; connector.Connect(); }
public bool RaiseEventOnClient(string clientid, ushort eventCode, LNSWriter m_writer, DeliveryMethod deliveryMethod) { if (isConnected && isInActiveRoom) { new Thread(() => { lock (thelock) { writer.Reset(); writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_TO_CLIENT); writer.Put(clientid); writer.Put(eventCode); if (m_writer.Data.Length > 0) { writer.Put(m_writer.Data); } peer.Send(writer, deliveryMethod); //peer.Send(writer, deliveryMethod); } }).Start(); return(true); } return(false); }
//public bool RaiseEventOnAllAndCache(ushort eventCode, LNSWriter m_writer) //{ // if (isConnected && isInActiveRoom) // { // new Thread(() => // { // lock (thelock) // { // writer.Reset(); // writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_CACHE); // writer.Put(eventCode); // writer.Put(m_writer.Data); // peer.Send(writer,DeliveryMethod.ReliableOrdered); // } // }).Start(); // return true; // } // return false; //} //public bool RaiseEventRemoveMyCache() //{ // if (isConnected && isInActiveRoom) // { // new Thread(() => // { // lock (thelock) // { // //writer.Reset(); // //writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_CACHE); // //writer.Put(eventCode); // //writer.Put(m_writer.Data); // //peer.Send(writer, DeliveryMethod.ReliableOrdered); // } // }).Start(); // return true; // } // return false; //} //public bool RaiseEventRemoveAllCache() //{ // if (isConnected && isInActiveRoom && isLocalPlayerMasterClient) // { // new Thread(() => // { // lock (thelock) // { // writer.Reset(); // writer.Put(LNSConstants.SERVER_EVT_REMOVE_ALL_CACHE); // peer.Send(writer, DeliveryMethod.ReliableOrdered); // //writer.Put(eventCode); // //writer.Put(m_writer.Data); // } // }).Start(); // return true; // } // return false; //} public bool RaiseEventOnClient(LNSClient client, ushort eventCode, LNSWriter m_writer, DeliveryMethod deliveryMethod) { return(RaiseEventOnClient(client.id, eventCode, m_writer, deliveryMethod)); }
/// <summary> /// RaiseEventOnAll but with Quad tree optimizations /// </summary> /// <param name="eventCode"></param> /// <param name="m_writer"></param> /// <param name="deliveryMethod"></param> /// <returns></returns> public bool RaiseEventOnNearby(ushort eventCode, Vector2 position, float extends, LNSWriter m_writer, DeliveryMethod deliveryMethod) { if (isConnected && isInActiveRoom) { writer.Reset(); writer.Put(LNSConstants.SERVER_EVT_RAW_DATA_TO_NEARBY_CLIENTS); Rect searchRect = new Rect(Vector3.zero, new Vector2(extends * 2, extends * 2)); searchRect.center = position; writer.Put(searchRect.x); writer.Put(searchRect.y); writer.Put(searchRect.size.x); writer.Put(searchRect.size.y); //Debug.LogFormat("From {0} - Search Rect {1},{2} {3},{4} - Position {5},{6}", "Client", searchRect.x, searchRect.x, searchRect.width, searchRect.height, position.x, position.y); writer.Put(eventCode); writer.Put(m_writer.Data, 0, m_writer.Length); peer.Send(writer, deliveryMethod); return(true); } return(false); }
public void StartProcess(string id) { clonePrefab.SetActive(false); writer = new LNSWriter(); player.GetComponent <SphereGizmos>().radius = searchExtends; if (name == "Client") { id = "Client"; player.localPosition = Vector3.zero; } StartCoroutine(RandomMovement()); player.localPosition = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * mapSize * .5f; LNSClientParameters clientParameters = new LNSClientParameters(id, gameObject.name); LNSConnectSettings connectSettings = new LNSConnectSettings(); if (useLive) { connectSettings.serverIp = liveip; } else { connectSettings.serverIp = ip; } connectSettings.serverPort = port; connectSettings.serverSecurityKey = "demokey"; connectSettings.gameKey = "hybriona.quadtreetest"; if (!useLive) { connectSettings.serverSecurityKey = "demokey"; } connector = new LNSConnector(clientParameters, connectSettings, this); connector.onConnected = () => { LNSCreateRoomParameters roomParameters = new LNSCreateRoomParameters(); roomParameters.maxPlayers = 100; roomParameters.isQuadTreeAllowed = true; //roomParameters.idleLife = 60 * 24; roomParameters.EnableQuadTreeCellOptimization(Vector2.zero, new Vector2(2000, 2000)); connector.CreateRoom("default", roomParameters); }; connector.onFailedToConnect = (CONNECTION_FAILURE_CODE code) => { Debug.LogError(name + " - " + code); }; connector.onDisconnectedFromRoom = () => { Debug.LogError(name + " - onDisconnectedFromRoom"); }; connector.onDisconnected = () => { Debug.LogError(name + " - onDisconnectedFromServer"); }; connector.onRoomCreateFailed = (ROOM_FAILURE_CODE code) => { Debug.LogError(name + " - " + code); if (code == ROOM_FAILURE_CODE.ROOM_ALREADY_EXIST) { connector.JoinRoom("default"); } }; connector.onRoomJoined = () => { Debug.Log(name + " on room joined"); }; connector.onRoomJoinFailed = (ROOM_FAILURE_CODE code) => { Debug.LogError(name + " - " + code); }; connector.Connect(); }