Beispiel #1
0
        private void onConnectRequestReceived(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data)
        {
            //step 1 - read the other-side address
            string otherHost = (string)data[0];
            int otherPort = (int)data[1];

            //we use these to store info
            string otherGatewayHost = (string) data[2];
            int otherGatewayPort = (int)data[3];

            logEvent("PM (" + gatewaySocket.ParentId + ") - Receive Connect Request - PeerAddress = " + otherHost+":"+otherPort.ToString() + ", PeerId = " + senderId);

            //step 2 - create a client peer socket and connect to that address
            PeerSocket myClientSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, senderId, otherHost, otherPort);

            //step 3 - check if client is connected successfully
            if (!myClientSocket.IsConnected || (myClientSocket.PeerHost != otherHost) ||
                (myClientSocket.PeerPort != otherPort))
            {
                myClientSocket.Dispose();
                myClientSocket = null;

                return;
            }

            //step 4 - add this client socket to peers
            peers.Add(senderId, myClientSocket);
            peersExpireDates[senderId] = DateTime.Now.AddMilliseconds(PeerMsgDef.HeartbeatValidness);

            //step 5 - let sender know that I am connected
            sender.SendMessageAsync(msgCode, msgId, true);

            if (PeerConnected != null)
            {
                PeerConnected(senderId, true, otherGatewayHost, otherGatewayPort );
            }
        }
Beispiel #2
0
        private void onDisconnectReceived(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data)
        {
            //if the sender is my gateway ignore this, as temp sockets will always disconnect from it ASAP
            if (sender == gatewaySocket) return;

            //dispose and remove my socket
            sender.Dispose();

            peers.Remove(senderId);
            peersExpireDates.Remove(senderId);

            if (PeerDisconnected != null)
            {
                //upon disconnect, we know host and port of peer
                PeerDisconnected(senderId, false, null, -1);
            }
        }
Beispiel #3
0
        public bool Connect(string host, int port, string peerId)
        {
            if (gatewaySocket == null) return false;

            //step 1 - create a server peerSocket
            PeerSocket myServerSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, peerId);

            //step 2 - create a temp client peer socket to send my address to the peer's gateway
            PeerSocket tempClientSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, peerId, host, port);

            //step 3 - use temp client socket to send my server socket address to peer
            List<object> response = null;

            try
            {
                response = tempClientSocket.SendMessage(PeerMsgDef.ConnectMsgCode, myServerSocket.Host, myServerSocket.Port, gatewaySocket.Host, gatewaySocket.Port);
            }
            catch (Exception exc)
            {
                myServerSocket.Dispose();
                myServerSocket = null;

                throw;
            }
            finally
            {
                //step 4 - dispose temp socket
                tempClientSocket.Dispose();
                tempClientSocket = null;
            }

            //step 5 - was connect successfull?
            if (response == null)
            {
                myServerSocket.Dispose();
                myServerSocket = null;
                return false;
            }

            //step 6 - extract address that peer has prepared for me (peer will connect to my server socket automatically)
            bool done = (bool)response[0];

            //step 7 - check correctness of the address
            if (!myServerSocket.IsConnected || !done)
            {
                myServerSocket.Dispose();
                myServerSocket = null;
                return false;
            }

            //step 8 - store peer socket
            peers.Add(peerId, myServerSocket);
            peersExpireDates[peerId] = DateTime.Now.AddMilliseconds(PeerMsgDef.HeartbeatValidness);

            logEvent("PM (" + gatewaySocket.ParentId + ") Connecting- PeerAddress = " + myServerSocket.PeerAddress + " PeerId = " + peerId);

            return true;
        }