Exemplo n.º 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);
         WZTool.LogMsg("\nClient Start Success!\nConnecting To Server......", LogLevel.Info);
     }
     catch (Exception e) {
         WZTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
Exemplo n.º 2
0
 void ServerConnectCB(IAsyncResult ar)
 {
     try {
         skt.EndConnect(ar);
         session = new T();
         session.StartRcvData(skt, null);
     }
     catch (Exception e) {
         WZTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
Exemplo n.º 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);
         WZTool.LogMsg("\nServer Start Success!\nWaiting for Connecting......", LogLevel.Info);
     }
     catch (Exception e) {
         WZTool.LogMsg(e.Message, LogLevel.Error);
     }
 }
Exemplo n.º 4
0
        private void SendCB(IAsyncResult ar)
        {
            NetworkStream ns = (NetworkStream)ar.AsyncState;

            try {
                ns.EndWrite(ar);
                ns.Flush();
                ns.Close();
            }
            catch (Exception e) {
                WZTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error);
            }
        }
Exemplo n.º 5
0
 void ClientConnectCB(IAsyncResult ar)
 {
     try {
         Socket clientSkt = skt.EndAccept(ar);
         T      session   = new T();
         session.StartRcvData(clientSkt, () => {
             if (sessionLst.Contains(session))
             {
                 sessionLst.Remove(session);
             }
         });
         sessionLst.Add(session);
     }
     catch (Exception e) {
         WZTool.LogMsg(e.Message, LogLevel.Error);
     }
     skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt);
 }
Exemplo n.º 6
0
        private void RcvBodyData(IAsyncResult ar)
        {
            try {
                WZPkg pack = (WZPkg)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 = WZTool.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) {
                WZTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error);
            }
        }
Exemplo n.º 7
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) {
                WZTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error);
            }
        }
Exemplo n.º 8
0
        public void StartRcvData(Socket skt, Action closeCB)
        {
            try {
                this.skt     = skt;
                this.closeCB = closeCB;

                OnConnected();

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