コード例 #1
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public ClientOps()
 {
     callBackObj        = null;
     hostName           = ServerConf.DEFAULT_HOSTNAME;
     port               = ServerConf.DEFAULT_PORT;
     noDelay            = true;
     waitTimeInMilliSec = Timeout.Infinite;
 }
コード例 #2
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="callBackObj">callback object</param>
 /// <param name="hostName">hostname</param>
 /// <param name="port">port</param>
 /// <param name="noDelay">flag for no delay</param>
 /// <param name="waitTimeInMilliSec">wait time in millisecond</param>
 public ClientOps(INetworkClientCallback callBackObj, String hostName, String port, bool noDelay = true, int waitTimeInMilliSec = Timeout.Infinite)
 {
     this.callBackObj        = callBackObj;
     this.hostName           = hostName;
     this.port               = port;
     this.noDelay            = noDelay;
     this.waitTimeInMilliSec = waitTimeInMilliSec;
 }
コード例 #3
0
        /// <summary>
        /// Default constructor
        /// </summary>
        public ClientOps()
		{
			callBackObj=null;
			hostName=ServerConf.DEFAULT_HOSTNAME;
			port=ServerConf.DEFAULT_PORT;
            noDelay = true;
            waitTimeInMilliSec = Timeout.Infinite;
		}
コード例 #4
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="callBackObj">callback object</param>
 /// <param name="hostName">hostname</param>
 /// <param name="port">port</param>
 /// <param name="noDelay">flag for no delay</param>
 /// <param name="connectionTimeOut">connection wait time in millisecond</param>
 public ClientOps(INetworkClientCallback callBackObj, String hostName, String port, bool noDelay = true, int connectionTimeOut = Timeout.Infinite)
 {
     this.CallBackObj       = callBackObj;
     this.HostName          = hostName;
     this.Port              = port;
     this.NoDelay           = noDelay;
     this.ConnectionTimeOut = connectionTimeOut;
 }
コード例 #5
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="packetType">packet type</param>
 /// <param name="packet">packet</param>
 /// <param name="offset">offset</param>
 /// <param name="size">size of packet in byte</param>
 /// <param name="iocpTcpClient">client</param>
 /// <param name="dataPacket">data packet for send</param>
 public PacketTransporter(PacketType packetType, Packet packet, int offset, int size, IocpTcpClient iocpTcpClient, Packet dataPacket = null)
 {
     m_packetType    = packetType;
     m_packet        = packet;
     m_offset        = offset;
     m_size          = size;
     m_iocpTcpClient = iocpTcpClient;
     m_dataPacket    = dataPacket;
     m_callBackObj   = iocpTcpClient.m_callBackObj;
 }
コード例 #6
0
 public PacketTransporter(PacketType packetType, Packet packet, int offset, int size, AsyncTcpClient tcpClient, int dataType = -1, Packet dataPacket = null)
 {
     m_packetType  = packetType;
     m_packet      = packet;
     m_offset      = offset;
     m_size        = size;
     m_TcpClient   = tcpClient;
     m_dataPacket  = dataPacket;
     m_DataType    = dataType;
     m_callBackObj = tcpClient.m_callBackObj;
 }
コード例 #7
0
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;

            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive)
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        new Task(delegate()
                        {
                            OnConnected(this, status);
                        }).Start();
                        return;
                    }

                    CallBackObj       = m_clientOps.CallBackObj;
                    HostName          = m_clientOps.HostName;
                    Port              = m_clientOps.Port;
                    NoDelay           = m_clientOps.NoDelay;
                    ConnectionTimeOut = m_clientOps.ConnectionTimeOut;

                    if (HostName == null || HostName.Length == 0)
                    {
                        HostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }

                    m_client         = new TcpClient();
                    m_client.NoDelay = NoDelay;

                    m_client.Client.BeginConnect(HostName, Convert.ToInt32(Port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(ConnectionTimeOut))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            new Task(delegate()
                            {
                                OnConnected(this, status);
                            }).Start();
                            return;
                        }
                        IsConnectionAlive = true;
                        new Task(delegate()
                        {
                            OnConnected(this, ConnectStatus.SUCCESS);
                        }).Start();
                    }
                    else
                    {
                        try
                        {
                            if (m_client.Client.Connected)
                            {
                                m_client.Client.Shutdown(SocketShutdown.Both);
                                //Client.Client.Disconnect(true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        }
                        m_client.Close();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        Task t = new Task(delegate()
                        {
                            OnConnected(this, status);
                        });
                        t.Start();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                Task t = new Task(delegate()
                {
                    OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                });
                t.Start();
                return;
            }
            startReceive();
        }
コード例 #8
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="callBackObj">callback object</param>
 /// <param name="hostName">hostname</param>
 /// <param name="port">port</param>
 /// <param name="noDelay">flag for no delay</param>
 /// <param name="connectionTimeOut">connection wait time in millisecond</param>
 public ClientOps(INetworkClientCallback callBackObj, String hostName, String port, bool noDelay = true, int connectionTimeOut = Timeout.Infinite)
 {
     this.CallBackObj = callBackObj;
     this.HostName = hostName;
     this.Port = port;
     this.NoDelay = noDelay;
     this.ConnectionTimeOut = connectionTimeOut;
 }
コード例 #9
0
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;
            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive)
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        throw new CallbackException();
                    }

                    CallBackObj = m_clientOps.CallBackObj;
                    HostName = m_clientOps.HostName;
                    Port = m_clientOps.Port;
                    NoDelay = m_clientOps.NoDelay;
                    ConnectionTimeOut = m_clientOps.ConnectionTimeOut;


                    if (HostName == null || HostName.Length == 0)
                    {
                        HostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }

                    m_client = new TcpClient();
                    m_client.NoDelay = NoDelay;

                    m_client.Client.BeginConnect(HostName, Convert.ToInt32(Port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(ConnectionTimeOut))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            throw new CallbackException();
                        }
                        IsConnectionAlive = true;
                        Task t = new Task(delegate()
                        {
                            OnConnected(this, ConnectStatus.SUCCESS);
                        });
                        t.Start();


                    }
                    else
                    {
                        try
                        {
                            m_client.Client.Shutdown(SocketShutdown.Both);
                            //Client.Client.Disconnect(true);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        }
                        m_client.Close();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        throw new CallbackException();
                    }
                }
            }
            catch(CallbackException)
            {
                Task t = new Task(delegate()
                {
                    OnConnected(this, status);
                });
                t.Start();
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                Task t = new Task(delegate()
                {
                    OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                });
                t.Start();
                return;
            }
            startReceive();

        }
コード例 #10
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="packetType">packet type</param>
 /// <param name="packet">packet</param>
 /// <param name="offset">offset</param>
 /// <param name="size">size of packet in byte</param>
 /// <param name="iocpTcpClient">client</param>
 /// <param name="dataPacket">data packet for send</param>
 public PacketTransporter(PacketType packetType,Packet packet, int offset, int size, IocpTcpClient iocpTcpClient,Packet dataPacket=null)
 {
     m_packetType = packetType;
     m_packet = packet;
     m_offset = offset;
     m_size = size;
     m_iocpTcpClient = iocpTcpClient;
     m_dataPacket = dataPacket;
     m_callBackObj = iocpTcpClient.CallBackObj;
 }
コード例 #11
0
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;

            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive())
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        throw new CallbackException();
                    }

                    m_callBackObj        = m_clientOps.callBackObj;
                    m_hostName           = m_clientOps.hostName;
                    m_port               = m_clientOps.port;
                    m_noDelay            = m_clientOps.noDelay;
                    m_waitTimeInMilliSec = m_clientOps.waitTimeInMilliSec;

                    if (m_hostName == null || m_hostName.Length == 0)
                    {
                        m_hostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (m_port == null || m_port.Length == 0)
                    {
                        m_port = ServerConf.DEFAULT_PORT;
                    }


                    m_client.NoDelay = m_noDelay;

                    m_client.BeginConnect(m_hostName, Convert.ToInt32(m_port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(m_waitTimeInMilliSec))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            throw new CallbackException();
                        }
                        m_isConnected  = true;
                        m_clientStream = new NetworkStream(m_client.Client);
                        if (m_callBackObj != null)
                        {
                            Thread t = new Thread(delegate()
                            {
                                m_callBackObj.OnConnected(this, ConnectStatus.SUCCESS);
                            });
                            t.Start();
                        }
                    }
                    else
                    {
                        m_client.Dispose();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        throw new CallbackException();
                    }
                }
            }
            catch (CallbackException)
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, status);
                    });
                    t.Start();
                }
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                    });
                    t.Start();
                }
                return;
            }
            startReceive();
        }
コード例 #12
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="callBackObj">callback object</param>
 /// <param name="hostName">hostname</param>
 /// <param name="port">port</param>
 /// <param name="noDelay">flag for no delay</param>
 /// <param name="waitTimeInMilliSec">wait time in millisecond</param>
 public ClientOps(INetworkClientCallback callBackObj, String hostName, String port, bool noDelay = true, int waitTimeInMilliSec = Timeout.Infinite)
 {
     this.callBackObj = callBackObj;
     this.hostName = hostName;
     this.port = port;
     this.noDelay = noDelay;
     this.waitTimeInMilliSec = waitTimeInMilliSec;
 }
コード例 #13
0
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;
            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive())
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        throw new CallbackException();
                    }

                    m_callBackObj = m_clientOps.callBackObj;
                    m_hostName = m_clientOps.hostName;
                    m_port = m_clientOps.port;
                    m_noDelay = m_clientOps.noDelay;
                    m_waitTimeInMilliSec = m_clientOps.waitTimeInMilliSec;

                    if (m_hostName == null || m_hostName.Length == 0)
                    {
                        m_hostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (m_port == null || m_port.Length == 0)
                    {
                        m_port = ServerConf.DEFAULT_PORT;
                    }


                    m_client.NoDelay = m_noDelay;

                    m_client.BeginConnect(m_hostName, Convert.ToInt32(m_port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(m_waitTimeInMilliSec))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            throw new CallbackException();
                        }
                        m_isConnected = true;
                        m_clientStream = new NetworkStream(m_client.Client);
                        if (m_callBackObj != null)
                        {
                            Thread t = new Thread(delegate()
                            {
                                m_callBackObj.OnConnected(this, ConnectStatus.SUCCESS);
                            });
                            t.Start();
                        }
                           
                    }
                    else
                    {
                        m_client.Dispose();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        throw new CallbackException();
                    }
              
                }
            }
            catch(CallbackException)
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, status);
                    });
                    t.Start();
                    
                }
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                    });
                    t.Start();
                   
                }
                return;
            }
            startReceive();

        }