예제 #1
0
        /// <summary>
        /// 异步连接到一个支持SM协议的计算机端口
        /// </summary>
        /// <param name="tstr">远程TcpStruct</param>
        public void AsyncConnectTo(TcpStruct tstr)
        {
            _remoteTcpStruct.IP   = tstr.IP;
            _remoteTcpStruct.Port = tstr.Port;

            Threads.ExecAsync(async_th_connect);
        }
예제 #2
0
 internal void onError(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpError error, Exception ex, TcpOptionType type, ITcpReader tcpReader)
 {
     if (OnError != null)
     {
         OnError(localTcpInfo, remoteTcpInfo, error, ex, type, tcpReader);
     }
 }
예제 #3
0
 internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, __listen__readSmmp tcpReader)
 {
     if (OnReceivedData != null)
     {
         OnReceivedData(localTcpInfo, remoteTcpInfo, packet, tcpReader);
     }
 }
예제 #4
0
 internal void onConnectionEvent(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, ITcpReader tcpClient, int clientID)
 {
     if (OnConnectionEvent != null)
     {
         OnConnectionEvent(localTcpInfo, remoteTcpInfo, tcpClient, clientID);
     }
 }
예제 #5
0
 internal void onConnectionEvent(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, __listen__readSmmp tcpClient, int clientID)
 {
     if (OnConnectionEvent != null)
     {
         OnConnectionEvent(localTcpInfo, remoteTcpInfo, tcpClient, clientID);
     }
 }
예제 #6
0
 internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, byte[] data, ITcpReader tcpReader)
 {
     if (OnReceivedData != null)
     {
         OnReceivedData(localTcpInfo, remoteTcpInfo, data, tcpReader);
     }
 }
예제 #7
0
 internal void onClose(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, int clientID)
 {
     _remoteTcpStruct.IP   = null;
     _remoteTcpStruct.Port = 0;
     if (_tcpMode == TcpMode.Client)
     {
         CloseConnect();
         _tcpMode = TcpMode.Unknown;
     }
     else if (_tcpMode == TcpMode.Server)
     {
         lock (_tcp_Reader_Array)
         {
             foreach (object obj in _tcp_Reader_Array)
             {
                 if (obj == null)
                 {
                     continue;
                 }
                 if (((__listen__readSmtcp)obj).ClientID == clientID)
                 {
                     _tcp_Reader_Array.Remove(obj);
                     break;
                 }
             }
         }
     }
     onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.Disconnected, null);
 }
예제 #8
0
        internal void onTcpEvents(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpOptionType type, ITcpReader tcpReader)
        {
            switch (type)
            {
            case TcpOptionType.StartListen:
                _listenning = true;
                break;

            case TcpOptionType.StopListen:
                _listenning = false;
                _listenWork = false;
                break;

            case TcpOptionType.ClientConnected:
                break;

            case TcpOptionType.Connected:
                break;

            case TcpOptionType.Disconnected:
                break;

            case TcpOptionType.Connecting:
                Thread.Sleep(0);
                break;

            default:
                break;
            }
            if (OnTcpEvents != null)
            {
                OnTcpEvents(localTcpInfo, remoteTcpInfo, type, tcpReader);
            }
        }
예제 #9
0
 private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, byte[] rawData)
 {
     if (OnReceivedData != null)
     {
         OnReceivedData(localTcpInfo, remoteTcpInfo, packet, this);
     }
     _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, packet, this);
 }
예제 #10
0
 private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, byte[] data)
 {
     if (OnReceivedData != null)
     {
         OnReceivedData(localTcpInfo, remoteTcpInfo, data, this);
     }
     _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, data, this);
 }
예제 #11
0
        /// <summary>
        /// 在指定的端口开始监听网络
        /// </summary>
        /// <param name="tstr">指定远程的计算机Tcp结构</param>
        public void StartListen(TcpStruct tstr)
        {
            try
            {
                if (_tl == null)
                {
                    _tl = new TcpListener(tstr.IP, tstr.Port);
                }
                _tl.Start();
            }
            catch (Exception ex) { onError(tstr, _remoteTcpStruct, TcpError.UncreateListen, ex, TcpOptionType.CreateListen, null); return; }

            _localTcpStruct.IP   = tstr.IP;
            _localTcpStruct.Port = tstr.Port;
            onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.StartListen, null);
            _tcpMode = TcpMode.Server;
            ReadDataFromListen();
        }
예제 #12
0
        /// <summary>
        /// 连接到一个支持SM协议的计算机端口
        /// </summary>
        /// <param name="tstr">指定远程的计算机Tcp结构</param>
        public TcpResult ConnectTo(TcpStruct tstr)
        {
            TcpResult result = new TcpResult();

            result.Success = false;
            onTcpEvents(this._localTcpStruct, this._remoteTcpStruct, TcpOptionType.Connecting, null);
            if (Connected)
            {
                onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientIsConnected, null, TcpOptionType.Connecting, null);
                result.Success = false;
                result.Error   = TcpError.TcpClientIsConnected;
                return(result);
            }
            _tc = new TcpClient();
            try
            {
                _tc.Connect(tstr.IP, tstr.Port);
                _tcpMode       = TcpMode.Client;
                _ns            = _tc.GetStream();
                result.Success = true;
            }
            catch (Exception ex)
            {
                onError(_localTcpStruct, _remoteTcpStruct, TcpError.ServerOffline, ex, TcpOptionType.Connecting, null);
                result.Error   = TcpError.ServerOffline;
                result.Success = false;
                return(result);
            }
            _remoteTcpStruct.IP   = tstr.IP;
            _remoteTcpStruct.Port = tstr.Port;

            FormatIPStringToTcpStruct(_tc.Client.LocalEndPoint.ToString(), ref _localTcpStruct);
            onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.Connected, null);
            if (!UseBlockRead)
            {
                Threads.ExecAsync(ReadDataFromConnectRemote);
            }
            return(result);
        }
예제 #13
0
 internal void onError(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpError error, Exception ex, TcpOptionType type, __listen__readSmmp tcpReader)
 {
     if (OnError != null) OnError(localTcpInfo, remoteTcpInfo, error, ex, type, tcpReader);
 }
예제 #14
0
 internal void onConnectionEvent(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, __listen__readSmmp tcpClient, int clientID)
 {
     if (OnConnectionEvent != null) OnConnectionEvent(localTcpInfo, remoteTcpInfo, tcpClient, clientID);
 }
예제 #15
0
 internal void onClose(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, int clientID)
 {
     _remoteTcpStruct.IP = null;
     _remoteTcpStruct.Port = 0;
     if (_tcpMode == TcpMode.Client)
     {
         CloseConnect();
         _tcpMode = TcpMode.Unknown;
     }
     else if (_tcpMode == TcpMode.Server)
     {
         lock (_tcp_Reader_Array)
         {
             foreach (object obj in _tcp_Reader_Array)
             {
                 if (obj == null) continue;
                 if (((__listen__readSmmp)obj).ClientID == clientID)
                 {
                     _tcp_Reader_Array.Remove(obj);
                     break;
                 }
             }
         }
     }
     onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.Disconnected, null);
 }
예제 #16
0
        /// <summary>
        /// ��ָ���Ķ˿ڿ�ʼ��������
        /// </summary>
        /// <param name="tstr">ָ��Զ�̵ļ����Tcp�ṹ</param>
        public void StartListen(TcpStruct tstr)
        {
            try
            {
                if (_tl == null) _tl = new TcpListener(tstr.IP, tstr.Port);
                _tl.Start();
            }
            catch (Exception ex) { onError(tstr, _remoteTcpStruct, TcpError.UncreateListen, ex, TcpOptionType.CreateListen, null); return; }

            _localTcpStruct.IP = tstr.IP;
            _localTcpStruct.Port = tstr.Port;
            onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.StartListen, null);
            _tcpMode = TcpMode.Server;
            ReadDataFromListen();
        }
예제 #17
0
        /// <summary>
        /// ���ӵ�һ��֧��SMЭ��ļ�����˿�
        /// </summary>
        /// <param name="tstr">ָ��Զ�̵ļ����Tcp�ṹ</param>
        public TcpResult ConnectTo(TcpStruct tstr)
        {
            TcpResult result = new TcpResult();
            result.Success = false;
            onTcpEvents(this._localTcpStruct, this._remoteTcpStruct, TcpOptionType.Connecting, null);
            if (Connected)
            {
                onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientIsConnected, null, TcpOptionType.Connecting, null);
                result.Success = false;
                result.Error = TcpError.TcpClientIsConnected;
                return result;
            }
            _tc = new TcpClient();
            try
            {
                _tc.Connect(tstr.IP, tstr.Port);
                _tcpMode = TcpMode.Client;
                _ns = _tc.GetStream();
                result.Success = true;
            }
            catch (Exception ex)
            {
                onError(_localTcpStruct, _remoteTcpStruct, TcpError.ServerOffline, ex, TcpOptionType.Connecting, null);
                result.Error = TcpError.ServerOffline;
                result.Success = false;
                return result;
            }
            _remoteTcpStruct.IP = tstr.IP;
            _remoteTcpStruct.Port = tstr.Port;

            FormatIPStringToTcpStruct(_tc.Client.LocalEndPoint.ToString(), ref _localTcpStruct);
            onTcpEvents(_localTcpStruct, _remoteTcpStruct, TcpOptionType.Connected, null);
            if (!UseBlockRead)
                Threads.ExecAsync(ReadDataFromConnectRemote);
            return result;
        }
예제 #18
0
        /// <summary>
        /// �첽���ӵ�һ��֧��SMЭ��ļ�����˿�
        /// </summary>
        /// <param name="tstr">Զ��TcpStruct</param>
        public void AsyncConnectTo(TcpStruct tstr)
        {
            _remoteTcpStruct.IP = tstr.IP;
            _remoteTcpStruct.Port = tstr.Port;

            Threads.ExecAsync(async_th_connect);
        }
예제 #19
0
 private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, byte[] rawData)
 {
     if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, packet, this);
     _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, packet, this);
 }
예제 #20
0
 private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, byte[] data)
 {
     if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, data, this);
     _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, data, this);
 }
예제 #21
0
 internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, byte[] data, ITcpReader tcpReader)
 {
     if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, data, tcpReader);
 }
예제 #22
0
 internal void onConnectionEvent(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, ITcpReader tcpClient, int clientID)
 {
     if (OnConnectionEvent != null) OnConnectionEvent(localTcpInfo, remoteTcpInfo, tcpClient, clientID);
 }
예제 #23
0
 public static string GetIPPortString(TcpStruct tcpStruct)
 {
     return tcpStruct.IP + ":" + tcpStruct.Port;
 }
예제 #24
0
 /// <summary>
 /// 把一个标准EndPoint字符串填充到TcpStruct里面
 /// </summary>
 /// <param name="ipstring">标准的EndPoint字符串</param>
 /// <param name="tstr">TcpStruct地址传入</param>
 public static void FormatIPStringToTcpStruct(string ipstring, ref TcpStruct tstr)
 {
     string[] s = ipstring.Split(new string[] { ":" }, StringSplitOptions.None);
     tstr.IP   = IPAddress.Parse(s[0]);
     tstr.Port = int.Parse(s[1]);
 }
예제 #25
0
 public static string GetIPPortString(TcpStruct tcpStruct)
 {
     return(tcpStruct.IP + ":" + tcpStruct.Port);
 }
예제 #26
0
 internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, __listen__readSmmp tcpReader)
 {
     if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, packet, tcpReader);
 }
예제 #27
0
 internal void onTcpEvents(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpOptionType type, __listen__readSmmp tcpReader)
 {
     switch (type)
     {
         case TcpOptionType.StartListen:
             _listenning = true;
             break;
         case TcpOptionType.StopListen:
             _listenning = false;
             _listenWork = false;
             break;
         case TcpOptionType.ClientConnected:
             break;
         case TcpOptionType.Connected:
             break;
         case TcpOptionType.Disconnected:
             if (_tcpMode == TcpMode.Client)
                 _tcpMode = TcpMode.Unknown;
             break;
         case TcpOptionType.Connecting:
             Thread.Sleep(0);
             break;
         default:
             break;
     }
     if (OnTcpEvents != null) OnTcpEvents(localTcpInfo, remoteTcpInfo, type, tcpReader);
 }
예제 #28
0
 /// <summary>
 /// ��һ����׼EndPoint�ַ�����䵽TcpStruct����
 /// </summary>
 /// <param name="ipstring">��׼��EndPoint�ַ���</param>
 /// <param name="tstr">TcpStruct��ַ����</param>
 public static void FormatIPStringToTcpStruct(string ipstring, ref TcpStruct tstr)
 {
     string[] s = ipstring.Split(new string[] { ":" }, StringSplitOptions.None);
     tstr.IP = IPAddress.Parse(s[0]);
     tstr.Port = int.Parse(s[1]);
 }