コード例 #1
0
 /// <summary>
 /// Launch Client
 /// </summary>
 public void StartAsClient(string ip, int port)
 {
     try
     {
         skt.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(ServerConnectCB), skt);
         IYTool.LogMsg("\nClient Start Success!\nConnecting To Server......", LogLevel.Info);
     }
     catch (Exception e)
     {
         IYTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
コード例 #2
0
 void ServerConnectCB(IAsyncResult ar)
 {
     try
     {
         skt.EndConnect(ar);
         session = new T();
         session.StartRcvData(skt, null);
     }
     catch (Exception e)
     {
         IYTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
コード例 #3
0
 /// <summary>
 /// Launch Server
 /// </summary>
 public void StartAsServer(string ip, int port)
 {
     try
     {
         skt.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
         skt.Listen(backlog);
         skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt);
         IYTool.LogMsg("\nServer Start Success!\nWaiting for Connecting......", LogLevel.Info);
     }
     catch (Exception e)
     {
         IYTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
コード例 #4
0
        private void SendCB(IAsyncResult ar)
        {
            NetworkStream ns = (NetworkStream)ar.AsyncState;

            try
            {
                ns.EndWrite(ar);
                ns.Flush();
                ns.Close();
            }
            catch (Exception e)
            {
                IYTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error);
            }
        }
コード例 #5
0
 private void RcvHeadData(IAsyncResult ar)
 {
     try
     {
         IYPkg pack = (IYPkg)ar.AsyncState;
         if (skt.Available == 0)
         {
             OnDisConnected();
             Clear();
             return;
         }
         int len = skt.EndReceive(ar);
         if (len > 0)
         {
             pack.headIndex += len;
             if (pack.headIndex < pack.headLen)
             {
                 skt.BeginReceive(
                     pack.headBuff,
                     pack.headIndex,
                     pack.headLen - pack.headIndex,
                     SocketFlags.None,
                     new AsyncCallback(RcvHeadData),
                     pack);
             }
             else
             {
                 pack.InitBodyBuff();
                 skt.BeginReceive(pack.bodyBuff,
                                  0,
                                  pack.bodyLen,
                                  SocketFlags.None,
                                  new AsyncCallback(RcvBodyData),
                                  pack);
             }
         }
         else
         {
             OnDisConnected();
             Clear();
         }
     }
     catch (Exception e)
     {
         IYTool.LogMsg("RcvHeadError:" + e.Message, LogLevel.Error);
     }
 }
コード例 #6
0
        private void RcvBodyData(IAsyncResult ar)
        {
            try
            {
                IYPkg pack = (IYPkg)ar.AsyncState;
                int   len  = skt.EndReceive(ar);
                if (len > 0)
                {
                    pack.bodyIndex += len;
                    if (pack.bodyIndex < pack.bodyLen)
                    {
                        skt.BeginReceive(pack.bodyBuff,
                                         pack.bodyIndex,
                                         pack.bodyLen - pack.bodyIndex,
                                         SocketFlags.None,
                                         new AsyncCallback(RcvBodyData),
                                         pack);
                    }
                    else
                    {
                        T msg = IYTool.DeSerialize <T>(pack.bodyBuff);
                        OnReciveMsg(msg);

                        //loop recive
                        pack.ResetData();
                        skt.BeginReceive(
                            pack.headBuff,
                            0,
                            pack.headLen,
                            SocketFlags.None,
                            new AsyncCallback(RcvHeadData),
                            pack);
                    }
                }
                else
                {
                    OnDisConnected();
                    Clear();
                }
            }
            catch (Exception e)
            {
                IYTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error);
            }
        }
コード例 #7
0
 void ClientConnectCB(IAsyncResult ar)
 {
     try
     {
         Socket clientSkt = skt.EndAccept(ar);
         T      session   = new T();
         sessionLst.Add(session);
         session.StartRcvData(clientSkt, () =>
         {
             if (sessionLst.Contains(session))
             {
                 sessionLst.Remove(session);
             }
         });
     }
     catch (Exception e)
     {
         IYTool.LogMsg(e.Message, LogLevel.Error);
     }
     skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt);
 }
コード例 #8
0
        /// <summary>
        /// Send binary data
        /// </summary>
        public void SendMsg(byte[] data)
        {
            NetworkStream ns = null;

            try
            {
                ns = new NetworkStream(skt);
                if (ns.CanWrite)
                {
                    ns.BeginWrite(
                        data,
                        0,
                        data.Length,
                        new AsyncCallback(SendCB),
                        ns);
                }
            }
            catch (Exception e)
            {
                IYTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error);
            }
        }
コード例 #9
0
        public void StartRcvData(Socket skt, Action closeCB)
        {
            try
            {
                this.skt     = skt;
                this.closeCB = closeCB;

                OnConnected();

                IYPkg pack = new IYPkg();
                skt.BeginReceive(
                    pack.headBuff,
                    0,
                    pack.headLen,
                    SocketFlags.None,
                    new AsyncCallback(RcvHeadData),
                    pack);
            }
            catch (Exception e)
            {
                IYTool.LogMsg("StartRcvData:" + e.Message, LogLevel.Error);
            }
        }
コード例 #10
0
 /// <summary>
 /// Disconnect network
 /// </summary>
 protected virtual void OnDisConnected()
 {
     IYTool.LogMsg("Session Disconnected.", LogLevel.Info);
 }
コード例 #11
0
 /// <summary>
 /// Receive network message
 /// </summary>
 protected virtual void OnReciveMsg(T msg)
 {
     IYTool.LogMsg("Receive Network Message.", LogLevel.Info);
 }
コード例 #12
0
 /// <summary>
 /// Connect network
 /// </summary>
 protected virtual void OnConnected()
 {
     IYTool.LogMsg("New Seesion Connected.", LogLevel.Info);
 }
コード例 #13
0
 /// <summary>
 /// Send message data
 /// </summary>
 public void SendMsg(T msg)
 {
     byte[] data = IYTool.PackLenInfo(IYTool.Serialize <T>(msg));
     SendMsg(data);
 }