示例#1
0
        public static void SendAsDspMessage <T>(this TcpSocket s, T body) where T : struct
        {
            var msg = new DspMessage();

            msg.SetData <T>(body);
            //s.Send(msg.ToBytes());
            s.Send(msg);
        }
示例#2
0
 public void Disconnect()
 {
     try
     {
         //   WriteLog("Disconnect");
         if (tcp != null)
         {
             tcp.Close();
             tcp = null;
         }
         //   WriteLog("Disconnected");
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Disconnect - " + ex);
     }
 }
示例#3
0
        /// <summary>
        /// OnIdle이 호출됨에 주의
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DspMessage ReadDspMessage(this TcpSocket s)
        {
            var headerBuffer = s.Read(DspMessage.HeaderSize);

            int i       = 0;
            int prefix1 = ByteUtil.ReadInt32(headerBuffer, i);

            i += 4;
            int prefix2 = ByteUtil.ReadInt32(headerBuffer, i);

            i += 4;

            if (prefix1 != DspMessage.Prefix1 || prefix2 != DspMessage.Prefix2)
            {
                throw new Exception("Wrong Prefix - " + prefix1 + ", " + prefix2);
            }

            var msgType = (MsgType)ByteUtil.ReadInt32(headerBuffer, i);

            i += 4;

            int msgSize = ByteUtil.ReadInt32(headerBuffer, i);

            i += 4;

            //Debug.WriteLine("DspMsg Read - type:" + msgType + ", size:" + msgSize);

            DspMessage msg;

            if (msgSize <= 0)
            {
                msg = new DspMessage {
                    Type = msgType, Size = msgSize
                };
            }
            else
            {
                var payload = new byte[msgSize];
                s.Read(payload, payload.Length);
                msg = new DspMessage {
                    Type = msgType, Size = msgSize, Data = payload
                };
            }

            return(msg);
        }
示例#4
0
        public bool Connect()
        {
            try
            {
                tcp                = new TcpSocket();
                tcp.SendTimeout    = 12000;
                tcp.ReceiveTimeout = 12000;
                tcp.Connect(IP, Port, new TimeSpan(0, 0, 5));

                if (Connected != null)
                {
                    Connected(this, null);
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }