Пример #1
0
        public static void AsyncConnectForIdentity(ConnectionRoute route, SocketAsyncCallbackEventHandler asyncCallback, SocketAsyncExceptionEventHandler exceptionCallback)
        {
            Task.Run(() =>
            {
                try
                {
                    SocketIdentity identity = SocketIdentity.None;
                    SocketClient client     = new SocketClient(route.NextNode, route.IsNextNodeProxy);
                    //client.Connect(Config.SocketSendTimeout, Config.SocketReceiveTimeout);
                    client.ConnectWithTimeout(Config.BuildConnectionTimeout);
                    client.SetTimeout(Config.SocketSendTimeout, Config.SocketReceiveTimeout);
                    if (route.IsNextNodeProxy)
                    {
                        /// 向代理服务器申请建立与服务端通信隧道, 并等待隧道建立完成
                        client.SendBytes(SocketPacketFlag.ProxyRouteRequest, route.GetBytes(node_start_index: 1), i1: 0);
                        client.ReceiveBytes(out HB32Header header, out byte[] bytes);
                        if (header.Flag != SocketPacketFlag.ProxyResponse)
                        {
                            throw new Exception(string.Format("Proxy exception at depth {0} : {1}. {2}",
                                                              header.I1, route.ProxyRoute[header.I1], Encoding.UTF8.GetString(bytes)));
                        }
                    }
                    /// 获取 socket 权限
                    client.SendBytes(SocketPacketFlag.AuthenticationRequest, Config.KeyBytes);
                    client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.AuthenticationResponse, out HB32Header auth_header);
                    identity = (SocketIdentity)auth_header.I1;
                    client.Close();
                    asyncCallback.Invoke(null, EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    exceptionCallback.Invoke(null, new SocketAsyncExceptionEventArgs(ex));
                }
            });



            /*
             *
             * 原来这种方案同样是异步执行, 总会提前返回 SocketIdentity.None
             * SocketIdentity identity = SocketIdentity.None;
             * SocketClient client = new SocketClient(route.NextNode, route.IsNextNodeProxy);
             * client.SocketAsyncCallback += (object sender, EventArgs e) =>
             * {
             *  if (route.IsNextNodeProxy)
             *  {
             *      /// 向代理服务器申请建立与服务端通信隧道, 并等待隧道建立完成
             *      client.SendBytes(SocketPacketFlag.ProxyRouteRequest, route.GetBytes(node_start_index: 1));
             *      client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.ProxyResponse);
             *  }
             *  /// 获取 socket 权限
             *  client.SendBytes(SocketPacketFlag.AuthenticationRequest, Config.KeyBytes);
             *  client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.AuthenticationResponse, out HB32Header header);
             *  identity = (SocketIdentity)header.I1;
             *  client.Close();
             * };
             * client.SocketAsyncCallback += asyncCallback;
             * client.SocketAsyncException += exceptionCallback;
             * client.AsyncConnect(Config.SocketSendTimeout, Config.SocketReceiveTimeout);
             */
        }
Пример #2
0
 public static void AsyncConnectForIdentity(SocketAsyncCallbackEventHandler asyncCallback, SocketAsyncExceptionEventHandler exceptionCallback)
 {
     AsyncConnectForIdentity(CurrentRoute, asyncCallback, exceptionCallback);
 }