예제 #1
0
파일: TCPClient.cs 프로젝트: git4paulqu/Net
        private void ConnetAsync()
        {
            IPEndPoint remote = NetUtility.GetIPEndPoint(setting.host, setting.port);

            if (null == remote)
            {
                NetDebug.Log("[TCPClient] remote can not be null.");
                return;
            }

            socket = new Socket(remote.AddressFamily,
                                SocketType.Stream,
                                ProtocolType.Tcp);
            socket.Blocking = setting.blocking;
            socket.NoDelay  = setting.noDelay;

            SocketAsyncEventArgs saea = new SocketAsyncEventArgs();

            saea.Completed     += new EventHandler <SocketAsyncEventArgs>(OnSAEACompleted);
            saea.AcceptSocket   = socket;
            saea.RemoteEndPoint = remote;

            bool willRaiseEvent = socket.ConnectAsync(saea);

            if (!willRaiseEvent)
            {
                ConnectAsyncCallback(saea);
            }
        }
예제 #2
0
        private KCPStateObject InitKCPObject(EndPoint remote)
        {
            lock (lockObject)
            {
                KCPStateObject kcp       = null;
                string         remoteKey = RemotePoint2Key(remote);
                if (map_remote2KCP.TryGetValue(remoteKey, out kcp))
                {
                    return(kcp);
                }

                NetDebug.Log("[S] InitKCPObject, remote:{0}", remote);

                kcp = new KCPStateObject();
                kcp.Initialize(remote, setting.kcp);
                kcp.remote = remote;

                kcp.sendHandle      = OnKCPSend;
                kcp.receiveHandle   = OnKCPReceive;
                kcp.timeoutCallback = OnTimeoutCallback;
                map_remote2KCP.Add(remoteKey, kcp);

                return(kcp);
            }
        }
예제 #3
0
파일: TCPClient.cs 프로젝트: git4paulqu/Net
        public void Connect()
        {
            NetDebug.Log("[TCPClient] Try to Connect {0}:{1}.", setting.host, setting.port);

            remote = NetUtility.FormatIPEndPoint(setting.host, setting.port);
            Reset();
            ConnetAsync();
        }
예제 #4
0
 private void OnTimeoutCallback(KCPStateObject kcpObject)
 {
     NetDebug.Log("[OnTimeoutCallback] {0}", kcpObject.remote);
     if (null != kcpObject)
     {
         Close(kcpObject.remote);
     }
 }
예제 #5
0
 private void Debug(object obj, params object[] args)
 {
     lock (_debugMutex)
     {
         Console.ForegroundColor = IsServer ? ConsoleColor.Cyan : ConsoleColor.Green;
         NetDebug.Log(obj.ToString(), args);
         Console.ResetColor();
     }
 }
예제 #6
0
        public void Send(string remote, byte[] data)
        {
            CSConnection connection = null;

            if (!map_remote2Connection.TryGetValue(remote, out connection))
            {
                NetDebug.Log("[TCPServer] try send, but the remote:{0} is null.", remote);
                return;
            }
            connection.Send(data);
        }
예제 #7
0
 public void Send(byte[] data, int offset, int count)
 {
     if (null != _kcpObject)
     {
         _kcpObject.Send(data, offset, count);
     }
     else
     {
         NetDebug.Log("KCP connection send error, the kcp object is not be null.");
     }
 }
 public void Close()
 {
     NetDebug.Log("UDPDiscoverClient.Close");
     if (client != null)
     {
         client.Close();
         client.Dispose();
         client = null;
     }
     if (sendThread != null)
     {
         sendThread.Abort();
         sendThread = null;
     }
 }
예제 #9
0
파일: TCPClient.cs 프로젝트: git4paulqu/Net
        private void OnConnectCallback(bool connect, Socket connectSocket)
        {
            NetDebug.Log("[TCPClient] connect to {0}:{1} result:{2}.",
                         setting.host,
                         setting.port,
                         connect);

            if (connect)
            {
                ReceiveAsync();
                onConnectCallback(null);
                return;
            }

            onConnectFailedCallback(null);
        }
예제 #10
0
        public void Listen()
        {
            NetDebug.Log("[TCPServer] Listen.");

            IPEndPoint remote = NetUtility.GetIPEndPoint(setting.host, setting.port);

            if (null == remote)
            {
                NetDebug.Log("[TCPServer] ip port can not be null.");
                return;
            }

            socket = new Socket(remote.AddressFamily,
                                SocketType.Stream,
                                ProtocolType.Tcp);
            socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            socket.Blocking = setting.blocking;
            socket.Bind(remote);
            socket.Listen(setting.backlog);
            AcceptAsync();
        }
예제 #11
0
        public void Connect(string address, int port)
        {
            Initialize();
            IsServer = false;
            State    = UDPConnectionState.OPENING;

            remoterPoint = NetUtility.GetIPEndPoint(address, port);
            if (null == remoterPoint)
            {
                NetDebug.Log("[TCPClient] remote can not be null.");
                return;
            }

            socket = new Socket(remoterPoint.AddressFamily,
                                SocketType.Dgram,
                                ProtocolType.Udp);
            socket.Connect(remoterPoint);

            ReceiveFromAsync(remoterPoint);
            //socket.Blocking = setting.blocking;
            //socket.NoDelay = setting.noDelay;

            Send(remoterPoint, UDPPacketType.SYN);
        }