private async void Connect(string host) { cancelToken = new CancellationTokenSource(); try { hostSteamID = new CSteamID(Convert.ToUInt64(host)); connectedComplete = new TaskCompletionSource <Task>(); OnConnected += SetConnectedComplete; CloseP2PSessionWithUser(hostSteamID); SendInternal(hostSteamID, InternalMessages.CONNECT); Task connectedCompleteTask = connectedComplete.Task; if (await Task.WhenAny(connectedCompleteTask, Task.Delay(ConnectionTimeout, cancelToken.Token)) != connectedCompleteTask) { OnConnected -= SetConnectedComplete; throw new Exception("Timed out while connecting"); } OnConnected -= SetConnectedComplete; } catch (FormatException) { OnReceivedError?.Invoke(new Exception("ERROR passing steam ID address")); } catch (Exception ex) { OnReceivedError?.Invoke(ex); } }
protected override void OnReceiveInternalData(InternalMessages type, ProductUserId clientUserId) { switch (type) { case InternalMessages.CONNECT: if (epicToMirrorIds.Count >= maxConnections) { SendInternal(clientUserId, InternalMessages.DISCONNECT); return; } SendInternal(clientUserId, InternalMessages.ACCEPT_CONNECT); int connectionId = nextConnectionID++; epicToMirrorIds.Add(clientUserId, connectionId); OnConnected.Invoke(connectionId); Debug.Log($"Client with Product User ID {clientUserId} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: if (epicToMirrorIds.TryGetValue(clientUserId, out int connId)) { OnDisconnected.Invoke(connId); CloseP2PSessionWithUser(clientUserId); epicToMirrorIds.Remove(clientUserId); Debug.Log($"Client with Product User ID {clientUserId} disconnected."); } else { OnReceivedError.Invoke(-1, new Exception("ERROR Unknown Product User ID")); } break; default: Debug.Log("Received unknown message type"); break; } }
public string ServerGetClientAddress(int connectionId) { if (epicToMirrorIds.TryGetValue(connectionId, out ProductUserId userId)) { return userId.ToString(); } else { Debug.LogError("Trying to get info on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); return string.Empty; } }
public void SendAll(int connectionId, byte[] data, int channelId) { if (epicToMirrorIds.TryGetValue(connectionId, out ProductUserId userId)) { Send(userId, data, (byte)channelId); } else { Debug.LogError("Trying to send on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); } }
public void Send(int connectionId, byte[] data, int channelId) { if (steamToMirrorIds.TryGetValue(connectionId, out CSteamID steamId)) { Send(steamId, data, channelId); } else { Debug.LogError("Trying to send on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); } }
protected override void OnReceiveInternalData(InternalMessages type, ProductUserId clientUserId, SocketId socketId) { if (ignoreAllMessages) { return; } switch (type) { case InternalMessages.CONNECT: if (epicToMirrorIds.Count >= maxConnections) { Debug.LogError("Reached max connections"); //CloseP2PSessionWithUser(clientUserId, socketId); SendInternal(clientUserId, socketId, InternalMessages.DISCONNECT); return; } SendInternal(clientUserId, socketId, InternalMessages.ACCEPT_CONNECT); int connectionId = nextConnectionID++; epicToMirrorIds.Add(clientUserId, connectionId); epicToSocketIds.Add(clientUserId, socketId); Debug.LogError("Adding new connection with ID: " + connectionId); OnConnected.Invoke(connectionId); string clientUserIdString; clientUserId.ToString(out clientUserIdString); Debug.Log($"Client with Product User ID {clientUserIdString} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: if (epicToMirrorIds.TryGetValue(clientUserId, out int connId)) { OnDisconnected.Invoke(connId); //CloseP2PSessionWithUser(clientUserId, socketId); epicToMirrorIds.Remove(clientUserId); epicToSocketIds.Remove(clientUserId); Debug.LogError($"Client with Product User ID {clientUserId} disconnected."); } else { OnReceivedError.Invoke(-1, new Exception("ERROR Unknown Product User ID")); } break; default: Debug.Log("Received unknown message type"); break; } }
protected override void OnReceiveData(byte[] data, ProductUserId clientUserId, int channel) { if (epicToMirrorIds.TryGetValue(clientUserId, out int connectionId)) { OnReceivedData.Invoke(connectionId, data, channel); } else { CloseP2PSessionWithUser(clientUserId); string productId; clientUserId.ToString(out productId); Debug.LogError("Data received from epic client thats not known " + productId); OnReceivedError.Invoke(-1, new Exception("ERROR Unknown product ID")); } }
protected override void OnReceiveData(byte[] data, CSteamID clientSteamID, int channel) { if (steamToMirrorIds.TryGetValue(clientSteamID, out int connectionId)) { OnReceivedData.Invoke(connectionId, data, channel); } else { CloseP2PSessionWithUser(clientSteamID); Debug.LogError("Data received from steam client thats not known " + clientSteamID); OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID")); } }
public string ServerGetClientAddress(int connectionId) { if (steamIDToMirrorID.TryGetValue(connectionId, out CSteamID steamId)) { return(steamId.ToString()); } else { Debug.LogError("Trying to get info on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); return(string.Empty); } }
public string ServerGetClientAddress(int connectionId) { if (steamToMirrorIds.Contains(connectionId)) { return(steamToMirrorIds[connectionId].ToString()); } else { Debug.LogError("Trying to get info on unknown connection: " + connectionId); OnReceivedError?.Invoke(connectionId, new Exception("ERROR Unknown Connection")); return(string.Empty); } }
protected override void OnReceiveData(byte[] data, SteamId clientSteamID, int channel) { if (steamToMirrorIds.Contains(clientSteamID)) { int connectionId = steamToMirrorIds[clientSteamID]; OnReceivedData?.Invoke(connectionId, data, channel); } else { CloseP2PSessionWithUser(clientSteamID); Debug.LogError("Data received from steam client thats not known " + clientSteamID); OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID")); } }
public bool Send(List <int> connectionIds, byte[] data, int channelId = 0) { for (int i = 0; i < connectionIds.Count; i++) { try { SteamClient steamClient = steamConnectionMap.fromConnectionID[connectionIds[i]]; //will default to reliable at channel 0 if sent on an unknown channel Send(steamClient.steamID, data, channelToSendType(channelId), channelId >= channels.Length ? 0 : channelId); } catch (KeyNotFoundException) { //we have no idea who this connection is Debug.LogError("Tryign to Send on a connection thats not known " + connectionIds[i]); OnReceivedError?.Invoke(connectionIds[i], new Exception("ERROR Unknown Connection")); } } return(true); }
public string ServerGetClientAddress(int connectionId) { try { SteamClient steamClient = steamConnectionMap.fromConnectionID[connectionId]; return(steamClient.steamID.ToString()); } catch (KeyNotFoundException) { //we have no idea who this connection is Debug.LogError("Trying to get info on an unknown connection " + connectionId); OnReceivedError?.Invoke(connectionId, new Exception("ERROR Unknown Connection")); } return(null); }
public void Send(int connectionId, byte[] data, int channelId) { if (connToMirrorID.TryGetValue(connectionId, out HSteamNetConnection conn)) { EResult res = SendSocket(conn, data, channelId); if (res != EResult.k_EResultOK) { Debug.LogError($"Could not send: {res.ToString()}"); } } else { Debug.LogError("Trying to send on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); } }
public bool SendAll(List <int> connectionIds, byte[] data, int channelId) { bool success = true; foreach (int connId in connectionIds) { if (steamToMirrorIds.TryGetValue(connId, out CSteamID steamId)) { success = success && Send(steamId, data, channelId); } else { Debug.LogError("Trying to send on unknown connection: " + connId); OnReceivedError.Invoke(connId, new Exception("ERROR Unknown Connection")); } } return(success); }
private async Task ReceiveLoop() { Debug.Log("ReceiveLoop Start"); uint readPacketSize; SteamId clientSteamID; try { byte[] receiveBuffer; while (!Offline) { for (int i = 0; i < channels.Length; i++) { while (Receive(out readPacketSize, out clientSteamID, out receiveBuffer, i)) { if (readPacketSize == 0) { continue; } try { int connectionId = steamConnectionMap.fromSteamID[clientSteamID].connectionID; // we received some data, raise event OnReceivedData?.Invoke(connectionId, receiveBuffer, i); } catch (KeyNotFoundException) { CloseP2PSessionWithUser(clientSteamID); //we have no idea who this connection is Debug.LogError("Data received from steam client thats not known " + clientSteamID); OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID")); } } } //not got a message - wait a bit more await Task.Delay(TimeSpan.FromSeconds(secondsBetweenPolls)); } } catch (ObjectDisposedException) { } Debug.Log("ReceiveLoop Stop"); }
public void Send(int connectionId, byte[] data, int channelId) { if (connToMirrorID.TryGetValue(connectionId, out Connection conn)) { Result res = SendSocket(conn, data, channelId); if (res == Result.NoConnection || res == Result.InvalidParam) { Debug.Log($"Connection to {connectionId} was lost."); InternalDisconnect(connectionId, conn); } else if (res != Result.OK) { Debug.LogError($"Could not send: {res.ToString()}"); } } else { Debug.LogError("Trying to send on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); } }
protected override void OnReceiveInternalData(InternalMessages type, CSteamID clientSteamID) { switch (type) { case InternalMessages.CONNECT: if (steamToMirrorIds.Count >= maxConnections) { SendInternal(clientSteamID, InternalMessages.DISCONNECT); return; } SendInternal(clientSteamID, InternalMessages.ACCEPT_CONNECT); int connectionId = nextConnectionID++; steamToMirrorIds.Add(clientSteamID, connectionId); OnConnected.Invoke(connectionId); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: if (steamToMirrorIds.Contains(clientSteamID)) { OnDisconnected.Invoke(steamToMirrorIds[clientSteamID]); steamToMirrorIds.Remove(clientSteamID); CloseP2PSessionWithUser(clientSteamID); Debug.Log($"Client with SteamID {clientSteamID} disconnected."); } else { OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID")); } break; default: Debug.Log("Received unknown message type"); break; } }
public async void Connect(string host) { // not if already started if (!Disconnected) { // exceptions are better than silence Debug.LogError("Client already connected or connecting"); OnReceivedError?.Invoke(new Exception("Client already connected")); return; } // We are connecting from now until Connect succeeds or fails Connecting = true; initialise(); try { hostSteamID = new CSteamID(Convert.ToUInt64(host)); InternalReceiveLoop(); connectedComplete = new TaskCompletionSource <Task>(); OnConnected += setConnectedComplete; //Send a connect message to the steam client - this requests a connection with them SendInternal(hostSteamID, connectMsgBuffer); Task connectedCompleteTask = connectedComplete.Task; if (await Task.WhenAny(connectedCompleteTask, Task.Delay(clientConnectTimeoutMS)) != connectedCompleteTask) { //Timed out waiting for connection to complete OnConnected -= setConnectedComplete; Exception e = new Exception("Timed out while connecting"); OnReceivedError?.Invoke(e); throw e; } OnConnected -= setConnectedComplete; await ReceiveLoop(); } catch (FormatException) { Debug.LogError("Failed to connect ERROR passing steam ID address"); OnReceivedError?.Invoke(new Exception("ERROR passing steam ID address")); return; } catch (Exception ex) { Debug.LogError("Failed to connect " + ex); OnReceivedError?.Invoke(ex); } finally { Disconnect(); } }
//start a async loop checking for internal messages and processing them. This includes internal connect negotiation and disconnect requests so runs outside "connected" private async void InternalReceiveLoop() { Debug.Log("InternalReceiveLoop Start"); uint readPacketSize; SteamId clientSteamID; try { while (!Offline) { while (ReceiveInternal(out readPacketSize, out clientSteamID)) { Debug.Log("InternalReceiveLoop - data"); if (readPacketSize != 1) { continue; } Debug.Log("InternalReceiveLoop - received " + receiveBufferInternal[0]); switch (receiveBufferInternal[0]) { //requesting to connect to us case (byte)InternalMessages.CONNECT: if (steamConnectionMap.Count >= maxConnections) { SendInternal(clientSteamID, disconnectMsgBuffer); continue; //too many connections, reject } SendInternal(clientSteamID, acceptConnectMsgBuffer); int connectionId = nextConnectionID++; steamConnectionMap.Add(clientSteamID, connectionId, SteamClient.ConnectionState.CONNECTED); OnConnected?.Invoke(connectionId); break; //asking us to disconnect case (byte)InternalMessages.DISCONNECT: try { SteamClient steamClient = steamConnectionMap.fromSteamID[clientSteamID]; steamConnectionMap.Remove(steamClient); OnDisconnected?.Invoke(steamClient.connectionID); CloseP2PSessionWithUser(steamClient.steamID); } catch (KeyNotFoundException) { //we have no idea who this connection is Debug.LogError("Trying to disconnect a client thats not known SteamID " + clientSteamID); OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID")); } break; } } //not got a message - wait a bit more await Task.Delay(TimeSpan.FromSeconds(secondsBetweenPolls)); } } catch (ObjectDisposedException) { } Debug.Log("InternalReceiveLoop Stop"); }