static void Main(string[] args) { ProfileSite obj = new ProfileSite(); obj.ProfileId = 1; obj.SiteId = 2; Link[] lnk1 = new Link[2]; lnk1[0] = new Link(); lnk1[1] = new Link(); lnk1[0].OriginalUrl = "wolframe.net"; lnk1[1].OriginalUrl = "test.net"; obj.Links = lnk1; var res = Serializer.getRequestContent("profileSite", "profileSite", typeof(ProfileSite), obj); Console.WriteLine("\nDocument:\n{0}", System.Text.UTF8Encoding.UTF8.GetString(res)); ProfileSite inv = (ProfileSite)Serializer.getResult(res, typeof(ProfileSite)); Console.WriteLine("Object ProfileId: {0}", inv.ProfileId); Console.WriteLine("Object SiteId: {0}", inv.SiteId); foreach (Link lnk in inv.Links) { Console.WriteLine("Object Link: {0}", lnk.OriginalUrl); } }
private void HandleAnswer(PendingRequest rq) { byte[] ln = m_connection.ReadLine(); if (ln == null) { SetState(State.Terminated, "server closed connection"); return; } else if (Protocol.IsCommand("OK", ln)) { m_answerCallback(new Answer { msgtype = Answer.MsgType.Result, id = rq.id, number = rq.number, obj = null }); } else if (Protocol.IsCommand("ERR", ln)) { m_answerCallback(new Answer { msgtype = Answer.MsgType.Failure, id = rq.id, number = rq.number, obj = Protocol.CommandArg("ERR", ln) }); } else if (Protocol.IsCommand("BAD", ln)) { SetState(State.Terminated, "protocol error"); m_answerCallback(new Answer { msgtype = Answer.MsgType.Error, id = rq.id, number = rq.number, obj = "protocol error" }); return; } else if (Protocol.IsCommand("ANSWER", ln)) { byte[] msg = m_connection.ReadContent(); if (msg == null) { SetState(State.Terminated, "server closed connection"); m_answerCallback(new Answer { msgtype = Answer.MsgType.Error, id = rq.id, number = rq.number, obj = "server closed connection" }); return; } ln = m_connection.ReadLine(); if (msg == null) { SetState(State.Terminated, "server closed connection"); m_answerCallback(new Answer { msgtype = Answer.MsgType.Error, id = rq.id, number = rq.number, obj = "server closed connection" }); return; } else if (Protocol.IsCommand("OK", ln)) { object answerobj = m_serializer.getResult(msg, rq.answertype); m_answerCallback(new Answer { msgtype = Answer.MsgType.Result, id = rq.id, number = rq.number, obj = answerobj }); } else if (Protocol.IsCommand("ERR", ln)) { m_answerCallback(new Answer { msgtype = Answer.MsgType.Failure, id = rq.id, number = rq.number, obj = Protocol.CommandArg("ERR", ln) }); } else if (Protocol.IsCommand("BAD", ln)) { SetState(State.Terminated, "protocol error"); m_answerCallback(new Answer { msgtype = Answer.MsgType.Error, id = rq.id, number = rq.number, obj = "protocol error" }); return; } } }
public void ProcessConnectionMessageDelegate(byte[] msg) { switch (m_state) { case State.Init: HandleUnrecoverableError("protocol error in init"); break; case State.Connecting: //... get empty message here when connection succeded m_state = State.HandshakeReadBanner; m_connection.IssueReadLine(); break; case State.HandshakeReadBanner: m_banner = Encoding.UTF8.GetString(msg); m_state = State.HandshakeGrantConnect; m_connection.IssueReadLine(); break; case State.HandshakeGrantConnect: if (IsCommand("OK", msg)) { m_connection.WriteLine("AUTH"); m_state = State.HandshakeMechs; m_connection.IssueReadLine(); } else { HandleUnrecoverableError("connection refused"); } break; case State.HandshakeMechs: if (IsCommand("MECHS", msg)) { m_connection.WriteLine("MECH NONE"); m_state = State.HandshakeGrantMechNONE; m_connection.IssueReadLine(); } else { HandleUnrecoverableError("authorization process refused"); } break; case State.HandshakeGrantMechNONE: if (IsCommand("OK", msg)) { m_state = State.Idle; //... authorized now HandleNextRequest(); } else { HandleUnrecoverableError("authorization refused"); } break; case State.Idle: HandleUnrecoverableError("got unexpected message in idle state"); break; case State.WaitAnswer: if (IsCommand("ANSWER", msg)) { m_state = State.WaitAnswerResult; m_connection.IssueReadContent(); } else if (IsCommand("OK", msg)) { m_processAnswer(new Answer { msgtype = Answer.MsgType.Result, id = m_request.id, content = null }); HandleNextRequest(); } else if (IsCommand("BAD", msg)) { string arg = Encoding.UTF8.GetString(msg, 3, msg.Length - 3); m_processAnswer(new Answer { msgtype = Answer.MsgType.Error, id = m_request.id, content = arg }); ClearRequestQueue(arg); m_connection.WriteLine("QUIT"); m_state = State.WaitQuit; //... we do not try to recover from protocol errors } else if (IsCommand("ERR", msg)) { string arg = Encoding.UTF8.GetString(msg, 3, msg.Length - 3); m_processAnswer(new Answer { msgtype = Answer.MsgType.Failure, id = m_request.id, content = arg }); HandleNextRequest(); } else { HandleUnrecoverableError("protocol error in wait answer"); } break; case State.WaitAnswerResult: m_answerbuf = msg; m_state = State.WaitAnswerSuccess; m_connection.IssueReadLine(); break; case State.WaitAnswerSuccess: if (IsCommand("OK", msg)) { object result = Serializer.getResult(m_answerbuf, m_request.answertype); m_processAnswer(new Answer { msgtype = Answer.MsgType.Result, id = m_request.id, content = result }); HandleNextRequest(); } else if (IsCommand("ERR", msg)) { string arg = Encoding.UTF8.GetString(msg, 3, msg.Length - 3); m_processAnswer(new Answer { msgtype = Answer.MsgType.Failure, id = m_request.id, content = arg }); HandleNextRequest(); } else { HandleUnrecoverableError("protocol error after answer result"); } break; case State.WaitQuit: if (IsCommand("BYE", msg)) { m_connection.Close(); m_state = State.Terminated; } else { // ... Ignore message (already handled in .Close()) } break; case State.Terminated: HandleUnrecoverableError("got unexpected message after termination"); break; } }