示例#1
0
        public bool ReceiveToken(byte[] data, int dataSize, out IMessageRider riderToken, out NetworkMsg type)
        {
            byte tokType;
            int  size;

            riderToken = socketInterface.ReadToken(dataSize, data, out tokType, out size);

            //Null token received
            if (size == 0)
            {
                //NetLog.Info("<color=green> GOT A NULL TOKEN </color>");
            }

            type = (NetworkMsg)tokType;

            if (type != NetworkMsg.Unknown)
            {
                //Handle null tokens
                if (size == 0)
                {
                    return(true);
                }
                if (riderToken != null)
                {
                    SocketLog.Info("{0} token successfully received from host", type);
                    return(true);
                }
            }
            SocketLog.Error("Failed to unpack {0} token from host", type);
            return(false);
        }
 public bool AddConnection(SocketConnection conn)
 {
     if (!Connections.Contains(conn))
     {
         Connections.Add(conn);
         SocketLog.Info("Added connection {0}:{1} as ID: {2}", conn.ConnectionInfo.Address, conn.ConnectionInfo.Port, conn.ConnectionInfo.Peer.ConnectId);
         return(true);
     }
     SocketLog.Error("Failed to add connection {0} : {1} | ID: {2}", conn.ConnectionInfo.Address, conn.ConnectionInfo.Port, conn.ConnectionInfo.Peer.ConnectId);
     return(false);
 }
 /// <summary>
 /// Removes a connection from the current socket interface
 /// </summary>
 /// <param name="connection">The connection to remove</param>
 /// <returns>If successful true (exists in our list of connections), if not false</returns>
 public bool RemovePendingConnection(SocketConnection connection)
 {
     if (PendingConnections.Contains(connection))
     {
         SocketLog.Info("Removed Pending connection {0}:{1}| ID: {2}", connection.ConnectionInfo.Address, connection.ConnectionInfo.Port, connection.ConnectionInfo.Peer.ConnectId);
         PendingConnections.Remove(connection);
         return(true);
     }
     SocketLog.Error("Failed to remove Pending connection {0} | It does not exist.", connection.ConnectionInfo.Peer.EndPoint);
     return(false);
 }
 public void RefuseConnectionWithBytes(SocketConnection conn, byte[] tokenBytes)
 {
     if (AscensionNetwork.IsClient)
     {
         SocketLog.Error("Client cannot refuse!");
     }
     //Send Token
     conn.SendToken(tokenBytes, tokenBytes.Length, NetworkMsg.Refuse);
     //Disconnect
     server.DisconnectPeer(conn.ConnectionInfo.Peer);
 }
 public bool UpdateConnection(NetPeer peer, SocketConnectionInfo connInfo, out SocketConnection updatedConnection)
 {
     updatedConnection = GetConnection(peer);
     if (updatedConnection != null)
     {
         updatedConnection.UpdateConnectionInfo(connInfo);
         SocketLog.Info("Sucessfully updated connection {0} with new connection information.", peer.EndPoint);
         return(true);
     }
     SocketLog.Error("Failed to update connection {0} with new connection information. Connection not found in Socket Interface!", peer.EndPoint);
     return(false);
 }
 public void Disconnect(NetPeer peer)
 {
     if (AscensionNetwork.IsClient && !connected)
     {
         SocketLog.Error("Cannot disconnect if we are not connected!");
         return;
     }
     //Disconnect
     if (server != null)
     {
         server.DisconnectPeer(peer);
     }
 }
 /// <summary>
 /// Adds a connection to the list of connections on this socket interface
 /// </summary>
 /// <param name="newSocketConnection">If successful, the newly created connection</param>
 /// <returns>If connection is added successfully, true, if not false - also newSocketConnection will be null</returns>
 public bool AddConnection(SocketConnectionInfo connInfo, out SocketConnection newSocketConnection)
 {
     newSocketConnection = new SocketConnection(this, connInfo);
     if (!Connections.Contains(newSocketConnection))
     {
         Connections.Add(newSocketConnection);
         SocketLog.Info("Added connection {0}:{1}", newSocketConnection.ConnectionInfo.Address, newSocketConnection.ConnectionInfo.Port);
         return(true);
     }
     SocketLog.Error("Failed to add connection {0}:{1} from {2}", connInfo.Peer.EndPoint.Host, connInfo.Peer.EndPoint.Port, connInfo.Peer.ConnectId);
     newSocketConnection = null;
     return(false);
 }
 public bool AddPendingConnection(SocketConnectionInfo connInfo, out SocketConnection potentialSocketConnection)
 {
     potentialSocketConnection = new SocketConnection(this, connInfo);
     if (!PendingConnections.Contains(potentialSocketConnection))
     {
         PendingConnections.Add(potentialSocketConnection);
         SocketLog.Info("Connection {0}:{1} currently pending approval | ID: {2}", potentialSocketConnection.ConnectionInfo.Address, potentialSocketConnection.ConnectionInfo.Port, potentialSocketConnection.ConnectionInfo.Peer.ConnectId);
         return(true);
     }
     SocketLog.Error("Failed to place connection {0}:{1} | ID: {2} in pending queue.", potentialSocketConnection.ConnectionInfo.Address, potentialSocketConnection.ConnectionInfo.Port, potentialSocketConnection.ConnectionInfo.Peer.ConnectId);
     potentialSocketConnection = null;
     return(false);
 }
 public bool SetDisconnected()
 {
     if (IsClient)
     {
         if (!connected)
         {
             SocketLog.Error("Failed to set disconnected, we are not connected!");
             return(false);
         }
         connected = false;
         return(true);
     }
     SocketLog.Error("Failed to set disconnected, we are not a client!");
     return(false);
 }
示例#10
0
 public bool RefuseConnection(SocketConnection conn, IMessageRider token)
 {
     if (AscensionNetwork.IsClient)
     {
         SocketLog.Error("Client cannot refuse!");
         return(false);
     }
     //COnstruct byte array of the token
     byte[] tokenBytes = WriteToken(token, NetworkMsg.Refuse);
     //Send Token
     conn.SendToken(tokenBytes, tokenBytes.Length, NetworkMsg.Refuse);
     //Disconnect
     server.DisconnectPeer(conn.ConnectionInfo.Peer);
     return(true);
 }
示例#11
0
        /// <summary>
        /// If the proper signature is received, sets this client to the ready state for being able to send and receive messages to and from the host
        /// </summary>
        /// <param name="data">Byte array of the data contained in the packet</param>
        /// <param name="dataSize">Packet size</param>
        /// <returns>If communication lines are open true, if not false</returns>
        public bool GetConnectionReady(byte[] data, int dataSize)
        {
            Packet p      = new Packet(data, dataSize);
            byte   nMsg   = p.ReadByte();
            byte   cState = p.ReadByte();

            if (nMsg == (byte)NetworkMsg.Ready)
            {
                communicationReady = true;
                state = (ConnectionState)cState;
                SocketLog.Info("Communication socket ready and opened by hosting server");
                return(true);
            }
            SocketLog.Error("Communication socket failed to be opened by hosting server - invalid Network Message {0} | Mode: {1}", nMsg, mode);
            return(false);
        }
示例#12
0
 public bool SetConnected()
 {
     if (IsClient)
     {
         if (!connecting)
         {
             SocketLog.Error("Failed to set connected, we are not connecting!");
             return(false);
         }
         connecting = false;
         connected  = true;
         return(true);
     }
     SocketLog.Error("Failed to set connected, we are not a client!");
     return(false);
 }
示例#13
0
        public bool Accept(IPEndPoint endPoint)
        {
            SocketConnection conn = GetPendingConnection(endPoint);

            if (conn != null)
            {
                //Remove from pending
                RemovePendingConnection(conn);
                //Add to current
                AddConnection(conn);
                SocketLog.Info("Accepted connection from {0}:{1}", endPoint.Address, endPoint.Port);
                return(true);
            }
            SocketLog.Error("Failed to accept connection {0}:{1}", endPoint.Address, endPoint.Port);
            return(false);
        }
示例#14
0
 public void Disconnect(SocketConnection conn, IMessageRider token)
 {
     if (AscensionNetwork.IsClient && !connected)
     {
         SocketLog.Error("Cannot disconnect if we are not connected!");
         return;
     }
     //Construct byte array of the token
     byte[] tokenBytes = WriteToken(token, NetworkMsg.Disconnect);
     //Send Token
     conn.SendToken(tokenBytes, tokenBytes.Length, NetworkMsg.Disconnect);
     //Disconnect
     if (server != null)
     {
         server.DisconnectPeer(conn.ConnectionInfo.Peer);
     }
 }
示例#15
0
        public bool Refuse(IPEndPoint endPoint, IMessageRider token)
        {
            bool refused = false;

            SocketConnection conn = GetPendingConnection(endPoint);

            if (conn != null)
            {
                refused = RefuseConnection(conn, token);
            }
            else
            {
                SocketLog.Error("Refuse failed! No connection with the endpoint: {0} found.", endPoint);
                return(false);
            }
            return(refused);
        }
示例#16
0
        public bool Refuse(int connId)
        {
            bool refused = false;

            SocketConnection conn = GetPendingConnection(connId);

            if (conn != null)
            {
                refused = RefuseConnection(conn, null);
            }
            else
            {
                SocketLog.Error("Refuse failed! No connection with the ID: {0} found.", connId);
                return(false);
            }
            return(refused);
        }
示例#17
0
        public void Disconnect(byte[] tokenBytes)
        {
            if (AscensionNetwork.IsClient && !connected)
            {
                SocketLog.Error("Cannot disconnect if we are not connected!");
                return;
            }
            //Send Disconnect token
            SendDisconnectToken(tokenBytes);
            //Disconnect
            if (server != null)
            {
                server.DisconnectPeer(socketPeer);
            }

            //TODO: Do something with the token here
            //tokenBytes
        }
示例#18
0
        public bool Accept(IPEndPoint endPoint, IMessageRider acceptToken, IMessageRider connectToken)
        {
            SocketConnection conn = GetPendingConnection(endPoint);

            if (conn != null)
            {
                //Remove from pending
                RemovePendingConnection(conn);
                //Add to current
                AddConnection(conn);
                //Send Tokens
                SendToken(conn.ConnectionInfo.Peer, acceptToken, NetworkMsg.Accept);
                SendToken(conn.ConnectionInfo.Peer, connectToken, NetworkMsg.Connect);
                SocketLog.Info("Accepted connection from {0}:{1}", endPoint.Address, endPoint.Port);
                return(true);
            }
            SocketLog.Error("Failed to accept connection {0}:{1}", endPoint.Address, endPoint.Port);
            return(false);
        }
示例#19
0
        public bool Connect(SocketEndPoint endPoint, IMessageRider token)
        {
            //Checks before preceeding
            if (IsHost)
            {
                SocketLog.Error("Cannot connect when we are a host!");
                return(false);
            }
            if (connected)
            {
                SocketLog.Error("Cannot connect when we are already connected!");
                return(false);
            }
            if (connecting)
            {
                SocketLog.Error("Cannot connect when we are already connecting!");
                return(false);
            }

            client.Connect(endPoint.address, endPoint.port);

            //Set Address and Port
            address = endPoint.address;
            port    = endPoint.port;
            //Set the values of this connection
            SetConnectionValues(endPoint.address, endPoint.port);
            //Create a new instance of connection info
            SocketConnectionInfo info = new SocketConnectionInfo(address, port);
            //Initialize a new connection
            SocketConnection conn;

            //Add a connection
            AddConnection(info, out conn);
            //Cache our connect token to be sent once the communication lines have been established
            conn.ConnectToken = WriteToken(token, NetworkMsg.Connect);
            //Log our succeeded attempt
            SocketLog.Info("Connection Attempt to: {0} : {1}", endPoint.address, endPoint.port);
            //Set connecting
            connecting = true;

            return(true);
        }
示例#20
0
        /// <summary>
        /// Receives a state change from the host we are connected to
        /// </summary>
        /// <param name="connId">Connection this change is concerned with</param>
        /// <param name="data">The packet data</param>
        /// <param name="size">The packet size</param>
        /// <returns>True if proper signature is received and ready state is changed, false otherwise</returns>
        public bool ReceiveConnectionReady(NetPeer peer, byte[] data, int size)
        {
            SocketConnection conn = GetConnection(peer);

            if (conn != null)
            {
                //Do not attempt to set the ready state again if we are already in the ready state
                if (conn.Ready)
                {
                    return(false);
                }
                //Attempt to set the connection to a ready state
                if (conn.GetConnectionReady(data, size))
                {
                    SocketLog.Info("Received ready state for connection {0}", peer.ConnectId);
                    return(true);
                }
            }
            SocketLog.Error("Received ready state failure for connection {0}", peer.ConnectId);
            return(false);
        }
示例#21
0
        /// <summary>
        /// Sets the current connection to the ready state and attempts to send this state change across the wire
        /// </summary>
        /// <param name="connId">Connection to ready up</param>
        /// <returns>If the state change send was successful. If it is already ready true also</returns>
        public bool SetConnectionReady(NetPeer peer)
        {
            SocketConnection conn = GetPendingConnection(peer);
            bool             sent;

            if (conn != null)
            {
                if (conn.Ready)
                {
                    //Skip this if we are already set to ready
                    SocketLog.Error("Socket connection {0} was already in the ready state! [Pending Connection]", peer.ConnectId);
                    return(false);
                }
                conn.SetConnectionReady();

                SocketLog.Info("Set pending connection {0} to the ready state", peer.ConnectId);
                return(true);
            }

            conn = GetConnection(peer);

            if (conn != null)
            {
                if (conn.Ready)
                {
                    //Skip this if we are already set to ready
                    SocketLog.Error("Socket connection {0} was already in the ready state! [Connection]", peer.ConnectId);
                    return(false);
                }
                conn.SetConnectionReady();

                SocketLog.Info("Set connection {0} to the ready state", peer.ConnectId);
                return(true);
            }
            SocketLog.Error("Failed to set connection {0} to the Ready State", peer.ConnectId);
            return(false);
        }