Esempio n. 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);
        }
Esempio n. 2
0
 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);
 }
Esempio n. 3
0
 /// <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);
 }
Esempio n. 4
0
 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);
 }
Esempio n. 5
0
 /// <summary>
 /// Attempts to unbind the socket and then shutdown the transport layer. If we fail to unbind we will still shutdown.
 /// </summary>
 /// <returns>True is unbinding is successful, false if not</returns>
 public void Shutdown()
 {
     if (server != null)
     {
         server.Stop();
     }
     if (client != null)
     {
         client.Stop();
     }
     SocketLog.Info("Unbinding socket {0} : {1}", socketEndPoint.Address, socketEndPoint.Port);
 }
Esempio n. 6
0
 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);
 }
Esempio n. 7
0
 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);
     }
 }
Esempio n. 8
0
 /// <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);
 }
Esempio n. 9
0
 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);
 }
Esempio n. 10
0
        /// <summary>
        /// Set the ready state for communication to true, send a packet to the connection
        /// </summary>
        /// <param name="sent">If the sending of the packet succeeds or fails</param>
        public void SetConnectionReady()
        {
            communicationReady = true;

            Packet p = new Packet();

            p.WriteByte((byte)NetworkMsg.Ready);
            p.WriteByte((byte)ConnectionState.Connecting);
            p.FinishWriting();

            SockInterface.Send(p, connectionInfo.Peer, SendOptions.ReliableOrdered);

            SocketLog.Info("Communication socket ready and open sent to {0}", connectionInfo.Peer.ConnectId);
        }
Esempio n. 11
0
        public void SetToken(int dataSize, byte[] data)
        {
            //Initialization
            NetworkMsg msgType = NetworkMsg.Unknown;
            byte       type;
            int        size;
            //Read the packet and convert it to a byte array
            IMessageRider token = SockInterface.ReadToken(dataSize, data, out type, out size);

            //Convert the type into a enum readable type
            msgType = (NetworkMsg)type;
            //Handle the token
            HandleToken(msgType, token);
            SocketLog.Info("Set token type {0} for connection {1}", msgType, ConnectionInfo.Peer.ConnectId);
        }
Esempio n. 12
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);
 }
Esempio n. 13
0
 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);
 }
Esempio n. 14
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);
 }
Esempio n. 15
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);
        }
Esempio n. 16
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);
        }
Esempio n. 17
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);
        }
Esempio n. 18
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);
     }
 }
Esempio n. 19
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);
        }
Esempio n. 20
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
        }
Esempio n. 21
0
        public bool SendToken(NetPeer peer, byte[] token)
        {
            //If token is empty, don't send
            if (token == null)
            {
                return(false);
            }
            //If we don't have a proper connection, don't send
            if ((server == null && client == null) || socketPeer == null)
            {
                return(false);
            }

            //Send token
            peer.Send(token, SendOptions.ReliableOrdered);
            SocketLog.Info("Sending token to {0} on socket {1}, channel {2} | Token Length: {3}", peer.ConnectId, socketPeer.EndPoint, socketPeer.ConnectId, SendOptions.ReliableOrdered, token.Length);
            return(true);
        }
Esempio n. 22
0
        public bool Initialize(IPEndPoint endPoint, ManualResetEvent finishedEvent)
        {
            host = Core.IsServer;

            Core.SetNetEventListener(socketEventListener);

            if (Core.IsServer)
            {
                server = new NetManager(socketEventListener, RuntimeSettings.Instance.maxDefaultConnections, key);
                server.Start(endPoint.Port);
            }
            else
            {
                client = new NetManager(socketEventListener, key);
                client.Start();
            }

            //Reset the transport layer if it has already been started
            if (server != null || client != null)
            {
                //Set address and port
                address = endPoint.Address.ToString();
                port    = endPoint.Port;
                //If successful set out endpoint
                socketEndPoint = endPoint;
                //Set up packet pool
                packetPool = new BasePacketPool(this);
                //Socket log
                SocketLog.Info("Socket bound to {0}", endPoint);
                //Set network update time
                SetUpdateTime(Core.FramesPerSecond);
                //After we have initialized the Socket, start done
                Core.StartDone();
                //...finally free our thread to begin again
                if (finishedEvent != null)
                {
                    finishedEvent.Set();
                }
                return(true);
            }
            //Socket initialization failed
            Core.StartFailed();
            return(false);
        }
Esempio n. 23
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);
        }
Esempio n. 24
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);
        }
Esempio n. 25
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);
        }
Esempio n. 26
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);
        }