示例#1
0
        private static void on_local_connected(IAsyncResult ar)
        {
            object[]  ar_arr   = ar.AsyncState as object[];
            Socket    sock_svr = ar_arr[0] as Socket;
            WorkGroup work     = (WorkGroup)ar_arr[1];

            ++_stat_info[work._id]._connect;
            Socket sock_cli = sock_svr.EndAccept(ar);

            sock_svr.BeginAccept(on_local_connected, ar.AsyncState);
            Socket sock_cli_remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                sock_cli_remote.Connect(work._point_out_host, work._point_out_port);
            }
            catch (Exception exp)
            {
                try
                {
                    sock_cli.Shutdown(SocketShutdown.Both);
                    sock_cli_remote.Shutdown(SocketShutdown.Both);
                    sock_cli.Close();
                    sock_cli_remote.Close();
                }
                catch (Exception) { }
                --_stat_info[work._id]._connect;
                return;
            }
            Thread t_send = new Thread(new ParameterizedThreadStart(recv_and_send_caller))
            {
                IsBackground = true
            };
            Thread t_recv = new Thread(new ParameterizedThreadStart(recv_and_send_caller))
            {
                IsBackground = true
            };

            t_send.Start(new object[] { sock_cli, sock_cli_remote, work._id, true });
            t_recv.Start(new object[] { sock_cli_remote, sock_cli, work._id, false });
            t_send.Join();
            t_recv.Join();
            --_stat_info[work._id]._connect;
        }
示例#2
0
        private static void map_start(WorkGroup work)
        {
            Socket sock_svr    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            bool   start_error = false;

            try
            {
                sock_svr.Bind(work._point_in);
                sock_svr.Listen(10);
                sock_svr.BeginAccept(on_local_connected, new object[] { sock_svr, work });
            }
            catch (Exception exp)
            {
                start_error = true;
            }
            finally
            {
                _stat_info.Add(work._id, new WorkStatus(work._server, work._point_in.ToString(), work._point_out_host + ":" + work._point_out_port, !start_error, 0, 0, 0));
            }
        }
示例#3
0
        private static List <WorkGroup> load_maps_cfg()
        {
            List <WorkGroup> rtn = new List <WorkGroup>();
            WorkGroup        rtn_item;

            foreach (string item in _mapping)
            {
                string[] temp = item.Split(new[] { '|' });

                rtn_item = new WorkGroup {
                    _id = ++_id_plus
                };
                rtn_item._server = temp[0];

                ushort    port;
                IPAddress ip;
                for (int i = 0; i < 2; i++)
                {
                    string[] iport = temp[i + 1].Split(':');

                    ip   = IPAddress.Any;
                    port = ushort.Parse(iport[1]);

                    if (i == 0)
                    {
                        rtn_item._point_in = new IPEndPoint(ip, port);
                    }
                    else
                    {
                        rtn_item._point_out_host = iport[0];
                        rtn_item._point_out_port = ushort.Parse(iport[1]);
                    }
                }

                rtn.Add(rtn_item);
            }

            return(rtn);
        }