Exemplo n.º 1
0
        /// <summary>
        /// Listen for data, while explicitly defining what callback we should use when data is recieved.
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="dataRecievedCallback"></param>
        public static void listenForData(SocketState ss, SocketState.EventProcessor dataRecievedCallback)
        {
            if (!ss.SafeToSendRequest)
            {
                return;
            }

            ss.TheCallback = dataRecievedCallback;

            // Start listening for a message
            // When a message arrives, handle it on a new thread with ReceiveCallback
            try
            {
                ss.TheSocket.BeginReceive(ss.MessageBuffer, 0, ss.MessageBuffer.Length, SocketFlags.None, Networking.ReceiveCallback, ss);
            }
            catch (Exception e)
            {
                //If An Error Occurs Notify The Socket Owner
                ss.ErrorOccured  = true;
                ss.ErrorMesssage = e.Message;

                ss.TheCallback(ss);
                return;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Disconnects the specified socket contained in the passed SocketState, calling the passed handler when the socket closes.
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="reuse"></param>
        /// <param name="socketClosedHandler"></param>
        public static void Disconnect(SocketState ss, bool reuse, SocketState.EventProcessor socketClosedHandler)
        {
            if (ss.TheSocket == null || !ss.TheSocket.Connected)
            {
                socketClosedHandler(ss);
                return;
            }

            ss.TheCallback       = socketClosedHandler;
            ss.SafeToSendRequest = false;
            ss.TheSocket.BeginDisconnect(reuse, DisconnectedCallback, ss);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Start attempting to connect to the server
        /// </summary>
        /// <param name="host_name"> server to connect to </param>
        /// <returns></returns>
        public static Socket ConnectToNetworkNode(string hostName, SocketState.EventProcessor nodeConnectedCallback, int port = DEFAULT_PORT, int timeoutMs = -1)
        {
            System.Diagnostics.Debug.WriteLine("connecting  to " + hostName);

            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                IPHostEntry ipHostInfo;
                IPAddress   ipAddress = IPAddress.None;

                // Determine if the server address is a URL or an IP
                try
                {
                    ipHostInfo = Dns.GetHostEntry(hostName);
                    bool foundIPV4 = false;
                    foreach (IPAddress addr in ipHostInfo.AddressList)
                    {
                        if (addr.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            foundIPV4 = true;
                            ipAddress = addr;
                            break;
                        }
                    }
                    // Didn't find any IPV4 addresses
                    if (!foundIPV4)
                    {
                        System.Diagnostics.Debug.WriteLine("Invalid addres: " + hostName);
                        return(null);
                    }
                }
                catch (Exception)
                {
                    // see if host name is actually an ipaddress, i.e., 155.99.123.456
                    System.Diagnostics.Debug.WriteLine("using IP");
                    ipAddress = IPAddress.Parse(hostName);
                }

                // Create a TCP/IP socket.
                Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

                SocketState newSocketState = new SocketState(socket, nodeConnectedCallback);

                IAsyncResult result = socket.BeginConnect(ipAddress, port, Networking.ConnectedToNetworkNode, newSocketState);

                bool timedOut = !result.AsyncWaitHandle.WaitOne(timeoutMs, true);
                if (timedOut)
                {
                    newSocketState.ErrorOccured  = true;
                    newSocketState.ErrorMesssage = "Timeout: Couldn't Connect With The Server";

                    newSocketState.TheCallback(newSocketState);
                    socket.Close();
                }

                return(socket);
            }
            catch (Exception e)
            {
                SocketState errorSocketState = new SocketState(null, nodeConnectedCallback);
                errorSocketState.ErrorOccured  = true;
                errorSocketState.ErrorMesssage = e.Message;

                errorSocketState.TheCallback(errorSocketState);
                return(null);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Connect to a server using the default port for this class.
 /// </summary>
 /// <param name="hostName"></param>
 /// <param name="nodeConnectedCallback"></param>
 /// <returns></returns>
 public static Socket ConnectToNetworkNode(string hostName, SocketState.EventProcessor nodeConnectedCallback, int timeoutMs = -1)
 {
     return(Networking.ConnectToNetworkNode(hostName, nodeConnectedCallback, Networking.DEFAULT_PORT, timeoutMs));
 }