public void Connect(ConnectionId connectionId, ApprovalSecret approvalSecret, IPEndPoint endpoint) { var approvalSecretMessage = _netPeer.CreateMessage(); approvalSecretMessage.Write(approvalSecret.Value); var connection = _netPeer.Connect(endpoint, approvalSecretMessage); AddConnection(connectionId, connection); }
private void ApproveConnection(ApprovalSecret secret) { NetConnection connection; if (_awaitingApprovals.TryGetValue(secret, out connection)) { Console.WriteLine("approving connection " + connection.RemoteEndPoint); connection.Approve(); } _awaitingApprovals.Remove(secret); }
private void PassRequestApproval( ConnectionId connectionId, ApprovalSecret secret, Action <ApprovalSecret> approve, Action <ApprovalSecret> deny) { if (RequestApproval != null) { RequestApproval(connectionId, secret, approve, deny); } else { approve(secret); } }
public ConnectionId Connect(IPEndPoint hostEndpoint, ApprovalSecret approvalSecret, OnConnectionEstablished onConnectionEstablished = null, OnConnectionFailure onConnectionFailure = null, OnDisconnected onDisconnected = null) { var connectionId = _connectionIdPool.Take(); _transporter.Connect(connectionId, approvalSecret, hostEndpoint); _connections[connectionId] = new ConnectionRegistration { Endpoint = hostEndpoint, OnConnectionEstablished = onConnectionEstablished ?? EmptyOnConnectionEstablished, OnConnectionFailure = onConnectionFailure ?? EmptyOnConnectionFailure, OnDisconnected = onDisconnected ?? EmptyOnDisconnected, Status = ConnectionStatus.Pending, }; return(connectionId); }
private void DenyConnection(ApprovalSecret secret) { NetConnection connection; if (_awaitingApprovals.TryGetValue(secret, out connection)) { ConnectionId connectionId; if (_connections.TryGetValue(connection, out connectionId)) { RemoveConnection(connection); if (OnConnectionClosed != null) { OnConnectionClosed(connectionId); } } Console.WriteLine("denying connection " + connection.RemoteEndPoint); connection.Deny(); } _awaitingApprovals.Remove(secret); }
public ConnectionId Connect(IPEndPoint hostEndpoint, ApprovalSecret approvalSecret, OnConnectionEstablished onConnectionEstablished = null, OnConnectionFailure onConnectionFailure = null, OnDisconnected onDisconnected = null) { var connectionId = _connectionIdPool.Take(); var connectionRegistration = _connectionRegistrationPool.Take(); connectionRegistration.Instance.Timestamp = Time.realtimeSinceStartup; connectionRegistration.Instance.ConnectionId = connectionId; connectionRegistration.Instance.ApprovalSecret = approvalSecret; connectionRegistration.Instance.PublicEndpoint = hostEndpoint; connectionRegistration.Instance.OnConnectionEstablished = onConnectionEstablished ?? EmptyOnConnectionEstablished; connectionRegistration.Instance.OnConnectionFailure = onConnectionFailure ?? EmptyOnConnectionFailure; connectionRegistration.Instance.OnDisconnected = onDisconnected ?? EmptyOnDisconnected; var punchId = _natPunchClient.Punch(hostEndpoint, OnPunchSuccess, OnPunchFailure); _punchAttempts.Add(punchId, connectionRegistration); return(connectionId); }
void Receive() { if (_lastSampledFrame >= Time.renderedFrameCount || _status == TransporterStatus.Closed) { return; } NetIncomingMessage msg; while ((msg = _netPeer.ReadMessage()) != null) { //Debug.Log("incoming message of type " + msg.MessageType + " from " + msg.SenderEndPoint); ConnectionId connectionId; switch (msg.MessageType) { case NetIncomingMessageType.Error: break; case NetIncomingMessageType.ConnectionApproval: var approvalSecret = new ApprovalSecret(msg.ReadString()); if (RequestApproval != null) { _awaitingApprovals.Add(approvalSecret, msg.SenderConnection); connectionId = _connectionIdPool.Take(); AddConnection(connectionId, msg.SenderConnection); RequestApproval(connectionId, approvalSecret, _approveConnection, _denyConnection); } else { msg.SenderConnection.Approve(); } break; case NetIncomingMessageType.StatusChanged: var connectionStatus = (NetConnectionStatus)msg.ReadByte(); //Debug.Log("connection status " + connectionStatus); switch (connectionStatus) { case NetConnectionStatus.None: case NetConnectionStatus.ReceivedInitiation: case NetConnectionStatus.RespondedAwaitingApproval: case NetConnectionStatus.RespondedConnect: case NetConnectionStatus.InitiatedConnect: case NetConnectionStatus.Disconnecting: break; case NetConnectionStatus.Connected: if (!_connections.TryGetValue(msg.SenderConnection, out connectionId)) { connectionId = _connectionIdPool.Take(); AddConnection(connectionId, msg.SenderConnection); } Debug.Log("Connection opened to " + msg.SenderConnection.RemoteEndPoint + " with connection id " + connectionId); if (OnConnectionOpened != null) { OnConnectionOpened(connectionId, msg.SenderEndPoint); } break; case NetConnectionStatus.Disconnected: if (_connections.TryGetValue(msg.SenderConnection, out connectionId)) { Debug.Log("Disconnected: " + connectionId); RemoveConnection(msg.SenderConnection); if (OnConnectionClosed != null) { OnConnectionClosed(connectionId); } } break; default: Debug.LogError("Unhandled connection status: " + connectionStatus); break; } break; case NetIncomingMessageType.UnconnectedData: Console.WriteLine("Receiving unconnected data from " + msg.SenderEndPoint + ": " + msg.ToHexString()); if (OnUnconnectedDataReceived != null) { OnUnconnectedDataReceived(msg.SenderEndPoint, msg); } break; // case NetIncomingMessageType.ConnectionApproval: // Debug.Log("Approving connection to " + msg.SenderEndPoint); // msg.SenderConnection.Approve(); // break; case NetIncomingMessageType.Data: if (OnDataReceived != null) { connectionId = (ConnectionId)msg.SenderConnection.Tag; OnDataReceived(connectionId, msg.SenderEndPoint, msg); } break; case NetIncomingMessageType.NatIntroductionSuccess: var token = msg.ReadString(); if (OnNatPunchSuccess != null) { OnNatPunchSuccess(token, msg.SenderEndPoint); } break; case NetIncomingMessageType.ConnectionLatencyUpdated: connectionId = (ConnectionId)msg.SenderConnection.Tag; var roundtripTime = msg.ReadFloat(); var latency = roundtripTime * 0.5f; _latencyInfo.UpdateLatency(connectionId, latency); break; case NetIncomingMessageType.VerboseDebugMessage: Console.WriteLine("LIDGREN (trace): " + msg.ReadString()); break; case NetIncomingMessageType.DebugMessage: Console.WriteLine("LIDGREN (info): " + msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: Console.WriteLine("LIDGREN (warning): " + msg.ReadString()); break; case NetIncomingMessageType.ErrorMessage: Console.WriteLine("LIDGREN (error): " + msg.ReadString()); break; default: Console.WriteLine("LIDGREN (warning): Unhandled message type: " + msg.MessageType); break; } _netPeer.Recycle(msg); } _lastSampledFrame = Time.renderedFrameCount; }